summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2017-11-18 20:24:11 +0100
committerLars Wirzenius <liw@liw.fi>2017-11-18 20:24:35 +0100
commitceae946c47b4ce34d9d66652b94df66d82fe8c69 (patch)
tree2ff5d9e9517f1b886573302ce4f6099139686248
parentb0c6bce3a8df1ea730a535cd2e3dd8be7ff4422e (diff)
downloadick2-ceae946c47b4ce34d9d66652b94df66d82fe8c69.tar.gz
Refactor: move APIbase into its own module
-rw-r--r--ick2/__init__.py1
-rw-r--r--ick2/apibase.py129
-rw-r--r--ick2/controllerapi.py119
-rw-r--r--without-tests1
4 files changed, 134 insertions, 116 deletions
diff --git a/ick2/__init__.py b/ick2/__init__.py
index 1145ca6..002ac3b 100644
--- a/ick2/__init__.py
+++ b/ick2/__init__.py
@@ -28,6 +28,7 @@ from .responses import (
not_found,
text_plain,
)
+from .apibase import APIbase
from .controllerapi import (
ControllerAPI,
ProjectAPI,
diff --git a/ick2/apibase.py b/ick2/apibase.py
new file mode 100644
index 0000000..36aad3b
--- /dev/null
+++ b/ick2/apibase.py
@@ -0,0 +1,129 @@
+# 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 ick2
+
+
+class APIbase:
+
+ def __init__(self, state):
+ self._state = state
+
+ def get_routes(self, path):
+ return [
+ {
+ 'method': 'POST',
+ 'path': path,
+ 'callback': self.POST(self.create),
+ },
+ {
+ 'method': 'GET',
+ 'path': path,
+ 'callback': self.GET(self.list),
+ },
+ {
+ 'method': 'GET',
+ 'path': '{}/<name>'.format(path),
+ 'callback': self.GET(self.show),
+ },
+ {
+ 'method': 'PUT',
+ 'path': '{}/<name>'.format(path),
+ 'callback': self.PUT(self.update),
+ },
+ {
+ 'method': 'DELETE',
+ 'path': '{}/<name>'.format(path),
+ 'callback': self.DELETE(self.delete),
+ },
+ ]
+
+ def GET(self, callback):
+ def wrapper(content_type, body, **kwargs):
+ ick2.log.log(
+ 'trace', msg_text='GET called', kwargs=kwargs,
+ content_type=content_type, body=body)
+ try:
+ if 'raw_uri_path' in kwargs:
+ del kwargs['raw_uri_path']
+ body = callback(**kwargs)
+ except ick2.NotFound as e:
+ return ick2.not_found(e)
+ if isinstance(body, dict):
+ return ick2.OK(body)
+ elif isinstance(body, str):
+ return ick2.text_plain(body)
+ raise Exception('this must not happen')
+ return wrapper
+
+ def POST(self, callback):
+ def wrapper(content_type, body, **kwargs):
+ ick2.log.log(
+ 'trace', msg_text='POST called', kwargs=kwargs,
+ content_type=content_type, body=body)
+ body = callback(body)
+ ick2.log.log('trace', msg_text='returned body', body=repr(body))
+ return ick2.created(body)
+ return wrapper
+
+ def PUT(self, callback):
+ def wrapper(content_type, body, **kwargs):
+ ick2.log.log(
+ 'trace', msg_text='PUT called', kwargs=kwargs,
+ content_type=content_type, body=body)
+ if 'raw_uri_path' in kwargs:
+ del kwargs['raw_uri_path']
+ try:
+ body = callback(body, **kwargs)
+ except ick2.NotFound as 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 ick2.bad_request(e)
+ ick2.log.log('trace', msg_text='returned body', body=repr(body))
+ return ick2.OK(body)
+ return wrapper
+
+ def DELETE(self, callback):
+ def wrapper(content_type, body, **kwargs):
+ ick2.log.log(
+ 'trace', msg_text='DELETE called', kwargs=kwargs,
+ content_type=content_type, body=body)
+ try:
+ if 'raw_uri_path' in kwargs:
+ del kwargs['raw_uri_path']
+ body = callback(**kwargs)
+ except ick2.NotFound as e:
+ return ick2.not_found(e)
+ return ick2.OK(body)
+ return wrapper
+
+ def create(self, body):
+ raise NotImplementedError()
+
+ def update(self, body, name):
+ raise NotImplementedError()
+
+ def delete(self, name):
+ raise NotImplementedError()
+
+ def list(self):
+ raise NotImplementedError()
+
+ def show(self, name):
+ raise NotImplementedError()
diff --git a/ick2/controllerapi.py b/ick2/controllerapi.py
index 6cbf389..1c482fe 100644
--- a/ick2/controllerapi.py
+++ b/ick2/controllerapi.py
@@ -46,120 +46,7 @@ class ControllerAPI:
return routes
-class APIbase: # pragma: no cover
-
- def __init__(self, state):
- self._state = state
-
- def get_routes(self, path):
- return [
- {
- 'method': 'POST',
- 'path': path,
- 'callback': self.POST(self.create),
- },
- {
- 'method': 'GET',
- 'path': path,
- 'callback': self.GET(self.list),
- },
- {
- 'method': 'GET',
- 'path': '{}/<name>'.format(path),
- 'callback': self.GET(self.show),
- },
- {
- 'method': 'PUT',
- 'path': '{}/<name>'.format(path),
- 'callback': self.PUT(self.update),
- },
- {
- 'method': 'DELETE',
- 'path': '{}/<name>'.format(path),
- 'callback': self.DELETE(self.delete),
- },
- ]
-
- def GET(self, callback):
- def wrapper(content_type, body, **kwargs):
- ick2.log.log(
- 'trace', msg_text='GET called', kwargs=kwargs,
- content_type=content_type, body=body)
- try:
- if 'raw_uri_path' in kwargs:
- del kwargs['raw_uri_path']
- body = callback(**kwargs)
- except ick2.NotFound as e:
- return ick2.not_found(e)
- if isinstance(body, dict):
- return ick2.OK(body)
- elif isinstance(body, str):
- return ick2.text_plain(body)
- raise Exception('this must not happen')
- return wrapper
-
- def POST(self, callback):
- def wrapper(content_type, body, **kwargs):
- ick2.log.log(
- 'trace', msg_text='POST called', kwargs=kwargs,
- content_type=content_type, body=body)
- body = callback(body)
- ick2.log.log('trace', msg_text='returned body', body=repr(body))
- return ick2.created(body)
- return wrapper
-
- def PUT(self, callback):
- def wrapper(content_type, body, **kwargs):
- ick2.log.log(
- 'trace', msg_text='PUT called', kwargs=kwargs,
- content_type=content_type, body=body)
- if 'raw_uri_path' in kwargs:
- del kwargs['raw_uri_path']
- try:
- body = callback(body, **kwargs)
- except ick2.NotFound as 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 ick2.bad_request(e)
- ick2.log.log('trace', msg_text='returned body', body=repr(body))
- return ick2.OK(body)
- return wrapper
-
- def DELETE(self, callback):
- def wrapper(content_type, body, **kwargs):
- ick2.log.log(
- 'trace', msg_text='DELETE called', kwargs=kwargs,
- content_type=content_type, body=body)
- try:
- if 'raw_uri_path' in kwargs:
- del kwargs['raw_uri_path']
- body = callback(**kwargs)
- except ick2.NotFound as e:
- return ick2.not_found(e)
- return ick2.OK(body)
- return wrapper
-
- def create(self, body):
- raise NotImplementedError()
-
- def update(self, body, name):
- raise NotImplementedError()
-
- def delete(self, name):
- raise NotImplementedError()
-
- def list(self):
- raise NotImplementedError()
-
- def show(self, name):
- raise NotImplementedError()
-
-
-class VersionAPI(APIbase):
+class VersionAPI(ick2.APIbase):
def __init__(self, state):
super().__init__(state)
@@ -192,7 +79,7 @@ class VersionAPI(APIbase):
pass
-class ResourceApiBase(APIbase):
+class ResourceApiBase(ick2.APIbase):
def __init__(self, type_name, state):
super().__init__(state)
@@ -341,7 +228,7 @@ class ProjectAPI(ResourceApiBase):
}
-class WorkAPI(APIbase):
+class WorkAPI(ick2.APIbase):
def __init__(self, state):
super().__init__(state)
diff --git a/without-tests b/without-tests
index b455c2b..cc60aae 100644
--- a/without-tests
+++ b/without-tests
@@ -1,4 +1,5 @@
ick2/__init__.py
+ick2/apibase.py
ick2/exceptions.py
ick2/logging.py
ick2/responses.py