Skip to content

Commit

Permalink
WIP: draft for issue #24 about rest api demo
Browse files Browse the repository at this point in the history
  • Loading branch information
gyermolenko committed May 6, 2018
1 parent c3cd949 commit edb5222
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions demos/rest/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from aiohttp import web
from aiohttp_swagger import setup_swagger
import aiosqlite

DB_NAME = 'sqlite.db'


class MyView(web.View):
"""
---
description: This is test
tags:
- Index url
produces:
- text/plain
responses:
"200":
description: successful operation
"""

async def get(self):
resp = []
async with aiosqlite.connect(DB_NAME) as db:
async with db.execute("SELECT 42;") as cursor:
async for row in cursor:
resp.append(row)

return web.json_response({'data': resp})


async def post(self):
async with aiosqlite.connect(DB_NAME) as db:
await db.execute("INSERT INTO some_table VALUES (43)")
await db.commit()

return web.json_response({'status_code': 201})



async def init_app():

app = web.Application()

app.add_routes([
web.get('/', MyView),
web.post('/', MyView)
])

setup_swagger(app)

return app


def main():
app = init_app()
web.run_app(app)


if __name__ == '__main__':
main()

0 comments on commit edb5222

Please sign in to comment.