Skip to content

Commit edb5222

Browse files
committed
WIP: draft for issue #24 about rest api demo
1 parent c3cd949 commit edb5222

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

demos/rest/main.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from aiohttp import web
2+
from aiohttp_swagger import setup_swagger
3+
import aiosqlite
4+
5+
DB_NAME = 'sqlite.db'
6+
7+
8+
class MyView(web.View):
9+
"""
10+
---
11+
description: This is test
12+
tags:
13+
- Index url
14+
produces:
15+
- text/plain
16+
responses:
17+
"200":
18+
description: successful operation
19+
"""
20+
21+
async def get(self):
22+
resp = []
23+
async with aiosqlite.connect(DB_NAME) as db:
24+
async with db.execute("SELECT 42;") as cursor:
25+
async for row in cursor:
26+
resp.append(row)
27+
28+
return web.json_response({'data': resp})
29+
30+
31+
async def post(self):
32+
async with aiosqlite.connect(DB_NAME) as db:
33+
await db.execute("INSERT INTO some_table VALUES (43)")
34+
await db.commit()
35+
36+
return web.json_response({'status_code': 201})
37+
38+
39+
40+
async def init_app():
41+
42+
app = web.Application()
43+
44+
app.add_routes([
45+
web.get('/', MyView),
46+
web.post('/', MyView)
47+
])
48+
49+
setup_swagger(app)
50+
51+
return app
52+
53+
54+
def main():
55+
app = init_app()
56+
web.run_app(app)
57+
58+
59+
if __name__ == '__main__':
60+
main()

0 commit comments

Comments
 (0)