forked from GoogleCloudPlatform/functions-framework-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_main.py
More file actions
59 lines (37 loc) · 1.22 KB
/
async_main.py
File metadata and controls
59 lines (37 loc) · 1.22 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
import asyncio
import json
from cloudevents.http import to_json
import functions_framework.aio
filename = "function_output.json"
class RawJson:
data: dict
def __init__(self, data):
self.data = data
@staticmethod
def from_dict(obj: dict) -> "RawJson":
return RawJson(obj)
def to_dict(self) -> dict:
return self.data
def _write_output(content):
with open(filename, "w") as f:
f.write(content)
async def write_http(request):
json_data = await request.json()
_write_output(json.dumps(json_data))
return "OK", 200
async def write_cloud_event(cloud_event):
_write_output(to_json(cloud_event).decode())
@functions_framework.aio.http
async def write_http_declarative(request):
json_data = await request.json()
_write_output(json.dumps(json_data))
return "OK", 200
@functions_framework.aio.cloud_event
async def write_cloud_event_declarative(cloud_event):
_write_output(to_json(cloud_event).decode())
@functions_framework.aio.http
async def write_http_declarative_concurrent(request):
await asyncio.sleep(1)
return "OK", 200
# Note: Typed events are not supported in ASGI mode yet
# Legacy event functions are also not supported in ASGI mode