diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-11-18 19:59:56 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-11-18 19:59:56 +0100 |
commit | 18753714a35b6c8e5af4b6076973f490a1ba2097 (patch) | |
tree | 15be406183e1b505fb312a954af20531ac825cd3 /ick2/controllerapi.py | |
parent | 1c9c77331874c8932f221c454346568b23c25024 (diff) | |
download | ick2-18753714a35b6c8e5af4b6076973f490a1ba2097.tar.gz |
Refactor: move HTTP response function into their own module
Diffstat (limited to 'ick2/controllerapi.py')
-rw-r--r-- | ick2/controllerapi.py | 65 |
1 files changed, 9 insertions, 56 deletions
diff --git a/ick2/controllerapi.py b/ick2/controllerapi.py index ef20daa..6d00b9e 100644 --- a/ick2/controllerapi.py +++ b/ick2/controllerapi.py @@ -13,9 +13,6 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. -import apifw - - import ick2 @@ -93,11 +90,11 @@ class APIbase: # pragma: no cover del kwargs['raw_uri_path'] body = callback(**kwargs) except ick2.NotFound as e: - return not_found(e) + return ick2.not_found(e) if isinstance(body, dict): - return OK(body) + return ick2.OK(body) elif isinstance(body, str): - return text_plain(body) + return ick2.text_plain(body) raise Exception('this must not happen') return wrapper @@ -108,7 +105,7 @@ class APIbase: # pragma: no cover content_type=content_type, body=body) body = callback(body) ick2.log.log('trace', msg_text='returned body', body=repr(body)) - return created(body) + return ick2.created(body) return wrapper def PUT(self, callback): @@ -121,15 +118,15 @@ class APIbase: # pragma: no cover try: body = callback(body, **kwargs) except ick2.NotFound as e: - return not_found(e) + return ick2.not_found(e) except ick2.WrongPipelineStatus as e: ick2.log.log( 'error', msg_text='Wrong state for pipeline', exception=str(e)) - return bad_request(e) + return ick2.bad_request(e) ick2.log.log('trace', msg_text='returned body', body=repr(body)) - return OK(body) + return ick2.OK(body) return wrapper def DELETE(self, callback): @@ -142,8 +139,8 @@ class APIbase: # pragma: no cover del kwargs['raw_uri_path'] body = callback(**kwargs) except ick2.NotFound as e: - return not_found(e) - return OK(body) + return ick2.not_found(e) + return ick2.OK(body) return wrapper def create(self, body): @@ -557,47 +554,3 @@ class MethodNotAllowed(Exception): # pragma: no cover def __init__(self, wat): super().__init__(wat) - - -def response(status_code, body, headers): # pragma: no cover - obj = { - 'status': status_code, - 'body': body, - 'headers': headers, - } - return apifw.Response(obj) - - -def OK(body): # pragma: no cover - headers = { - 'Content-Type': 'application/json', - } - return response(apifw.HTTP_OK, body, headers) - - -def text_plain(body): # pragma: no cover - headers = { - 'Content-Type': 'text/plain', - } - return response(apifw.HTTP_OK, body, headers) - - -def not_found(error): # pragma: no cover - headers = { - 'Content-Type': 'text/plain', - } - return response(apifw.HTTP_NOT_FOUND, str(error), headers) - - -def bad_request(error): # pragma: no cover - headers = { - 'Content-Type': 'text/plain', - } - return response(apifw.HTTP_BAD_REQUEST, str(error), headers) - - -def created(body): # pragma: no cover - headers = { - 'Content-Type': 'application/json', - } - return response(apifw.HTTP_CREATED, body, headers) |