diff options
author | Milo Casagrande <milo.casagrande@linaro.org> | 2014-07-30 10:51:35 +0200 |
---|---|---|
committer | Milo Casagrande <milo.casagrande@linaro.org> | 2014-07-30 10:51:35 +0200 |
commit | ecb44ce6b03d5e4d8d444652f9ec2face4e74f56 (patch) | |
tree | 97679af74f199c3fd1e3cc19dc371482894e0bf4 /app/handlers/base.py | |
parent | 2bb966a61c272334d33a65d2d3dc2587afd8418a (diff) |
Add HadlerResponse class.
* Create a general handler response class used to build
response object that will be passed via callbacks.
Change-Id: If0a025f86862dae609a47c75f04450f7d91729f0
Diffstat (limited to 'app/handlers/base.py')
-rw-r--r-- | app/handlers/base.py | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/app/handlers/base.py b/app/handlers/base.py index 3d8043c..b7b1494 100644 --- a/app/handlers/base.py +++ b/app/handlers/base.py @@ -39,6 +39,7 @@ from tornado.web import ( ) from handlers.decorators import protected +from handlers.response import HandlerResponse from models import ( AGGREGATE_KEY, CREATED_KEY, @@ -140,20 +141,28 @@ class BaseHandler(RequestHandler): """Create a valid JSON response based on its type. :param response: The response we have from a query to the database. - :return A (int, str) tuple composed of the status code, and the - message. """ + headers = {} + if isinstance(response, (types.DictionaryType, types.ListType)): - status, message = 200, dumps(response) + status_code, message = 200, dumps(response) elif isinstance(response, types.IntType): - status, message = response, self._get_status_message(response) + status_code, message = response, self._get_status_message(response) elif isinstance(response, types.NoneType): - status, message = 404, self._get_status_message(404) + status_code, message = 404, self._get_status_message(404) + elif isinstance(response, HandlerResponse): + status_code, message, headers = \ + response.status_code, response.message, response.headers else: - status, message = 200, self._get_status_message(200) + status_code, message = 200, self._get_status_message(200) + + message = message or self._get_status_message(status_code) - self.set_status(status_code=status) - self.write(dict(code=status, result=message)) + self.set_status(status_code=status_code, reason=message) + self.write(dict(code=status_code, result=message)) + if headers: + for key, val in headers.iteritems(): + self.add_header(key, val) self.finish() def _get_callback(self, result): |