forked from spec-first/connexion
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaiohttp_handlers.py
More file actions
executable file
·149 lines (96 loc) · 3.59 KB
/
aiohttp_handlers.py
File metadata and controls
executable file
·149 lines (96 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
import datetime
import uuid
from connexion.lifecycle import ConnexionResponse
import aiohttp
from aiohttp.web import Request
from aiohttp.web import Response as AioHttpResponse
async def get_bye(name):
return AioHttpResponse(text=f'Goodbye {name}')
async def aiohttp_str_response():
return 'str response'
async def aiohttp_non_str_non_json_response():
return 1234
async def aiohttp_bytes_response():
return b'bytes response'
async def aiohttp_validate_responses():
return {"validate": True}
async def aiohttp_post_greeting(name, **kwargs):
data = {'greeting': f'Hello {name}'}
return data
async def aiohttp_echo(**kwargs):
return aiohttp.web.json_response(data=kwargs, status=200)
async def aiohttp_access_request_context(request_ctx):
assert request_ctx is not None
assert isinstance(request_ctx, aiohttp.web.Request)
return None
async def aiohttp_query_parsing_str(query):
return {'query': query}
async def aiohttp_query_parsing_array(query):
return {'query': query}
async def aiohttp_query_parsing_array_multi(query):
return {'query': query}
USERS = [
{"id": 1, "name": "John Doe"},
{"id": 2, "name": "Nick Carlson"}
]
async def aiohttp_users_get(*args):
return aiohttp.web.json_response(data=USERS, status=200)
async def aiohttp_users_post(user):
if "name" not in user:
return ConnexionResponse(body={"error": "name is undefined"},
status_code=400,
content_type='application/json')
user['id'] = len(USERS) + 1
USERS.append(user)
return aiohttp.web.json_response(data=USERS[-1], status=201)
async def aiohttp_token_info(token_info):
return aiohttp.web.json_response(data=token_info)
async def aiohttp_all_auth(token_info):
return await aiohttp_token_info(token_info)
async def aiohttp_async_auth(token_info):
return await aiohttp_token_info(token_info)
async def aiohttp_bearer_auth(token_info):
return await aiohttp_token_info(token_info)
async def aiohttp_async_bearer_auth(token_info):
return await aiohttp_token_info(token_info)
async def get_datetime():
return ConnexionResponse(body={'value': datetime.datetime(2000, 1, 2, 3, 4, 5, 6)})
async def get_date():
return ConnexionResponse(body={'value': datetime.date(2000, 1, 2)})
async def get_uuid():
return ConnexionResponse(body={'value': uuid.UUID(hex='e7ff66d0-3ec2-4c4e-bed0-6e4723c24c51')})
async def aiohttp_multipart_single_file(myfile):
return aiohttp.web.json_response(
data={
'fileName': myfile.filename,
'myfile_content': myfile.file.read().decode('utf8')
},
)
async def aiohttp_multipart_many_files(myfiles):
return aiohttp.web.json_response(
data={
'files_count': len(myfiles),
'myfiles_content': [ f.file.read().decode('utf8') for f in myfiles ]
},
)
async def aiohttp_multipart_mixed_single_file(myfile, body):
dir_name = body['dir_name']
return aiohttp.web.json_response(
data={
'dir_name': dir_name,
'fileName': myfile.filename,
'myfile_content': myfile.file.read().decode('utf8'),
},
)
async def aiohttp_multipart_mixed_many_files(myfiles, body):
dir_name = body['dir_name']
test_count = body['test_count']
return aiohttp.web.json_response(
data={
'files_count': len(myfiles),
'dir_name': dir_name,
'test_count': test_count,
'myfiles_content': [ f.file.read().decode('utf8') for f in myfiles ]
},
)