Browse Source

clean example_a_aiohttp.py

Bastien Sevajol 6 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
 # coding: utf-8
1
 # coding: utf-8
2
-import json
3
-import yaml
4
-
5
 from aiohttp import web
2
 from aiohttp import web
6
 from hapic import async as hapic
3
 from hapic import async as hapic
7
 from hapic import async as HapicData
4
 from hapic import async as HapicData
10
 from hapic.ext.aiohttp.context import AiohttpContext
7
 from hapic.ext.aiohttp.context import AiohttpContext
11
 
8
 
12
 
9
 
13
-class HandleInputPath(marshmallow.Schema):
10
+class DisplayNameInputPathSchema(marshmallow.Schema):
14
     name = marshmallow.fields.String(
11
     name = marshmallow.fields.String(
15
         required=False,
12
         required=False,
16
         allow_none=True,
13
         allow_none=True,
17
     )
14
     )
18
 
15
 
19
 
16
 
20
-class HandleInputBody(marshmallow.Schema):
17
+class DisplayBodyInputBodySchema(marshmallow.Schema):
21
     foo = marshmallow.fields.String(
18
     foo = marshmallow.fields.String(
22
         required=True,
19
         required=True,
23
     )
20
     )
24
 
21
 
25
 
22
 
26
-class Handle2OutputBody(marshmallow.Schema):
23
+class DisplayBodyOutputBodySchema(marshmallow.Schema):
27
     data = marshmallow.fields.Dict(
24
     data = marshmallow.fields.Dict(
28
         required=True,
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
 @hapic.with_api_doc()
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
     name = request.match_info.get('name', "Anonymous")
32
     name = request.match_info.get('name', "Anonymous")
43
     text = "Hello, " + name
33
     text = "Hello, " + name
44
     return web.json_response({
34
     return web.json_response({
47
 
37
 
48
 
38
 
49
 @hapic.with_api_doc()
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
     data = hapic_data.body
43
     data = hapic_data.body
54
     return {
44
     return {
55
         'data': data,
45
         'data': data,
67
 
57
 
68
 app = web.Application(debug=True)
58
 app = web.Application(debug=True)
69
 app.add_routes([
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
     web.post('/login', do_login),
64
     web.post('/login', do_login),
75
 ])
65
 ])
76
 
66
 
77
 
67
 
78
 hapic.set_context(AiohttpContext(app))
68
 hapic.set_context(AiohttpContext(app))
79
 
69
 
70
+
71
+# import json
72
+# import yaml
80
 # print(yaml.dump(
73
 # print(yaml.dump(
81
 #     json.loads(json.dumps(hapic.generate_doc())),
74
 #     json.loads(json.dumps(hapic.generate_doc())),
82
 #     default_flow_style=False,
75
 #     default_flow_style=False,