diff options
author | Michael Gundlach <michael.gundlach@rackspace.com> | 2010-09-01 16:27:46 -0400 |
---|---|---|
committer | Michael Gundlach <michael.gundlach@rackspace.com> | 2010-09-01 16:27:46 -0400 |
commit | 7595f752137d13449b4cde5680e722582a36c1de (patch) | |
tree | 28ebeaf3a5f09e8444cd0b3f33f575dcf49bfe5a /nova/wsgi.py | |
parent | d1237038c52c1578086337f038c5b812bf6ab473 (diff) | |
download | nova-7595f752137d13449b4cde5680e722582a36c1de.tar.gz nova-7595f752137d13449b4cde5680e722582a36c1de.tar.xz nova-7595f752137d13449b4cde5680e722582a36c1de.zip |
Abstractified generalization mechanism
Diffstat (limited to 'nova/wsgi.py')
-rw-r--r-- | nova/wsgi.py | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/nova/wsgi.py b/nova/wsgi.py index bec0a7b1c..543ecb71e 100644 --- a/nova/wsgi.py +++ b/nova/wsgi.py @@ -241,6 +241,9 @@ class Serializer(object): """ self.environ = environ self.metadata = metadata or {} + self._methods = { + 'application/json', self._to_json, + 'application/xml', self._to_xml} def to_content_type(self, data): """ @@ -250,20 +253,20 @@ class Serializer(object): """ mimetype = 'application/xml' # TODO(gundlach): determine mimetype from request - - if mimetype == 'application/json': - import json - return json.dumps(data) - elif mimetype == 'application/xml': - metadata = self.metadata.get('application/xml', {}) - # We expect data to contain a single key which is the XML root. - root_key = data.keys()[0] - from xml.dom import minidom - doc = minidom.Document() - node = self._to_xml_node(doc, metadata, root_key, data[root_key]) - return node.toprettyxml(indent=' ') - else: - return repr(data) + return self._methods.get(mimetype, repr)(data) + + def _to_json(self, data): + import json + return json.dumps(data) + + def _to_xml(self, data): + metadata = self.metadata.get('application/xml', {}) + # We expect data to contain a single key which is the XML root. + root_key = data.keys()[0] + from xml.dom import minidom + doc = minidom.Document() + node = self._to_xml_node(doc, metadata, root_key, data[root_key]) + return node.toprettyxml(indent=' ') def _to_xml_node(self, doc, metadata, nodename, data): """Recursive method to convert data members to XML nodes.""" |