diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-11-18 20:24:11 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-11-18 20:24:35 +0100 |
commit | ceae946c47b4ce34d9d66652b94df66d82fe8c69 (patch) | |
tree | 2ff5d9e9517f1b886573302ce4f6099139686248 /ick2/controllerapi.py | |
parent | b0c6bce3a8df1ea730a535cd2e3dd8be7ff4422e (diff) | |
download | ick2-ceae946c47b4ce34d9d66652b94df66d82fe8c69.tar.gz |
Refactor: move APIbase into its own module
Diffstat (limited to 'ick2/controllerapi.py')
-rw-r--r-- | ick2/controllerapi.py | 119 |
1 files changed, 3 insertions, 116 deletions
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) |