From ceae946c47b4ce34d9d66652b94df66d82fe8c69 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 18 Nov 2017 20:24:11 +0100 Subject: Refactor: move APIbase into its own module --- ick2/apibase.py | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 ick2/apibase.py (limited to 'ick2/apibase.py') 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 . + + +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': '{}/'.format(path), + 'callback': self.GET(self.show), + }, + { + 'method': 'PUT', + 'path': '{}/'.format(path), + 'callback': self.PUT(self.update), + }, + { + 'method': 'DELETE', + 'path': '{}/'.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() -- cgit v1.2.1