Browse Source

clean example_a_aiohttp.py

Bastien Sevajol 5 years ago
parent
commit
a74afa1297
1 changed files with 15 additions and 22 deletions
  1. 15 22
      example/example_a_aiohttp.py

+ 15 - 22
example/example_a_aiohttp.py View File

@@ -1,7 +1,4 @@
1 1
 # coding: utf-8
2
-import json
3
-import yaml
4
-
5 2
 from aiohttp import web
6 3
 from hapic import async as hapic
7 4
 from hapic import async as HapicData
@@ -10,35 +7,28 @@ import marshmallow
10 7
 from hapic.ext.aiohttp.context import AiohttpContext
11 8
 
12 9
 
13
-class HandleInputPath(marshmallow.Schema):
10
+class DisplayNameInputPathSchema(marshmallow.Schema):
14 11
     name = marshmallow.fields.String(
15 12
         required=False,
16 13
         allow_none=True,
17 14
     )
18 15
 
19 16
 
20
-class HandleInputBody(marshmallow.Schema):
17
+class DisplayBodyInputBodySchema(marshmallow.Schema):
21 18
     foo = marshmallow.fields.String(
22 19
         required=True,
23 20
     )
24 21
 
25 22
 
26
-class Handle2OutputBody(marshmallow.Schema):
23
+class DisplayBodyOutputBodySchema(marshmallow.Schema):
27 24
     data = marshmallow.fields.Dict(
28 25
         required=True,
29 26
     )
30 27
 
31 28
 
32
-class HandleOutputBody(marshmallow.Schema):
33
-    sentence = marshmallow.fields.String(
34
-        required=True,
35
-    )
36
-
37
-
38 29
 @hapic.with_api_doc()
39
-@hapic.input_path(HandleInputPath())
40
-# @hapic.output_body(HandleOutputBody())
41
-async def handle(request, hapic_data):
30
+@hapic.input_path(DisplayNameInputPathSchema())
31
+async def display_name(request, hapic_data):
42 32
     name = request.match_info.get('name', "Anonymous")
43 33
     text = "Hello, " + name
44 34
     return web.json_response({
@@ -47,9 +37,9 @@ async def handle(request, hapic_data):
47 37
 
48 38
 
49 39
 @hapic.with_api_doc()
50
-@hapic.input_body(HandleInputBody())
51
-@hapic.output_body(Handle2OutputBody())
52
-async def handle2(request, hapic_data: HapicData):
40
+@hapic.input_body(DisplayBodyInputBodySchema())
41
+@hapic.output_body(DisplayBodyOutputBodySchema())
42
+async def display_body(request, hapic_data: HapicData):
53 43
     data = hapic_data.body
54 44
     return {
55 45
         'data': data,
@@ -67,16 +57,19 @@ async def do_login(request):
67 57
 
68 58
 app = web.Application(debug=True)
69 59
 app.add_routes([
70
-    web.get('/n/', handle),
71
-    web.get('/n/{name}', handle),
72
-    web.post('/n/{name}', handle),
73
-    web.post('/b/', handle2),
60
+    web.get('/n/', display_name),
61
+    web.get('/n/{name}', display_name),
62
+    web.post('/n/{name}', display_name),
63
+    web.post('/b/', display_body),
74 64
     web.post('/login', do_login),
75 65
 ])
76 66
 
77 67
 
78 68
 hapic.set_context(AiohttpContext(app))
79 69
 
70
+
71
+# import json
72
+# import yaml
80 73
 # print(yaml.dump(
81 74
 #     json.loads(json.dumps(hapic.generate_doc())),
82 75
 #     default_flow_style=False,