Skip to content

Commit

Permalink
Merge pull request #8 from HTTPArchive/add-cors-headers
Browse files Browse the repository at this point in the history
Add CORS support
  • Loading branch information
maceto authored Nov 6, 2023
2 parents d8c25c8 + efd4163 commit 03cb91f
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 24 deletions.
4 changes: 2 additions & 2 deletions functions/adoption/libs/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json

def output(result):
def output(result, headers={}):
status = 200 if result.success() else 400
payload = result.result if result.success() else convert_to_hashes(result.errors)
return (json.dumps(payload), status)
return (json.dumps(payload), status, headers)

def convert_to_hashes(arr):
hashes_arr = []
Expand Down
20 changes: 19 additions & 1 deletion functions/adoption/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@

@functions_framework.http
def dispatcher(request):
# For more information about CORS and CORS preflight requests, see:
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

# Set CORS headers for the preflight request
if request.method == "OPTIONS":
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

return ("", 204, headers)

# Set CORS headers for the main request
headers = {"Access-Control-Allow-Origin": "*"}
args = request.args.to_dict()

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

response = list_data(result.result)

return output(response)
return output(response, headers)
4 changes: 2 additions & 2 deletions functions/categories/libs/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json

def output(result):
def output(result, headers={}):
status = 200 if result.success() else 400
payload = result.result if result.success() else convert_to_hashes(result.errors)
return (json.dumps(payload), status)
return (json.dumps(payload), status, headers)

def convert_to_hashes(arr):
hashes_arr = []
Expand Down
20 changes: 19 additions & 1 deletion functions/categories/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@

@functions_framework.http
def dispatcher(request):
# For more information about CORS and CORS preflight requests, see:
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

# Set CORS headers for the preflight request
if request.method == "OPTIONS":
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

return ("", 204, headers)

# Set CORS headers for the main request
headers = {"Access-Control-Allow-Origin": "*"}
args = request.args.to_dict()

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

response = list_data(result.result)

return output(response)
return output(response, headers)
4 changes: 2 additions & 2 deletions functions/cwvtech/libs/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json

def output(result):
def output(result, headers={}):
status = 200 if result.success() else 400
payload = result.result if result.success() else convert_to_hashes(result.errors)
return (json.dumps(payload), status)
return (json.dumps(payload), status, headers)

def convert_to_hashes(arr):
hashes_arr = []
Expand Down
20 changes: 19 additions & 1 deletion functions/cwvtech/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@

@functions_framework.http
def dispatcher(request):
# For more information about CORS and CORS preflight requests, see:
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

# Set CORS headers for the preflight request
if request.method == "OPTIONS":
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

return ("", 204, headers)

# Set CORS headers for the main request
headers = {"Access-Control-Allow-Origin": "*"}
args = request.args.to_dict()

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

response = list_data(result.result)

return output(response)
return output(response, headers)
4 changes: 2 additions & 2 deletions functions/geos/libs/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json

def output(result):
def output(result, headers={}):
status = 200 if result.success() else 400
payload = result.result if result.success() else convert_to_hashes(result.errors)
return (json.dumps(payload), status)
return (json.dumps(payload), status, headers)

def convert_to_hashes(arr):
hashes_arr = []
Expand Down
20 changes: 19 additions & 1 deletion functions/geos/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,25 @@

@functions_framework.http
def dispatcher(request):
# For more information about CORS and CORS preflight requests, see:
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

# Set CORS headers for the preflight request
if request.method == "OPTIONS":
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

return ("", 204, headers)

# Set CORS headers for the main request
headers = {"Access-Control-Allow-Origin": "*"}

response = Result(result=COUNTRIES)

return output(response)
return output(response, headers)
4 changes: 2 additions & 2 deletions functions/lighthouse/libs/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json

def output(result):
def output(result, headers={}):
status = 200 if result.success() else 400
payload = result.result if result.success() else convert_to_hashes(result.errors)
return (json.dumps(payload), status)
return (json.dumps(payload), status, headers)

def convert_to_hashes(arr):
hashes_arr = []
Expand Down
20 changes: 19 additions & 1 deletion functions/lighthouse/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@

@functions_framework.http
def dispatcher(request):
# For more information about CORS and CORS preflight requests, see:
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

# Set CORS headers for the preflight request
if request.method == "OPTIONS":
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

return ("", 204, headers)

# Set CORS headers for the main request
headers = {"Access-Control-Allow-Origin": "*"}
args = request.args.to_dict()

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

response = list_data(result.result)

return output(response)
return output(response, headers)
4 changes: 2 additions & 2 deletions functions/ranks/libs/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json

def output(result):
def output(result, headers={}):
status = 200 if result.success() else 400
payload = result.result if result.success() else convert_to_hashes(result.errors)
return (json.dumps(payload), status)
return (json.dumps(payload), status, headers)

def convert_to_hashes(arr):
hashes_arr = []
Expand Down
20 changes: 19 additions & 1 deletion functions/ranks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,25 @@

@functions_framework.http
def dispatcher(request):
# For more information about CORS and CORS preflight requests, see:
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

# Set CORS headers for the preflight request
if request.method == "OPTIONS":
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

return ("", 204, headers)

# Set CORS headers for the main request
headers = {"Access-Control-Allow-Origin": "*"}

response = Result(result=RANKS)

return output(response)
return output(response, headers)
4 changes: 2 additions & 2 deletions functions/report/libs/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json

def output(result):
def output(result, headers={}):
status = 200 if result.success() else 400
payload = result.result if result.success() else convert_to_hashes(result.errors)
return (json.dumps(payload), status)
return (json.dumps(payload), status, headers)

def convert_to_hashes(arr):
hashes_arr = []
Expand Down
20 changes: 19 additions & 1 deletion functions/report/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@

@functions_framework.http
def dispatcher(request):
# For more information about CORS and CORS preflight requests, see:
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

# Set CORS headers for the preflight request
if request.method == "OPTIONS":
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

return ("", 204, headers)

# Set CORS headers for the main request
headers = {"Access-Control-Allow-Origin": "*"}
args = request.args.to_dict()

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

response = list_data(result.result)

return output(response)
return output(response, headers)
4 changes: 2 additions & 2 deletions functions/technologies/libs/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json

def output(result):
def output(result, headers={}):
status = 200 if result.success() else 400
payload = result.result if result.success() else convert_to_hashes(result.errors)
return (json.dumps(payload), status)
return (json.dumps(payload), status, headers)

def convert_to_hashes(arr):
hashes_arr = []
Expand Down
20 changes: 19 additions & 1 deletion functions/technologies/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@

@functions_framework.http
def dispatcher(request):
# For more information about CORS and CORS preflight requests, see:
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

# Set CORS headers for the preflight request
if request.method == "OPTIONS":
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

return ("", 204, headers)

# Set CORS headers for the main request
headers = {"Access-Control-Allow-Origin": "*"}
args = request.args.to_dict()

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

response = list_data(result.result)

return output(response)
return output(response, headers)

0 comments on commit 03cb91f

Please sign in to comment.