summaryrefslogtreecommitdiff
path: root/ick2/apibase.py
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 /ick2/apibase.py
parentb0c6bce3a8df1ea730a535cd2e3dd8be7ff4422e (diff)
downloadick2-ceae946c47b4ce34d9d66652b94df66d82fe8c69.tar.gz
Refactor: move APIbase into its own module
Diffstat (limited to 'ick2/apibase.py')
-rw-r--r--ick2/apibase.py129
1 files changed, 129 insertions, 0 deletions
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()