From 18753714a35b6c8e5af4b6076973f490a1ba2097 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 18 Nov 2017 19:59:56 +0100 Subject: Refactor: move HTTP response function into their own module --- ick2/controllerapi.py | 65 +++++++-------------------------------------------- 1 file changed, 9 insertions(+), 56 deletions(-) (limited to 'ick2/controllerapi.py') 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 . -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) -- cgit v1.2.1