From b048fe125430e4a1e93e1266a16afe438fd49bfc Mon Sep 17 00:00:00 2001 From: androiddrew Date: Tue, 8 May 2018 18:27:33 -0400 Subject: [PATCH] Refactored resources out to own submodule --- cookie_api/app.py | 63 +++----------------------------- cookie_api/resources/__init__.py | 1 + cookie_api/resources/cookies.py | 57 +++++++++++++++++++++++++++++ tests/test_cookies.py | 2 +- 4 files changed, 64 insertions(+), 59 deletions(-) create mode 100644 cookie_api/resources/__init__.py create mode 100644 cookie_api/resources/cookies.py diff --git a/cookie_api/app.py b/cookie_api/app.py index a47434f..bb36fc5 100644 --- a/cookie_api/app.py +++ b/cookie_api/app.py @@ -1,73 +1,20 @@ -import typing - -from apistar import Route, http -from apistar_jwt import JWT, authentication_required, JWTUser - -import logbook +from apistar_jwt import JWT from sqlalchemy import create_engine -from sqlalchemy.orm import Session from cookie_api.auth import auth_routes from cookie_api.logger import global_init -from cookie_api.models import Cookie -from cookie_api.schema import CookieSchema -from cookie_api.util import SQLAlchemyHook, SQLAlchemySession, ExtJSONResponse, MetaApp as App +from cookie_api.resources import cookie_routes +from cookie_api.util import SQLAlchemyHook, SQLAlchemySession, MetaApp as App engine = create_engine('postgresql://apistar@localhost:5432/apistar') -logger = logbook.Logger('Cookies') - - -def get_cookies(session: Session) -> typing.List[CookieSchema]: - cookies = session.query(Cookie).all() - return [CookieSchema(cookie) for cookie in cookies] - - -def get_cookie(session: Session, id) -> CookieSchema: - cookie = session.query(Cookie).filter_by(id=id).one_or_none() - if cookie is None: - msg = {"error": "404 Not Found"} - return ExtJSONResponse(msg, 404) - return ExtJSONResponse(CookieSchema(cookie), 200) - - -@authentication_required -def create_cookie(session: Session, cookie_data: CookieSchema, app: App, user: JWTUser): - cookie = Cookie(**cookie_data) - session.add(cookie) - session.commit() - headers = {'Location': app.reverse_url('get_cookie', id=cookie.id)} - return ExtJSONResponse(CookieSchema(cookie), 201, headers=headers) - - -@authentication_required -def delete_cookie(session: Session, id: int, user: JWTUser): - cookie = session.query(Cookie).filter_by(id=id).one_or_none() - if cookie is None: - msg = {"error": "404 Not Found"} - return http.Response(msg, status=404) - - logger.debug("Deleting cookie {} {}".format(cookie.id, cookie.name)) - session.delete(cookie) - session.commit() - - return ExtJSONResponse({}, 204) - - -_routes = [ - Route('/cookies', 'GET', get_cookies), - Route('/cookies', 'POST', create_cookie), - Route('/cookies/{id}', 'GET', get_cookie), - Route('/cookies/{id}', 'DELETE', delete_cookie) -] - app_settings = { "LOGGING": { "LEVEL": "DEBUG" } } -_routes = _routes + auth_routes +_routes = cookie_routes + auth_routes _hooks = [SQLAlchemyHook] @@ -80,7 +27,7 @@ _components = [ ] -def application_factory(routes=_routes, components=_components, hooks=_hooks, settings={},): +def application_factory(routes=_routes, components=_components, hooks=_hooks, settings={}, ): """Returns an instance of Cookie API""" _settings = {**app_settings, **settings} diff --git a/cookie_api/resources/__init__.py b/cookie_api/resources/__init__.py new file mode 100644 index 0000000..2f81617 --- /dev/null +++ b/cookie_api/resources/__init__.py @@ -0,0 +1 @@ +from .cookies import cookie_routes \ No newline at end of file diff --git a/cookie_api/resources/cookies.py b/cookie_api/resources/cookies.py new file mode 100644 index 0000000..cde9c68 --- /dev/null +++ b/cookie_api/resources/cookies.py @@ -0,0 +1,57 @@ +import typing + +import logbook +from apistar import http, Route, App +from apistar_jwt import authentication_required, JWTUser +from sqlalchemy.orm import Session + +from cookie_api.models import Cookie +from cookie_api.schema import CookieSchema +from cookie_api.util import MetaJSONResponse + +logger = logbook.Logger(__name__) + + +def get_cookies(session: Session) -> typing.List[CookieSchema]: + cookies = session.query(Cookie).all() + logger.debug('Cookies collection hit') + return [CookieSchema(cookie) for cookie in cookies] + + +def get_cookie(session: Session, id) -> CookieSchema: + cookie = session.query(Cookie).filter_by(id=id).one_or_none() + if cookie is None: + msg = {"error": "404 Not Found"} + return MetaJSONResponse(msg, 404) + return MetaJSONResponse(CookieSchema(cookie)) + + +@authentication_required +def create_cookie(session: Session, cookie_data: CookieSchema, app: App, user: JWTUser): + cookie = Cookie(**cookie_data) + session.add(cookie) + session.commit() + headers = {'Location': app.reverse_url('get_cookie', id=cookie.id)} + return MetaJSONResponse(CookieSchema(cookie), 201, headers) + + +@authentication_required +def delete_cookie(session: Session, id: int, user: JWTUser): + cookie = session.query(Cookie).filter_by(id=id).one_or_none() + if cookie is None: + msg = {"error": "404 Not Found"} + return MetaJSONResponse(msg, 204) + + logger.debug("Deleting cookie {} {}".format(cookie.id, cookie.name)) + session.delete(cookie) + session.commit() + + return MetaJSONResponse({}, 204) + + +cookie_routes = [ + Route('/cookies', 'GET', get_cookies), + Route('/cookies', 'POST', create_cookie), + Route('/cookies/{id}', 'GET', get_cookie), + Route('/cookies/{id}', 'DELETE', delete_cookie) +] diff --git a/tests/test_cookies.py b/tests/test_cookies.py index 615328d..0d87da6 100644 --- a/tests/test_cookies.py +++ b/tests/test_cookies.py @@ -5,7 +5,7 @@ from apistar import TestClient from cookie_api.models import Cookie -from cookie_api.app import get_cookies, get_cookie +from cookie_api.resources.cookies import get_cookies, get_cookie from cookie_api.util import ExtJSONResponse