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
Exemplo n.º 2
0
 def test_rpc_request(self):
     r = RpcRequest("product", "version", "action_name")
     # accept format
     self.assertIsNone(r.get_accept_format())
     r.set_accept_format('json')
     self.assertEqual(r.get_accept_format(), "json")
     # action name
     self.assertEqual(r.get_action_name(), "action_name")
     r.set_action_name('new action name')
     self.assertEqual(r.get_action_name(), "new action name")
     # body params
     self.assertDictEqual(r.get_body_params(), {})
     r.set_body_params({'key': 'value'})
     self.assertDictEqual(r.get_body_params(), {'key': 'value'})
     r.add_body_params("key2", "value2")
     self.assertDictEqual(r.get_body_params(), {
         'key': 'value',
         'key2': 'value2'
     })
     # content
     self.assertIsNone(r.get_content())
     r.set_content("content")
     self.assertEqual(r.get_content(), "content")
     # headers
     self.assertDictEqual(r.get_headers(), {'x-sdk-invoke-type': 'normal'})
     r.set_headers({})
     self.assertDictEqual(r.get_headers(), {})
     r.add_header("key", "value")
     self.assertDictEqual(r.get_headers(), {"key": "value"})
     # location endpoint type
     self.assertEqual(r.get_location_endpoint_type(), 'openAPI')
     # no set_location_endpoint_type ??
     # location_service_code
     self.assertEqual(r.get_location_service_code(), None)
     r.set_location_service_code('new service code')
     self.assertEqual(r.get_location_service_code(), 'new service code')
     # method
     self.assertEqual(r.get_method(), 'GET')
     r.set_method('POST')
     self.assertEqual(r.get_method(), 'POST')
     # product
     self.assertEqual(r.get_product(), 'product')
     r.set_product('new-product')
     self.assertEqual(r.get_product(), 'new-product')
     # protocol_type
     self.assertEqual(r.get_protocol_type(), "http")
     r.set_protocol_type('https')
     self.assertEqual(r.get_protocol_type(), "https")
     # query params
     self.assertDictEqual(r.get_query_params(), {})
     r.set_query_params({'key': 'value'})
     self.assertDictEqual(r.get_query_params(), {'key': 'value'})
     r.add_query_param("key2", "value2")
     self.assertDictEqual(r.get_query_params(), {
         'key': 'value',
         "key2": "value2"
     })
     # signed_header
     self.assertEqual(r.get_signed_header(), {})
     r.add_header("x-acs-xxx", "value")
     self.assertDictEqual(r.get_signed_header(), {"x-acs-xxx": "value"})
     # style
     self.assertEqual(r.get_style(), "RPC")
     # uri params
     self.assertEqual(r.get_uri_params(), None)
     r.set_uri_params({'user': "******"})
     self.assertDictEqual(r.get_uri_params(), {'user': '******'})
     # uri pattern
     self.assertEqual(r.get_uri_pattern(), None)
     r.set_uri_pattern('/users/:userid')
     self.assertEqual(r.get_uri_pattern(), '/users/:userid')
     # version
     self.assertEqual(r.get_version(), "version")
     r.set_version('2014-10-18')
     self.assertEqual(r.get_version(), "2014-10-18")
     # user-agent
     self.assertEqual(r.get_headers().get('User-Agent'), None)
     r.set_user_agent("user-agent")
     self.assertEqual(r.get_headers().get('User-Agent'), "user-agent")
     # content-type
     self.assertEqual(r.get_headers().get('Content-Type'), None)
     r.set_content_type("application/json")
     self.assertEqual(r.get_headers().get('Content-Type'),
                      "application/json")
     # endpoint
     self.assertEqual(r.endpoint, None)
     r.set_endpoint('endpoint')
     self.assertEqual(r.endpoint, "endpoint")