# coding=utf-8 from apistar import Include, Route, ASyncApp async def hello() -> str: return "Hello World!" routes = [ Route('/', 'GET', hello), ] app = ASyncApp(routes=routes) if __name__ == '__main__': app.serve('127.0.0.1', 8888, debug=True)
# raise exceptions.BadRequest() if not data['title']: raise exceptions.BadRequest() try: repo.update_article(article_id, data['title']) except LookupError: raise exceptions.NotFound() async def delete_article(article_id: int, password: http.Header): # if not is_auth(password): # raise exceptions.BadRequest() try: repo.delete_article(article_id) except LookupError: raise exceptions.NotFound() return http.Response('', status_code=204) routes = [ Route('/articles', method='GET', handler=list_articles), Route('/articles/{article_id}', method='GET', handler=get_article), Route('/articles', method='POST', handler=create_article), Route('/articles/{article_id}', method='POST', handler=update_article), Route('/articles/{article_id}', method='DELETE', handler=delete_article), ] app = ASyncApp(routes=routes) if __name__ == '__main__': app.serve('127.0.0.1', 8000, debug=False, schema_url=None)
from apistar import ASyncApp from apistar import Route from apistar import http from apistar import Include from controller import users def welcome(name=None): if name is None: return {'message': 'Welcome to API Star!'} return {'message': 'Welcome to API Star, %s!' % name} routes = [ Route('/', method='GET', handler=welcome), Include('/users', name='users', routes=users.routes), ] print(routes) app = ASyncApp(routes=routes) if __name__ == '__main__': app.serve('127.0.0.1', 5000, debug=True)
def jsonify(records): """ Parse asyncpg record response into JSON format """ list_return = [] list_keys = ['salary', 'address', 'age', 'id', 'name'] for r in records: itens = [i for i in r] itens = zip(list_keys, itens) list_return.append({ i[0]: i[1].rstrip() if type(i[1]) == str else i[1] for i in itens }) return list_return routes = [ Route('/db2', method='GET', handler=db2), ] app = ASyncApp(routes=routes) log = logging.getLogger('werkzeug') log.setLevel(logging.ERROR) if __name__ == '__main__': app.serve('127.0.0.1', 5000, use_debugger=False, use_reloader=False)