From 334f2215f0533e8181d40cd086e927e7913739f2 Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Mon, 11 Jul 2011 12:31:38 -0400 Subject: minor refactoring --- nova/api/openstack/wsgi.py | 52 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index 7a8376722..9a273bf6e 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -57,16 +57,26 @@ class Request(webob.Request): return content_type -class TextDeserializer(object): - """Custom request body deserialization based on controller action name.""" +class ActionDispatcher(object): + """Maps method name to local methods through action name.""" - def deserialize(self, datastring, action='default'): - """Find local deserialization method and parse request body.""" + def dispatch(self, *args, **kwargs): + """Find and call local method.""" + action = kwargs.pop('action', 'default') action_method = getattr(self, str(action), self.default) - return action_method(datastring) + return action_method(*args, **kwargs) + + def default(self, data): + raise NotImplementedError() + + +class TextDeserializer(ActionDispatcher): + """Default request body deserialization""" + + def deserialize(self, datastring, action='default'): + return self.dispatch(datastring, action=action) def default(self, datastring): - """Default deserialization code should live here""" return {} @@ -128,8 +138,13 @@ class XMLDeserializer(TextDeserializer): return {'body': self._from_xml(datastring)} -class RequestHeadersDeserializer(object): +class RequestHeadersDeserializer(ActionDispatcher): + """Default request headers deserializer""" + def deserialize(self, request, action): + return self.dispatch(request, action=action) + + def default(self, request): return {} @@ -220,20 +235,18 @@ class RequestDeserializer(object): return args -class DictSerializer(object): - """Custom response body serialization based on controller action name.""" +class DictSerializer(ActionDispatcher): + """Default request body serialization""" def serialize(self, data, action='default'): - """Find local serialization method and encode response body.""" - action_method = getattr(self, str(action), self.default) - return action_method(data) + return self.dispatch(data, action=action) def default(self, data): - """Default serialization code should live here""" - raise NotImplementedError() + return "" class JSONDictSerializer(DictSerializer): + """Default JSON request body serialization""" def default(self, data): return utils.dumps(data) @@ -320,8 +333,13 @@ class XMLDictSerializer(DictSerializer): return result -class ResponseHeadersSerializer(object): +class ResponseHeadersSerializer(ActionDispatcher): + """Default response headers serialization""" + def serialize(self, response, data, action): + self.dispatch(response, data, action=action) + + def default(self, response, data): response.status_int = 200 @@ -410,7 +428,9 @@ class Resource(wsgi.Application): #TODO(bcwaldon): find a more elegant way to pass through non-dict types if type(action_result) is dict: - response = self.serializer.serialize(action_result, accept, action) + response = self.serializer.serialize(action_result, + accept, + action=action) else: response = action_result -- cgit From ad23d0f354b8698b5314ed2a55e5a4d17abffba0 Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Mon, 11 Jul 2011 13:16:22 -0400 Subject: allowing controllers to return Nonew --- nova/api/openstack/wsgi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/api') diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index 9a273bf6e..8eff9e441 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -427,7 +427,7 @@ class Resource(wsgi.Application): action_result = self.dispatch(request, action, args) #TODO(bcwaldon): find a more elegant way to pass through non-dict types - if type(action_result) is dict: + if type(action_result) is dict or action_result is None: response = self.serializer.serialize(action_result, accept, action=action) -- cgit