From 492683445bf35121263fe4ea9e5feb7af6c548d4 Mon Sep 17 00:00:00 2001 From: Martin Aceto Date: Tue, 10 Dec 2024 17:20:30 -0500 Subject: [PATCH] new headers on adoption --- functions/adoption/libs/network.py | 17 +++++++++++++---- functions/adoption/libs/utils.py | 5 ----- functions/adoption/main.py | 12 +++--------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/functions/adoption/libs/network.py b/functions/adoption/libs/network.py index 59532bc..76a731f 100644 --- a/functions/adoption/libs/network.py +++ b/functions/adoption/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) \ No newline at end of file diff --git a/functions/adoption/libs/utils.py b/functions/adoption/libs/utils.py index 36b3a96..997c85e 100644 --- a/functions/adoption/libs/utils.py +++ b/functions/adoption/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/adoption/main.py b/functions/adoption/main.py index 7faece9..65865dc 100644 --- a/functions/adoption/main.py +++ b/functions/adoption/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,11 +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) @@ -23,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) + return respond(response)