diff options
| author | Michael Gundlach <michael.gundlach@rackspace.com> | 2010-09-02 13:43:23 +0000 |
|---|---|---|
| committer | Tarmac <> | 2010-09-02 13:43:23 +0000 |
| commit | d0a353e4c46773f23a25ff372ed204d17e89e049 (patch) | |
| tree | c9bf5a6b455f4a8e60933c8dc8b9292c409aea83 | |
| parent | d1237038c52c1578086337f038c5b812bf6ab473 (diff) | |
| parent | 89d8fb48628b6ff72a6baff1dca8772d0a7587f8 (diff) | |
Replace an if/else with a dict lookup to a factory method.
| -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..8a4e2a9f4 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.""" |
