|
|
|
from apistar import Include, Route, http, Command
|
|
|
|
from apistar.backends import sqlalchemy_backend
|
|
|
|
from apistar.backends.sqlalchemy_backend import Session
|
|
|
|
from apistar.frameworks.wsgi import WSGIApp as App
|
|
|
|
from apistar.handlers import docs_urls, static_urls
|
|
|
|
from apistar.interfaces import Router, Injector
|
|
|
|
|
|
|
|
from cookie_api.render import JSONRenderer
|
|
|
|
from cookie_api.commands import commands
|
|
|
|
from cookie_api.models.schema import Base, Cookie
|
|
|
|
|
|
|
|
from .auth import auth_routes, auth_components
|
|
|
|
|
|
|
|
|
|
|
|
def get_state(injector: Injector):
|
|
|
|
state = injector.state
|
|
|
|
d = dict()
|
|
|
|
for k, v in state.items():
|
|
|
|
d[k] = str(v)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def get_cookies(session: Session):
|
|
|
|
cookies = session.query(Cookie).all()
|
|
|
|
# result = [{"id": cookie.id,
|
|
|
|
# "created_date": cookie.created_date,
|
|
|
|
# "modified_date": cookie.modified_date,
|
|
|
|
# "name": cookie.name,
|
|
|
|
# "recipe_url": cookie.recipe_url,
|
|
|
|
# "sku": cookie.sku,
|
|
|
|
# "qoh": cookie.qoh,
|
|
|
|
# "unit_cost": cookie.unit_cost}
|
|
|
|
# for cookie in cookies]
|
|
|
|
result = [cookie.to_dict() for cookie in cookies]
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def get_cookie(session: Session, id):
|
|
|
|
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)
|
|
|
|
return cookie.to_dict()
|
|
|
|
|
|
|
|
|
|
|
|
# @annotate(authentication=[JWTAuthentication()])
|
|
|
|
def create_cookie(session: Session, json_data: http.RequestData, route: Router):
|
|
|
|
cookie = Cookie(name=json_data['name'],
|
|
|
|
recipe_url=json_data['recipe_url'],
|
|
|
|
sku=json_data['sku'],
|
|
|
|
qoh=json_data['qoh'],
|
|
|
|
unit_cost=json_data['unit_cost'])
|
|
|
|
session.add(cookie)
|
|
|
|
session.commit()
|
|
|
|
headers = {'Location': route.reverse_url('get_cookie', dict(id=cookie.id))}
|
|
|
|
return http.Response(cookie.to_dict(), status=201, headers=headers)
|
|
|
|
|
|
|
|
|
|
|
|
routes = [
|
|
|
|
Route('/state', 'GET', get_state),
|
|
|
|
Route('/cookies', 'GET', get_cookies),
|
|
|
|
Route('/cookies', 'POST', create_cookie),
|
|
|
|
Route('/cookies/{id}', 'GET', get_cookie),
|
|
|
|
Include('/docs', docs_urls),
|
|
|
|
Include('/static', static_urls)
|
|
|
|
]
|
|
|
|
|
|
|
|
settings = {
|
|
|
|
'DATABASE': {
|
|
|
|
'URL': 'postgresql://apistar:local@localhost/apistar',
|
|
|
|
'METADATA': Base.metadata
|
|
|
|
},
|
|
|
|
'RENDERERS': [JSONRenderer()],
|
|
|
|
'JWT': {
|
|
|
|
'SECRET': 'thisisasecret'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commands = sqlalchemy_backend.commands + commands
|
|
|
|
|
|
|
|
app = App(routes=routes + auth_routes,
|
|
|
|
settings=settings,
|
|
|
|
commands=commands,
|
|
|
|
components=sqlalchemy_backend.components + auth_components
|
|
|
|
)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.main()
|