diff options
author | Michael Gundlach <michael.gundlach@rackspace.com> | 2010-09-27 12:50:20 -0400 |
---|---|---|
committer | Michael Gundlach <michael.gundlach@rackspace.com> | 2010-09-27 12:50:20 -0400 |
commit | 1c978e8414b5841c4caf856c80f385026600f54e (patch) | |
tree | d8ee29c65622779b5180c77b8ba7d15bdba18a3d /nova/wsgi.py | |
parent | d642716db42b229e879f6f4673f166beb8d55faa (diff) | |
download | nova-1c978e8414b5841c4caf856c80f385026600f54e.tar.gz nova-1c978e8414b5841c4caf856c80f385026600f54e.tar.xz nova-1c978e8414b5841c4caf856c80f385026600f54e.zip |
Support content type detection in serializer
Diffstat (limited to 'nova/wsgi.py')
-rw-r--r-- | nova/wsgi.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/nova/wsgi.py b/nova/wsgi.py index 8a4e2a9f4..ac319db40 100644 --- a/nova/wsgi.py +++ b/nova/wsgi.py @@ -29,6 +29,7 @@ import eventlet.wsgi eventlet.patcher.monkey_patch(all=False, socket=True) import routes import routes.middleware +import webob import webob.dec import webob.exc @@ -239,11 +240,19 @@ class Serializer(object): 'metadata' is an optional dict mapping MIME types to information needed to serialize a dictionary to that type. """ - self.environ = environ self.metadata = metadata or {} - self._methods = { - 'application/json': self._to_json, - 'application/xml': self._to_xml} + req = webob.Request(environ) + suffix = req.path_info.split('.')[-1].lower() + if suffix == 'json': + self.handler = self._to_json + elif suffix == 'xml': + self.handler = self._to_xml + elif 'application/json' in req.accept: + self.handler = self._to_json + elif 'application/xml' in req.accept: + self.handler = self._to_xml + else: + self.handler = self._to_json # default def to_content_type(self, data): """ @@ -251,9 +260,7 @@ class Serializer(object): will be decided based on the Content Type requested in self.environ: by Accept: header, or by URL suffix. """ - mimetype = 'application/xml' - # TODO(gundlach): determine mimetype from request - return self._methods.get(mimetype, repr)(data) + return self.handler(data) def _to_json(self, data): import json |