-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.py
63 lines (47 loc) · 1.72 KB
/
func.py
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
from parliament import Context
from flask import Request
import json
# parse request body, json data or URL query parameters
def payload_print(req: Request) -> str:
if req.method == "POST":
if req.is_json:
return json.dumps(req.json) + "\n"
else:
# MultiDict needs some iteration
ret = "{"
for key in req.form.keys():
ret += '"' + key + '": "'+ req.form[key] + '", '
return ret[:-2] + "}\n" if len(ret) > 2 else "{}"
elif req.method == "GET":
# MultiDict needs some iteration
ret = "{"
for key in req.args.keys():
ret += '"' + key + '": "' + req.args[key] + '", '
return ret[:-2] + "}\n" if len(ret) > 2 else "{}"
# pretty print the request to stdout instantaneously
def pretty_print(req: Request) -> str:
ret = str(req.method) + ' ' + str(req.url) + ' ' + str(req.host) + '\n'
for (header, values) in req.headers:
ret += " " + str(header) + ": " + values + '\n'
if req.method == "POST":
ret += "Request body:\n"
ret += " " + payload_print(req) + '\n'
elif req.method == "GET":
ret += "URL Query String:\n"
ret += " " + payload_print(req) + '\n'
return ret
def main(context: Context):
"""
Function template
The context parameter contains the Flask request object and any
CloudEvent received with the request.
"""
# Add your business logic here
print("Received request")
if 'request' in context.keys():
ret = pretty_print(context.request)
print(ret, flush=True)
return payload_print(context.request), 200
else:
print("Empty request", flush=True)
return "{}", 200