summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2017-11-18 19:59:56 +0100
committerLars Wirzenius <liw@liw.fi>2017-11-18 19:59:56 +0100
commit18753714a35b6c8e5af4b6076973f490a1ba2097 (patch)
tree15be406183e1b505fb312a954af20531ac825cd3
parent1c9c77331874c8932f221c454346568b23c25024 (diff)
downloadick2-18753714a35b6c8e5af4b6076973f490a1ba2097.tar.gz
Refactor: move HTTP response function into their own module
-rw-r--r--ick2/__init__.py7
-rw-r--r--ick2/controllerapi.py65
-rw-r--r--ick2/responses.py60
-rw-r--r--without-tests1
4 files changed, 77 insertions, 56 deletions
diff --git a/ick2/__init__.py b/ick2/__init__.py
index 02a8e76..d92de41 100644
--- a/ick2/__init__.py
+++ b/ick2/__init__.py
@@ -16,6 +16,13 @@
from .version import __version__, __version_info__
from .logging import setup_logging, log
from .state import ControllerState, NotFound, WrongPipelineStatus
+from .responses import (
+ OK,
+ bad_request,
+ created,
+ not_found,
+ text_plain,
+)
from .controllerapi import (
ControllerAPI,
ProjectAPI,
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)
diff --git a/ick2/responses.py b/ick2/responses.py
new file mode 100644
index 0000000..ef68783
--- /dev/null
+++ b/ick2/responses.py
@@ -0,0 +1,60 @@
+# Copyright (C) 2017 Lars Wirzenius
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+import apifw
+
+
+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)
diff --git a/without-tests b/without-tests
index 5af6235..f180f97 100644
--- a/without-tests
+++ b/without-tests
@@ -1,5 +1,6 @@
ick2/__init__.py
ick2/logging.py
+ick2/responses.py
ick2/version.py