def do_roarequest_with_form(self, action, version, protocol, method, auth_type, pathname, body_type, request, runtime): """ Encapsulate the request and invoke the network with form body @type action: str @param action: api name @type version: str @param version: product version @type protocol: str @param protocol: http or https @type method: str @param method: e.g. GET @type auth_type: str @param auth_type: authorization type e.g. AK @type pathname: str @param pathname: pathname of every api @type body_type: str @param body_type: response body type e.g. String @param request: object of OpenApiRequest @param runtime: which controls some details of call api, such as retry times @rtype: dict @return: the response """ request.validate() runtime.validate() _runtime = { 'timeouted': 'retry', 'readTimeout': UtilClient.default_number(runtime.read_timeout, self._read_timeout), 'connectTimeout': UtilClient.default_number(runtime.connect_timeout, self._connect_timeout), 'httpProxy': UtilClient.default_string(runtime.http_proxy, self._http_proxy), 'httpsProxy': UtilClient.default_string(runtime.https_proxy, self._https_proxy), 'noProxy': UtilClient.default_string(runtime.no_proxy, self._no_proxy), 'socks5Proxy': UtilClient.default_string(runtime.socks_5proxy, self._socks_5proxy), 'socks5NetWork': UtilClient.default_string(runtime.socks_5net_work, self._socks_5net_work), 'maxIdleConns': UtilClient.default_number(runtime.max_idle_conns, self._max_idle_conns), 'retry': { 'retryable': runtime.autoretry, 'maxAttempts': UtilClient.default_number(runtime.max_attempts, 3) }, 'backoff': { 'policy': UtilClient.default_string(runtime.backoff_policy, 'no'), 'period': UtilClient.default_number(runtime.backoff_period, 1) }, 'ignoreSSL': runtime.ignore_ssl } _last_request = None _last_exception = None _now = time.time() _retry_times = 0 while TeaCore.allow_retry(_runtime.get('retry'), _retry_times, _now): if _retry_times > 0: _backoff_time = TeaCore.get_backoff_time(_runtime.get('backoff'), _retry_times) if _backoff_time > 0: TeaCore.sleep(_backoff_time) _retry_times = _retry_times + 1 try: _request = TeaRequest() _request.protocol = UtilClient.default_string(self._protocol, protocol) _request.method = method _request.pathname = pathname _request.headers = TeaCore.merge({ 'date': UtilClient.get_date_utcstring(), 'host': self._endpoint, 'accept': 'application/json', 'x-acs-signature-nonce': UtilClient.get_nonce(), 'x-acs-signature-method': 'HMAC-SHA1', 'x-acs-signature-version': '1.0', 'x-acs-version': version, 'x-acs-action': action, 'user-agent': UtilClient.get_user_agent(self._user_agent) }, request.headers) if not UtilClient.is_unset(request.body): m = UtilClient.assert_as_map(request.body) _request.body = OpenApiUtilClient.to_form(m) _request.headers['content-type'] = 'application/x-www-form-urlencoded' if not UtilClient.is_unset(request.query): _request.query = request.query if not UtilClient.equal_string(auth_type, 'Anonymous'): access_key_id = self.get_access_key_id() access_key_secret = self.get_access_key_secret() security_token = self.get_security_token() if not UtilClient.empty(security_token): _request.headers['x-acs-accesskey-id'] = access_key_id _request.headers['x-acs-security-token'] = security_token string_to_sign = OpenApiUtilClient.get_string_to_sign(_request) _request.headers['authorization'] = 'acs %s:%s' % (TeaConverter.to_unicode(access_key_id), TeaConverter.to_unicode(OpenApiUtilClient.get_roasignature(string_to_sign, access_key_secret))) _last_request = _request _response = TeaCore.do_action(_request, _runtime) if UtilClient.equal_number(_response.status_code, 204): return { 'headers': _response.headers } if UtilClient.is_4xx(_response.status_code) or UtilClient.is_5xx(_response.status_code): _res = UtilClient.read_as_json(_response.body) err = UtilClient.assert_as_map(_res) raise TeaException({ 'code': '%s' % TeaConverter.to_unicode(self.default_any(err.get('Code'), err.get('code'))), 'message': 'code: %s, %s request id: %s' % (TeaConverter.to_unicode(_response.status_code), TeaConverter.to_unicode(self.default_any(err.get('Message'), err.get('message'))), TeaConverter.to_unicode(self.default_any(err.get('RequestId'), err.get('requestId')))), 'data': err }) if UtilClient.equal_string(body_type, 'binary'): resp = { 'body': _response.body, 'headers': _response.headers } return resp elif UtilClient.equal_string(body_type, 'byte'): byt = UtilClient.read_as_bytes(_response.body) return { 'body': byt, 'headers': _response.headers } elif UtilClient.equal_string(body_type, 'string'): str = UtilClient.read_as_string(_response.body) return { 'body': str, 'headers': _response.headers } elif UtilClient.equal_string(body_type, 'json'): obj = UtilClient.read_as_json(_response.body) res = UtilClient.assert_as_map(obj) return { 'body': res, 'headers': _response.headers } elif UtilClient.equal_string(body_type, 'array'): arr = UtilClient.read_as_json(_response.body) return { 'body': arr, 'headers': _response.headers } else: return { 'headers': _response.headers } except Exception as e: if TeaCore.is_retryable(e): _last_exception = e continue raise e raise UnretryableException(_last_request, _last_exception)
def test_get_roa_signature(self): request = TeaRequest() str_to_sign = Client.get_string_to_sign(request) signature = Client.get_roasignature(str_to_sign, 'secret') self.assertEqual('GET\n\n\n\n\n', str_to_sign) self.assertEqual('XGXDWA78AEvx/wmfxKoVCq/afWw=', signature)
def do_roarequest_with_form(self, action, version, protocol, method, auth_type, pathname, body_type, request, runtime): """ Encapsulate the request and invoke the network with form body :type action: str :param action: api name :type version: str :param version: product version :type protocol: str :param protocol: http or https :type method: str :param method: e.g. GET :type auth_type: str :param auth_type: authorization type e.g. AK :type pathname: str :param pathname: pathname of every api :type body_type: str :param body_type: response body type e.g. String :param request: object of OpenApiRequest :param runtime: which controls some details of call api, such as retry times :return: the response """ request.validate() runtime.validate() _runtime = { "timeouted": "retry", "readTimeout": UtilClient.default_number(runtime.read_timeout, self._read_timeout), "connectTimeout": UtilClient.default_number(runtime.connect_timeout, self._connect_timeout), "httpProxy": UtilClient.default_string(runtime.http_proxy, self._http_proxy), "httpsProxy": UtilClient.default_string(runtime.https_proxy, self._https_proxy), "noProxy": UtilClient.default_string(runtime.no_proxy, self._no_proxy), "maxIdleConns": UtilClient.default_number(runtime.max_idle_conns, self._max_idle_conns), "retry": { "retryable": runtime.autoretry, "maxAttempts": UtilClient.default_number(runtime.max_attempts, 3) }, "backoff": { "policy": UtilClient.default_string(runtime.backoff_policy, "no"), "period": UtilClient.default_number(runtime.backoff_period, 1) }, "ignoreSSL": runtime.ignore_ssl } _last_request = None _last_exception = None _now = time.time() _retry_times = 0 while TeaCore.allow_retry(_runtime.get('retry'), _retry_times, _now): if _retry_times > 0: _backoff_time = TeaCore.get_backoff_time( _runtime.get('backoff'), _retry_times) if _backoff_time > 0: TeaCore.sleep(_backoff_time) _retry_times = _retry_times + 1 try: _request = TeaRequest() _request.protocol = UtilClient.default_string( self._protocol, protocol) _request.method = method _request.pathname = pathname _request.headers = TeaCore.merge( { "date": UtilClient.get_date_utcstring(), "host": self._endpoint, "accept": "application/json", "x-acs-signature-nonce": UtilClient.get_nonce(), "x-acs-signature-method": "HMAC-SHA1", "x-acs-signature-version": "1.0", "x-acs-version": version, "x-acs-action": action, "user-agent": UtilClient.get_user_agent( self._user_agent) }, request.headers) if not UtilClient.is_unset(request.body): _request.body = OpenApiUtilClient.to_form(request.body) _request.headers[ "content-type"] = "application/x-www-form-urlencoded" if not UtilClient.is_unset(request.query): _request.query = request.query if not UtilClient.equal_string(auth_type, "Anonymous"): access_key_id = self._credential.get_access_key_id() access_key_secret = self._credential.get_access_key_secret( ) security_token = self._credential.get_security_token() if not UtilClient.empty(security_token): _request.headers["x-acs-accesskey-id"] = access_key_id _request.headers[ "x-acs-security-token"] = security_token string_to_sign = OpenApiUtilClient.get_string_to_sign( _request) _request.headers["authorization"] = "acs " + str( access_key_id) + ":" + str( OpenApiUtilClient.get_roasignature( string_to_sign, access_key_secret)) + "" _last_request = _request _response = TeaCore.do_action(_request, _runtime) if UtilClient.equal_number(_response.status_code, 204): return {"headers": _response.headers} if UtilClient.is_4xx( _response.status_code) or UtilClient.is_5xx( _response.status_code): _res = UtilClient.read_as_json(_response.body) err = UtilClient.assert_as_map(_res) raise TeaException({ "message": err.get('Message'), "data": err, "code": err.get('Code') }) if UtilClient.equal_string(body_type, "binary"): resp = { "body": _response.body, "headers": _response.headers } return resp elif UtilClient.equal_string(body_type, "byte"): byt = UtilClient.read_as_bytes(_response.body) return {"body": byt, "headers": _response.headers} elif UtilClient.equal_string(body_type, "string"): str = UtilClient.read_as_string(_response.body) return {"body": str, "headers": _response.headers} elif UtilClient.equal_string(body_type, "json"): obj = UtilClient.read_as_json(_response.body) res = UtilClient.assert_as_map(obj) return {"body": res, "headers": _response.headers} else: return {"headers": _response.headers} except Exception as e: if TeaCore.is_retryable(e): _last_exception = e continue raise e raise UnretryableException(_last_request, _last_exception)