Skip to content

Commit c4f96f6

Browse files
committed
refactor network respond method
1 parent e2b0501 commit c4f96f6

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

functions/technologies/libs/network.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,33 @@
55
Handles formatting responses to match the tuple pattern required by
66
the flask/GCP wrapper for Cloud Functions.
77
"""
8+
import json
9+
from .utils import convert_to_hashes
810

911
PREFLIGHT_HEADERS = {
1012
"Access-Control-Allow-Origin": "*",
1113
"Access-Control-Allow-Methods": "GET",
12-
"Access-Control-Allow-Headers": "Content-Type",
14+
"Access-Control-Allow-Headers": "Content-Type, Timing-Allow-Origin",
1315
"Access-Control-Max-Age": "3600",
1416
}
1517

16-
HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}
18+
HEADERS = {
19+
"Access-Control-Allow-Origin": "*",
20+
"Content-Type": "application/json",
21+
"cache-control": "public, max-age=21600",
22+
"Timing-Allow-Origin": "*"
23+
}
1724

1825
def respond_cors():
1926
"""
2027
To be used to return OPTIONS responses to satisfy CORS preflight requests.
2128
"""
2229
return ("", 204, PREFLIGHT_HEADERS)
2330

24-
def respond(data, status=200):
31+
def respond(result, headers=HEADERS):
2532
"""
2633
To be used to return responses to satisfy CORS requests.
2734
"""
28-
return (data, status, HEADERS)
35+
status = 200 if result.success() else 400
36+
payload = result.result if result.success() else convert_to_hashes(result.errors)
37+
return (json.dumps(payload), status, headers)

functions/technologies/libs/utils.py

-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import json
22
from urllib.parse import unquote
33

4-
def output(result, headers={}):
5-
status = 200 if result.success() else 400
6-
payload = result.result if result.success() else convert_to_hashes(result.errors)
7-
return (json.dumps(payload), status, headers)
8-
94
def convert_to_hashes(arr):
105
hashes_arr = []
116
for inner_arr in arr:

functions/technologies/main.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
import functions_framework
22

33
from .libs.validator import Validator
4-
from .libs.utils import output
54
from .libs.queries import list_data
6-
from .libs.network import respond_cors
5+
from .libs.network import respond_cors, respond
76

87
@functions_framework.http
98
def dispatcher(request):
109

1110
if request.method == "OPTIONS":
1211
return respond_cors()
1312

14-
headers = {
15-
"Access-Control-Allow-Origin": "*",
16-
"cache-control": "public, max-age=21600"
17-
}
1813
args = request.args.to_dict()
1914

2015
validator = Validator(params=args)
2116
result = validator.validate()
2217

2318
if result.failure():
2419
print("error", result.errors)
25-
return output(result)
20+
return respond(result)
2621

2722
response = list_data(result.result)
2823

29-
return output(response, headers)
24+
return respond(response)

0 commit comments

Comments
 (0)