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/controllerapi.py | 119 ++------------------------------------------------ 1 file changed, 3 insertions(+), 116 deletions(-) (limited to 'ick2/controllerapi.py') 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': '{}/'.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() - - -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) -- cgit v1.2.1