diff --git a/functions/technologies/libs/network.py b/functions/technologies/libs/network.py index 59532bc..d097aa8 100644 --- a/functions/technologies/libs/network.py +++ b/functions/technologies/libs/network.py @@ -5,15 +5,22 @@ Handles formatting responses to match the tuple pattern required by the flask/GCP wrapper for Cloud Functions. """ +import json +from .utils import convert_to_hashes PREFLIGHT_HEADERS = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET", - "Access-Control-Allow-Headers": "Content-Type", + "Access-Control-Allow-Headers": "Content-Type, Timing-Allow-Origin", "Access-Control-Max-Age": "3600", } -HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"} +HEADERS = { + "Access-Control-Allow-Origin": "*", + "Content-Type": "application/json", + "cache-control": "public, max-age=21600", + "Timing-Allow-Origin": "*" + } def respond_cors(): """ @@ -21,8 +28,10 @@ def respond_cors(): """ return ("", 204, PREFLIGHT_HEADERS) -def respond(data, status=200): +def respond(result, headers=HEADERS): """ To be used to return responses to satisfy CORS requests. """ - return (data, status, 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, headers) diff --git a/functions/technologies/libs/utils.py b/functions/technologies/libs/utils.py index 63890f1..29692f4 100644 --- a/functions/technologies/libs/utils.py +++ b/functions/technologies/libs/utils.py @@ -1,11 +1,6 @@ import json from urllib.parse import unquote -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, headers) - def convert_to_hashes(arr): hashes_arr = [] for inner_arr in arr: diff --git a/functions/technologies/main.py b/functions/technologies/main.py index 6bcdd8c..3fab032 100644 --- a/functions/technologies/main.py +++ b/functions/technologies/main.py @@ -1,9 +1,8 @@ import functions_framework from .libs.validator import Validator -from .libs.utils import output from .libs.queries import list_data -from .libs.network import respond_cors +from .libs.network import respond_cors, respond @functions_framework.http def dispatcher(request): @@ -11,10 +10,6 @@ def dispatcher(request): if request.method == "OPTIONS": return respond_cors() - headers = { - "Access-Control-Allow-Origin": "*", - "cache-control": "public, max-age=21600" - } args = request.args.to_dict() validator = Validator(params=args) @@ -22,8 +17,8 @@ def dispatcher(request): if result.failure(): print("error", result.errors) - return output(result) + return respond(result) response = list_data(result.result) - return output(response, headers) \ No newline at end of file + return respond(response) \ No newline at end of file