-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_app.py
More file actions
52 lines (37 loc) · 1.47 KB
/
function_app.py
File metadata and controls
52 lines (37 loc) · 1.47 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
import base64
import json
from aiohttp import web as aiohttp_web, test_utils as aiohttp_test
from app import types as app_types
async def hello(req: aiohttp_web.Request):
if req.can_read_body:
body = await req.json()
else:
raise aiohttp_web.HTTPException()
return aiohttp_web.json_response(body)
async def get_hello(req: aiohttp_web.Request):
return aiohttp_web.json_response({"req": req.__dict__})
async def handler(event: app_types.YFunctionEvent, ctx):
print(event)
print(ctx)
server = aiohttp_web.Application()
server.router.add_post('/api/endpoint', hello)
server.router.add_get('/api/endpoint', get_hello)
async with aiohttp_test.TestClient(aiohttp_test.TestServer(server)) as dispatcher:
async with dispatcher.request(
event['httpMethod'],
event['url'],
params=event['multiValueQueryStringParameters'],
data=(base64.b64decode(event['body']) if event['isBase64Encoded'] else event['body']),
headers=event['headers'],
) as resp:
y_responst = {
"statusCode": resp.status,
"isBase64Encoded": False,
}
if resp.status >= 400:
return y_responst
y_responst["headers"] = {'Content-Type': 'application/json'},
body = await resp.json()
if body is not None:
y_responst['body'] = json.dumps(body)
return y_responst