Refactored resources out to own submodule
parent
bd818f1cf2
commit
b048fe1254
@ -0,0 +1 @@
|
|||||||
|
from .cookies import cookie_routes
|
@ -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)
|
||||||
|
]
|
Loading…
Reference in New Issue