Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the Timing-Allow-Origin header. #28

Merged
merged 11 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions functions/adoption/libs/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@
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():
"""
To be used to return OPTIONS responses to satisfy CORS preflight requests.
"""
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)
5 changes: 0 additions & 5 deletions functions/adoption/libs/utils.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
12 changes: 3 additions & 9 deletions functions/adoption/main.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
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):

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)
result = validator.validate()

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)
17 changes: 13 additions & 4 deletions functions/categories/libs/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@
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():
"""
To be used to return OPTIONS responses to satisfy CORS preflight requests.
"""
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)
5 changes: 0 additions & 5 deletions functions/categories/libs/utils.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
12 changes: 3 additions & 9 deletions functions/categories/main.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
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):

if request.method == "OPTIONS":
return respond_cors()

headers = {
"Access-Control-Allow-Origin": "*",
"cache-control": "public, max-age=21600"
}

args = request.args.to_dict()

Expand All @@ -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)
17 changes: 13 additions & 4 deletions functions/cwvtech/libs/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@
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():
"""
To be used to return OPTIONS responses to satisfy CORS preflight requests.
"""
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)
5 changes: 0 additions & 5 deletions functions/cwvtech/libs/utils.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
14 changes: 4 additions & 10 deletions functions/cwvtech/main.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
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):

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)
result = validator.validate()

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)
17 changes: 13 additions & 4 deletions functions/geos/libs/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@
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():
"""
To be used to return OPTIONS responses to satisfy CORS preflight requests.
"""
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)
5 changes: 0 additions & 5 deletions functions/geos/libs/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import json

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:
Expand Down
12 changes: 3 additions & 9 deletions functions/geos/main.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import functions_framework

from .libs.utils import output
from .libs.utils import ( COUNTRIES )
from .libs.result import Result
from .libs.network import respond_cors
from .libs.network import respond_cors, respond

@functions_framework.http
def dispatcher(request):

if request.method == "OPTIONS":
return respond_cors()

headers = {
"Access-Control-Allow-Origin": "*",
"cache-control": "public, max-age=21600"
}


response = Result(result=COUNTRIES)

return output(response, headers)
return respond(response)
17 changes: 13 additions & 4 deletions functions/lighthouse/libs/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@
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():
"""
To be used to return OPTIONS responses to satisfy CORS preflight requests.
"""
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)
5 changes: 0 additions & 5 deletions functions/lighthouse/libs/utils.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Loading
Loading