def _make_http_response(self, endpoint, request, specific_signer=None): body_params = request.get_body_params() if body_params: body = urllib.urlencode(body_params) request.set_content(body) request.set_content_type(format_type.APPLICATION_FORM) elif request.get_content( ) and "Content-Type" not in request.get_headers(): request.set_content_type(format_type.APPLICATION_OCTET_STREAM) method = request.get_method() signer = self._signer if specific_signer is None else specific_signer header, url = signer.sign(self.__region_id, request) if self.get_user_agent() is not None: header['User-Agent'] = self.get_user_agent() if header is None: header = {} header['x-sdk-client'] = 'python/2.0.0' protocol = request.get_protocol_type() response = HttpResponse(endpoint, url, method, header, protocol, request.get_content(), self._port, timeout=self._timeout) if body_params: body = urllib.urlencode(request.get_body_params()) response.set_content(body, "utf-8", format_type.APPLICATION_FORM) return response
def _make_http_response(self, endpoint, request, read_timeout, connect_timeout, specific_signer=None): body_params = request.get_body_params() if body_params: content_type = request.get_headers().get('Content-Type') if content_type and format_type.APPLICATION_JSON in content_type: body = json.dumps(body_params) request.set_content(body) elif content_type and format_type.APPLICATION_XML in content_type: body = aliyunsdkcore.utils.parameter_helper.to_xml(body_params) request.set_content(body) else: body = urlencode(body_params) request.set_content(body) request.set_content_type(format_type.APPLICATION_FORM) elif request.get_content( ) and "Content-Type" not in request.get_headers(): request.set_content_type(format_type.APPLICATION_OCTET_STREAM) method = request.get_method() if isinstance(request, CommonRequest): request.trans_to_acs_request() signer = self._signer if specific_signer is None else specific_signer header, url = signer.sign(self._region_id, request) base = self.user_agent_header() extra_agent = self.handle_extra_agent(request) default_agent = self.default_user_agent() user_agent = self.merge_user_agent(default_agent, extra_agent) for key, value in user_agent.items(): base += ' %s/%s' % (key, value) header['User-Agent'] = base header['x-sdk-client'] = 'python/2.0.0' protocol = request.get_protocol_type() response = HttpResponse(endpoint, url, method, header, protocol, request.get_content(), self._port, read_timeout=read_timeout, connect_timeout=connect_timeout, verify=self.get_verify(), session=self.session, proxy=self.proxy) if body_params: response.set_content(body, "utf-8", request.get_headers().get('Content-Type')) return response
def test_http_request(self): res = HttpResponse() self.assertFalse(res.get_ssl_enabled()) res.set_ssl_enable(True) self.assertTrue(res.get_ssl_enabled()) res = HttpResponse(protocol=HTTPS) self.assertTrue(res.get_ssl_enabled())
def do_action_with_exception(self, request: RpcRequest): request.set_accept_format('JSON') path = request.path_pattern if hasattr( request, 'path_pattern') else '/api/acs/openapi' if self._endpoint: endpoint = self._endpoint elif request.endpoint: endpoint = request.endpoint else: endpoint = self._resolve_endpoint(request) request.add_query_param('Product', request.get_product()) request.add_query_param('RegionId', self.get_region_id()) request.add_query_param('Action', request.get_action_name()) request.add_query_param('Version', request.get_version()) request._params.update(self.query_params) sign_params = dict(request._params) query, sign_str = get_signed_url(sign_params, self.get_access_key(), self.get_access_secret(), request.get_accept_format(), request.get_method(), request.get_body_params(), self.query_params) request.string_to_sign = sign_str endpoint += path response = HttpResponse( endpoint, query, request.get_method(), request.get_signed_header(self.get_region_id(), self.get_access_key(), self.get_access_secret()), request.get_protocol_type(), request.get_content(), self._port) if self._read_timeout: response._timeout = response.__read_timeout = self._read_timeout if self._connect_timeout: response.__connect_timeout = self._connect_timeout try: status, headers, body = response.get_response_object() except IOError as e: exception = ClientException(error_code.SDK_HTTP_ERROR, compat.ensure_string('%s' % e)) return None, None, None, exception exception = self.get_server_exception(status, body) if exception: raise exception return body
def _make_http_response(self, endpoint, request, read_timeout, connect_timeout, specific_signer=None): body_params = request.get_body_params() if body_params: body = urlencode(body_params) request.set_content(body) request.set_content_type(format_type.APPLICATION_FORM) elif request.get_content(): if 'Content-Type' not in request.get_headers() or \ request.get_headers()['Content-Type'] == format_type.APPLICATION_FORM: request.set_content_type(format_type.APPLICATION_OCTET_STREAM) method = request.get_method() signer = self._signer if specific_signer is None else specific_signer header, url = signer.sign(self._region_id, request) base = self.user_agent_header() extra_agent = self.handle_extra_agent(request) default_agent = self.default_user_agent() user_agent = self.merge_user_agent(default_agent, extra_agent) for key, value in user_agent.items(): base += ' %s/%s' % (key, value) header['User-Agent'] = base header['x-sdk-client'] = 'python/2.0.0' protocol = request.get_protocol_type() response = HttpResponse(endpoint, url, method, header, protocol, request.get_content(), self._port, read_timeout=read_timeout, connect_timeout=connect_timeout, verify=self.get_verify()) if body_params: body = urlencode(request.get_body_params()) response.set_content(body, "utf-8", format_type.APPLICATION_FORM) return response
def test_http_debug(self): os.environ.setdefault('DEBUG', 'SDK') http_debug = os.environ.get('DEBUG') if http_debug is not None and http_debug.lower() == 'sdk': request = CommonRequest( domain="ecs.aliyuncs.com", version="2014-05-26", action_name="DescribeRegions") request.set_endpoint("localhost") make_request = HttpResponse(request.endpoint, '/') request.headers = {'User-Agent': 'ALIBABACloud'} content = make_request.prepare_http_debug(request, '>') self.assertTrue('User-Agent' in content) client = self.init_client() response = self.do_request(client, request) if sys.version_info[0] == 3: response = make_request.prepare_http_debug(response, '<') self.assertTrue('User-Agent' in response) os.environ.pop('DEBUG', None)
def test_response_set_path(self): response = HttpResponse(verify=False) os.environ["ALIBABA_CLOUD_CA_BUNDLE"] = '/path/cacerts.pem' self.assertEqual(response.get_verify_value(), False) response = HttpResponse(verify=True) os.environ["ALIBABA_CLOUD_CA_BUNDLE"] = '/path/cacerts.pem' self.assertEqual(response.get_verify_value(), True) response = HttpResponse(verify=None) os.environ["ALIBABA_CLOUD_CA_BUNDLE"] = '/path/cacerts.pem' self.assertEqual(response.get_verify_value(), '/path/cacerts.pem') response = HttpResponse(verify='/path/cacerts1.pem') os.environ["ALIBABA_CLOUD_CA_BUNDLE"] = '/path/cacerts.pem' self.assertEqual(response.get_verify_value(), '/path/cacerts1.pem') os.environ.pop("ALIBABA_CLOUD_CA_BUNDLE", None) response = HttpResponse() self.assertEqual(response.get_verify_value(), True) os.environ["ALIBABA_CLOUD_CA_BUNDLE"] = '/path/cacerts.pem' response = HttpResponse() self.assertEqual(response.get_verify_value(), '/path/cacerts.pem') os.environ.pop("ALIBABA_CLOUD_CA_BUNDLE", None)