Skip to content

Commit 03cb91f

Browse files
authored
Merge pull request #8 from HTTPArchive/add-cors-headers
Add CORS support
2 parents d8c25c8 + efd4163 commit 03cb91f

File tree

16 files changed

+168
-24
lines changed

16 files changed

+168
-24
lines changed

functions/adoption/libs/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import json
22

3-
def output(result):
3+
def output(result, headers={}):
44
status = 200 if result.success() else 400
55
payload = result.result if result.success() else convert_to_hashes(result.errors)
6-
return (json.dumps(payload), status)
6+
return (json.dumps(payload), status, headers)
77

88
def convert_to_hashes(arr):
99
hashes_arr = []

functions/adoption/main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55

66
@functions_framework.http
77
def dispatcher(request):
8+
# For more information about CORS and CORS preflight requests, see:
9+
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
10+
11+
# Set CORS headers for the preflight request
12+
if request.method == "OPTIONS":
13+
# Allows GET requests from any origin with the Content-Type
14+
# header and caches preflight response for an 3600s
15+
headers = {
16+
"Access-Control-Allow-Origin": "*",
17+
"Access-Control-Allow-Methods": "GET",
18+
"Access-Control-Allow-Headers": "Content-Type",
19+
"Access-Control-Max-Age": "3600",
20+
}
21+
22+
return ("", 204, headers)
23+
24+
# Set CORS headers for the main request
25+
headers = {"Access-Control-Allow-Origin": "*"}
826
args = request.args.to_dict()
927

1028
validator = Validator(params=args)
@@ -16,4 +34,4 @@ def dispatcher(request):
1634

1735
response = list_data(result.result)
1836

19-
return output(response)
37+
return output(response, headers)

functions/categories/libs/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import json
22

3-
def output(result):
3+
def output(result, headers={}):
44
status = 200 if result.success() else 400
55
payload = result.result if result.success() else convert_to_hashes(result.errors)
6-
return (json.dumps(payload), status)
6+
return (json.dumps(payload), status, headers)
77

88
def convert_to_hashes(arr):
99
hashes_arr = []

functions/categories/main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55

66
@functions_framework.http
77
def dispatcher(request):
8+
# For more information about CORS and CORS preflight requests, see:
9+
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
10+
11+
# Set CORS headers for the preflight request
12+
if request.method == "OPTIONS":
13+
# Allows GET requests from any origin with the Content-Type
14+
# header and caches preflight response for an 3600s
15+
headers = {
16+
"Access-Control-Allow-Origin": "*",
17+
"Access-Control-Allow-Methods": "GET",
18+
"Access-Control-Allow-Headers": "Content-Type",
19+
"Access-Control-Max-Age": "3600",
20+
}
21+
22+
return ("", 204, headers)
23+
24+
# Set CORS headers for the main request
25+
headers = {"Access-Control-Allow-Origin": "*"}
826
args = request.args.to_dict()
927

1028
validator = Validator(params=args)
@@ -16,4 +34,4 @@ def dispatcher(request):
1634

1735
response = list_data(result.result)
1836

19-
return output(response)
37+
return output(response, headers)

functions/cwvtech/libs/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import json
22

3-
def output(result):
3+
def output(result, headers={}):
44
status = 200 if result.success() else 400
55
payload = result.result if result.success() else convert_to_hashes(result.errors)
6-
return (json.dumps(payload), status)
6+
return (json.dumps(payload), status, headers)
77

88
def convert_to_hashes(arr):
99
hashes_arr = []

functions/cwvtech/main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55

66
@functions_framework.http
77
def dispatcher(request):
8+
# For more information about CORS and CORS preflight requests, see:
9+
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
10+
11+
# Set CORS headers for the preflight request
12+
if request.method == "OPTIONS":
13+
# Allows GET requests from any origin with the Content-Type
14+
# header and caches preflight response for an 3600s
15+
headers = {
16+
"Access-Control-Allow-Origin": "*",
17+
"Access-Control-Allow-Methods": "GET",
18+
"Access-Control-Allow-Headers": "Content-Type",
19+
"Access-Control-Max-Age": "3600",
20+
}
21+
22+
return ("", 204, headers)
23+
24+
# Set CORS headers for the main request
25+
headers = {"Access-Control-Allow-Origin": "*"}
826
args = request.args.to_dict()
927

1028
validator = Validator(params=args)
@@ -16,4 +34,4 @@ def dispatcher(request):
1634

1735
response = list_data(result.result)
1836

19-
return output(response)
37+
return output(response, headers)

functions/geos/libs/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import json
22

3-
def output(result):
3+
def output(result, headers={}):
44
status = 200 if result.success() else 400
55
payload = result.result if result.success() else convert_to_hashes(result.errors)
6-
return (json.dumps(payload), status)
6+
return (json.dumps(payload), status, headers)
77

88
def convert_to_hashes(arr):
99
hashes_arr = []

functions/geos/main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,25 @@
66

77
@functions_framework.http
88
def dispatcher(request):
9+
# For more information about CORS and CORS preflight requests, see:
10+
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
11+
12+
# Set CORS headers for the preflight request
13+
if request.method == "OPTIONS":
14+
# Allows GET requests from any origin with the Content-Type
15+
# header and caches preflight response for an 3600s
16+
headers = {
17+
"Access-Control-Allow-Origin": "*",
18+
"Access-Control-Allow-Methods": "GET",
19+
"Access-Control-Allow-Headers": "Content-Type",
20+
"Access-Control-Max-Age": "3600",
21+
}
22+
23+
return ("", 204, headers)
24+
25+
# Set CORS headers for the main request
26+
headers = {"Access-Control-Allow-Origin": "*"}
927

1028
response = Result(result=COUNTRIES)
1129

12-
return output(response)
30+
return output(response, headers)

functions/lighthouse/libs/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import json
22

3-
def output(result):
3+
def output(result, headers={}):
44
status = 200 if result.success() else 400
55
payload = result.result if result.success() else convert_to_hashes(result.errors)
6-
return (json.dumps(payload), status)
6+
return (json.dumps(payload), status, headers)
77

88
def convert_to_hashes(arr):
99
hashes_arr = []

functions/lighthouse/main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55

66
@functions_framework.http
77
def dispatcher(request):
8+
# For more information about CORS and CORS preflight requests, see:
9+
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
10+
11+
# Set CORS headers for the preflight request
12+
if request.method == "OPTIONS":
13+
# Allows GET requests from any origin with the Content-Type
14+
# header and caches preflight response for an 3600s
15+
headers = {
16+
"Access-Control-Allow-Origin": "*",
17+
"Access-Control-Allow-Methods": "GET",
18+
"Access-Control-Allow-Headers": "Content-Type",
19+
"Access-Control-Max-Age": "3600",
20+
}
21+
22+
return ("", 204, headers)
23+
24+
# Set CORS headers for the main request
25+
headers = {"Access-Control-Allow-Origin": "*"}
826
args = request.args.to_dict()
927

1028
validator = Validator(params=args)
@@ -16,4 +34,4 @@ def dispatcher(request):
1634

1735
response = list_data(result.result)
1836

19-
return output(response)
37+
return output(response, headers)

0 commit comments

Comments
 (0)