diff options
author | Lars Wirzenius <liw@liw.fi> | 2018-03-24 15:48:12 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2018-03-24 15:48:12 +0200 |
commit | 88535a2107db72063031b2be07e9adddc6e58e1c (patch) | |
tree | 7b93befa0ca47e61c3242e1782cf7623b46f8d43 | |
parent | 6928b6a04aef1789452c3d02d785dfc7f4406e75 (diff) | |
download | apifw-88535a2107db72063031b2be07e9adddc6e58e1c.tar.gz |
Change: support bottle.static_file
This is very ugly. But it solves the problem, and apifw will need to
be re-designed to support this properly.
-rw-r--r-- | apifw/__init__.py | 1 | ||||
-rw-r--r-- | apifw/http.py | 18 | ||||
-rw-r--r-- | apitest.py | 13 |
3 files changed, 24 insertions, 8 deletions
diff --git a/apifw/__init__.py b/apifw/__init__.py index 7c1ce5b..348cf76 100644 --- a/apifw/__init__.py +++ b/apifw/__init__.py @@ -17,6 +17,7 @@ from .apixface import Api from .http import ( HttpTransaction, Response, + StaticFile, HTTP_OK, HTTP_CREATED, HTTP_UNAUTHORIZED, diff --git a/apifw/http.py b/apifw/http.py index 7b5a8e9..7761874 100644 --- a/apifw/http.py +++ b/apifw/http.py @@ -14,6 +14,9 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. +import bottle + + HTTP_OK = 200 HTTP_CREATED = 201 HTTP_UNAUTHORIZED = 401 @@ -105,10 +108,13 @@ class HttpTransaction: self._counter() self._log_request() data = callback(*args, **kwargs) + self._logger({'data': type(data)}) self._log_callback() self.amend_response() self._log_response() return data + except StaticFile as e: + return bottle.static_file(e.filename, '/') except SystemExit: # If we're exiting, we exit. No need to log an error. raise @@ -118,11 +124,21 @@ class HttpTransaction: raise +class StaticFile(Exception): + + def __init__(self, filename): + super().__init__() + self.filename = filename + + def __getitem__(self, key): + return None + + class Response: def __init__(self, values): self._dict = {} - self._keys = ['status', 'headers', 'body'] + self._keys = ['status', 'headers', 'body', 'static-file'] for key in self._keys: self[key] = '' for key, value in values.items(): @@ -19,7 +19,9 @@ import logging import os +import tempfile +import bottle import yaml import apifw @@ -76,13 +78,10 @@ class Api(apifw.Api): }) def download(self, content_type, body, **kwargs): - return apifw.Response({ - 'status': apifw.HTTP_OK, - 'body': self._blob, - 'headers': { - 'Content-Type': 'text/plain', - }, - }) + fd, filename = tempfile.mkstemp() + os.write(fd, self._blob) + os.close(fd) + raise apifw.StaticFile(filename) # We want logging. gunicorn provides logging, but only of its own |