diff --git a/functions/adoption/libs/utils.py b/functions/adoption/libs/utils.py index 882520f..3a5fe39 100644 --- a/functions/adoption/libs/utils.py +++ b/functions/adoption/libs/utils.py @@ -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 = [] diff --git a/functions/adoption/main.py b/functions/adoption/main.py index a774f01..6b1f196 100644 --- a/functions/adoption/main.py +++ b/functions/adoption/main.py @@ -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) @@ -16,4 +34,4 @@ def dispatcher(request): response = list_data(result.result) - return output(response) \ No newline at end of file + return output(response, headers) \ No newline at end of file diff --git a/functions/categories/libs/utils.py b/functions/categories/libs/utils.py index 21bab77..d2cfeed 100644 --- a/functions/categories/libs/utils.py +++ b/functions/categories/libs/utils.py @@ -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 = [] diff --git a/functions/categories/main.py b/functions/categories/main.py index a774f01..6b1f196 100644 --- a/functions/categories/main.py +++ b/functions/categories/main.py @@ -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) @@ -16,4 +34,4 @@ def dispatcher(request): response = list_data(result.result) - return output(response) \ No newline at end of file + return output(response, headers) \ No newline at end of file diff --git a/functions/cwvtech/libs/utils.py b/functions/cwvtech/libs/utils.py index b94321b..b0c5a67 100644 --- a/functions/cwvtech/libs/utils.py +++ b/functions/cwvtech/libs/utils.py @@ -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 = [] diff --git a/functions/cwvtech/main.py b/functions/cwvtech/main.py index a774f01..6b1f196 100644 --- a/functions/cwvtech/main.py +++ b/functions/cwvtech/main.py @@ -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) @@ -16,4 +34,4 @@ def dispatcher(request): response = list_data(result.result) - return output(response) \ No newline at end of file + return output(response, headers) \ No newline at end of file diff --git a/functions/geos/libs/utils.py b/functions/geos/libs/utils.py index 8c7acee..8706e4e 100644 --- a/functions/geos/libs/utils.py +++ b/functions/geos/libs/utils.py @@ -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 = [] diff --git a/functions/geos/main.py b/functions/geos/main.py index 19993d2..313546b 100644 --- a/functions/geos/main.py +++ b/functions/geos/main.py @@ -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) \ No newline at end of file + return output(response, headers) \ No newline at end of file diff --git a/functions/lighthouse/libs/utils.py b/functions/lighthouse/libs/utils.py index 21bab77..d2cfeed 100644 --- a/functions/lighthouse/libs/utils.py +++ b/functions/lighthouse/libs/utils.py @@ -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 = [] diff --git a/functions/lighthouse/main.py b/functions/lighthouse/main.py index a774f01..6b1f196 100644 --- a/functions/lighthouse/main.py +++ b/functions/lighthouse/main.py @@ -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) @@ -16,4 +34,4 @@ def dispatcher(request): response = list_data(result.result) - return output(response) \ No newline at end of file + return output(response, headers) \ No newline at end of file diff --git a/functions/ranks/libs/utils.py b/functions/ranks/libs/utils.py index 37828b8..d8f3e02 100644 --- a/functions/ranks/libs/utils.py +++ b/functions/ranks/libs/utils.py @@ -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 = [] diff --git a/functions/ranks/main.py b/functions/ranks/main.py index 13cfc76..1af1ba8 100644 --- a/functions/ranks/main.py +++ b/functions/ranks/main.py @@ -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) \ No newline at end of file + return output(response, headers) \ No newline at end of file diff --git a/functions/report/libs/utils.py b/functions/report/libs/utils.py index b94321b..b0c5a67 100644 --- a/functions/report/libs/utils.py +++ b/functions/report/libs/utils.py @@ -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 = [] diff --git a/functions/report/main.py b/functions/report/main.py index a774f01..6b1f196 100644 --- a/functions/report/main.py +++ b/functions/report/main.py @@ -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) @@ -16,4 +34,4 @@ def dispatcher(request): response = list_data(result.result) - return output(response) \ No newline at end of file + return output(response, headers) \ No newline at end of file diff --git a/functions/technologies/libs/utils.py b/functions/technologies/libs/utils.py index 21bab77..d2cfeed 100644 --- a/functions/technologies/libs/utils.py +++ b/functions/technologies/libs/utils.py @@ -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 = [] diff --git a/functions/technologies/main.py b/functions/technologies/main.py index a774f01..6b1f196 100644 --- a/functions/technologies/main.py +++ b/functions/technologies/main.py @@ -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) @@ -16,4 +34,4 @@ def dispatcher(request): response = list_data(result.result) - return output(response) \ No newline at end of file + return output(response, headers) \ No newline at end of file