You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cookie-api/app.py

53 lines
1.4 KiB
Python

import json
from apistar import Include, Route, http, Response, annotate
from apistar.frameworks.wsgi import WSGIApp as App
from apistar.handlers import docs_urls, static_urls
from apistar.backends import sqlalchemy_backend
from apistar.backends.sqlalchemy_backend import Session
from models import Base, Cookie
from model.util import alchemyencoder
from render import JSONRenderer
def welcome(name=None):
if name is None:
return {'message': 'Welcome to API Star!'}
return {'message': 'Welcome to API Star, %s!' % name}
@annotate(renderers=[JSONRenderer()])
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.unit_cost}
for cookie in cookies]
return result
routes = [
Route('/', 'GET', welcome),
Route('/cookies', 'GET', get_cookies),
Include('/docs', docs_urls),
Include('/static', static_urls)
]
settings = {
'DATABASE': {
'URL': 'postgresql://:@localhost/apistar',
'METADATA': Base.metadata
}
}
app = App(routes=routes,
settings=settings,
commands=sqlalchemy_backend.commands,
components=sqlalchemy_backend.components
)