示例#1
0
 def test_default(self):
     fixture = {"key": "value"}
     response = webob.Response()
     serializers.JSONResponseSerializer().default(response, fixture)
     self.assertEqual(200, response.status_int)
     content_types = list(
         filter(lambda h: h[0] == 'Content-Type', response.headerlist))
     self.assertEqual(1, len(content_types))
     self.assertEqual('application/json', response.content_type)
     self.assertEqual(b'{"key": "value"}', response.body)
示例#2
0
 def test_to_json_with_more_deep_format(self):
     fixture = collections.OrderedDict([
         ('is_public', True),
         ('name', [collections.OrderedDict([
             ('name1', 'test'),
         ])])
     ])
     expected = '{"is_public": true, "name": [{"name1": "test"}]}'
     actual = serializers.JSONResponseSerializer().to_json(fixture)
     self.assertEqual(json.loads(expected), json.loads(actual))
示例#3
0
 def __call__(self, req):
     if req.content_type == 'application/xml':
         serializer = serializers.XMLResponseSerializer()
     else:
         serializer = serializers.JSONResponseSerializer()
     resp = webob.Response(request=req)
     default_webob_exc = webob.exc.HTTPInternalServerError()
     resp.status_code = self.error.get('code', default_webob_exc.code)
     serializer.default(resp, self.error)
     return resp
示例#4
0
 def test_to_json_with_more_deep_format(self):
     fixture = {"is_public": True, "name": [{"name1": "test"}]}
     expected = '{"is_public": true, "name": [{"name1": "test"}]}'
     actual = serializers.JSONResponseSerializer().to_json(fixture)
     self.assertEqual(expected, actual)
示例#5
0
 def test_to_json_with_date_format_value(self):
     fixture = {"date": datetime.datetime(1, 3, 8, 2)}
     expected = '{"date": "0001-03-08T02:00:00"}'
     actual = serializers.JSONResponseSerializer().to_json(fixture)
     self.assertEqual(expected, actual)
示例#6
0
 def test_to_json(self):
     fixture = {"key": "value"}
     expected = '{"key": "value"}'
     actual = serializers.JSONResponseSerializer().to_json(fixture)
     self.assertEqual(expected, actual)
示例#7
0
def create_resource(options):
    """Resources resource factory method."""
    deserializer = wsgi.JSONRequestDeserializer()
    serializer = serializers.JSONResponseSerializer()
    return wsgi.Resource(ResourceController(options), deserializer, serializer)
示例#8
0
    def __call__(self, request):
        """WSGI method that controls (de)serialization and method dispatch."""
        action_args = self.get_action_args(request.environ)
        action = action_args.pop('action', None)

        # From reading the boto code, and observation of real AWS api responses
        # it seems that the AWS api ignores the content-type in the html header
        # Instead it looks at a "ContentType" GET query parameter
        # This doesn't seem to be documented in the AWS cfn API spec, but it
        # would appear that the default response serialization is XML, as
        # described in the API docs, but passing a query parameter of
        # ContentType=JSON results in a JSON serialized response...
        content_type = request.params.get("ContentType")

        LOG.info("Processing request: %(method)s %(path)s",
                 {'method': request.method, 'path': request.path})

        try:
            deserialized_request = self.dispatch(self.deserializer,
                                                 action, request)
            action_args.update(deserialized_request)

            LOG.debug(('Calling %(controller)s.%(action)s'),
                      {'controller': type(self.controller).__name__,
                       'action': action})

            action_result = self.dispatch(self.controller, action,
                                          request, **action_args)
        except TypeError as err:
            LOG.error('Exception handling resource: %s', err)
            msg = _('The server could not comply with the request since '
                    'it is either malformed or otherwise incorrect.')
            err = webob.exc.HTTPBadRequest(msg)
            http_exc = translate_exception(err, request.best_match_language())
            # NOTE(luisg): We disguise HTTP exceptions, otherwise they will be
            # treated by wsgi as responses ready to be sent back and they
            # won't make it into the pipeline app that serializes errors
            raise exception.HTTPExceptionDisguise(http_exc)
        except webob.exc.HTTPException as err:
            if isinstance(err, aws_exception.HeatAPIException):
                # The AWS compatible API's don't use faultwrap, so
                # we want to detect the HeatAPIException subclasses
                # and raise rather than wrapping in HTTPExceptionDisguise
                raise
            if not isinstance(err, webob.exc.HTTPError):
                # Some HTTPException are actually not errors, they are
                # responses ready to be sent back to the users, so we don't
                # error log, disguise or translate those
                raise
            if isinstance(err, webob.exc.HTTPServerError):
                LOG.error(
                    "Returning %(code)s to user: %(explanation)s",
                    {'code': err.code, 'explanation': err.explanation})
            http_exc = translate_exception(err, request.best_match_language())
            raise exception.HTTPExceptionDisguise(http_exc)
        except exception.HeatException as err:
            raise translate_exception(err, request.best_match_language())
        except Exception as err:
            log_exception(err, sys.exc_info())
            raise translate_exception(err, request.best_match_language())
        # Here we support either passing in a serializer or detecting it
        # based on the content type.
        try:
            serializer = self.serializer
            if serializer is None:
                if content_type == "JSON":
                    serializer = serializers.JSONResponseSerializer()
                else:
                    serializer = serializers.XMLResponseSerializer()

            response = webob.Response(request=request)
            self.dispatch(serializer, action, response, action_result)
            return response

        # return unserializable result (typically an exception)
        except Exception:
            # Here we should get API exceptions derived from HeatAPIException
            # these implement get_unserialized_body(), which allow us to get
            # a dict containing the unserialized error response.
            # We only need to serialize for JSON content_type, as the
            # exception body is pre-serialized to the default XML in the
            # HeatAPIException constructor
            # If we get something else here (e.g a webob.exc exception),
            # this will fail, and we just return it without serializing,
            # which will not conform to the expected AWS error response format
            if content_type == "JSON":
                try:
                    err_body = action_result.get_unserialized_body()
                    serializer.default(action_result, err_body)
                except Exception:
                    LOG.warning("Unable to serialize exception response")

            return action_result
示例#9
0
def create_resource(options):
    """Software deployments resource factory method."""
    deserializer = wsgi.JSONRequestDeserializer()
    serializer = serializers.JSONResponseSerializer()
    return wsgi.Resource(SoftwareDeploymentController(options), deserializer,
                         serializer)
示例#10
0
def create_resource(options):
    deserializer = wsgi.JSONRequestDeserializer()
    serializer = serializers.JSONResponseSerializer()
    return wsgi.Resource(ServiceController(options), deserializer, serializer)