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 )