Skip to content

Commit

Permalink
move options to libs
Browse files Browse the repository at this point in the history
  • Loading branch information
maceto committed Jun 12, 2024
1 parent e68fd39 commit bf9dbff
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 110 deletions.
3 changes: 2 additions & 1 deletion functions/adoption/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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, respond
from .libs.network import respond_cors

@functions_framework.http
def dispatcher(request):
Expand Down
28 changes: 28 additions & 0 deletions functions/categories/libs/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

"""
Network
Handles formatting responses to match the tuple pattern required by
the flask/GCP wrapper for Cloud Functions.
"""

PREFLIGHT_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}

def respond_cors():
"""
To be used to return OPTIONS responses to satisfy CORS preflight requests.
"""
return ("", 204, PREFLIGHT_HEADERS)

def respond(data, status=200):
"""
To be used to return responses to satisfy CORS requests.
"""
return (data, status, HEADERS)
16 changes: 3 additions & 13 deletions functions/categories/main.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
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

@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)
return respond_cors()

headers = {
"Access-Control-Allow-Origin": "*",
Expand Down
28 changes: 28 additions & 0 deletions functions/cwvtech/libs/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

"""
Network
Handles formatting responses to match the tuple pattern required by
the flask/GCP wrapper for Cloud Functions.
"""

PREFLIGHT_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}

def respond_cors():
"""
To be used to return OPTIONS responses to satisfy CORS preflight requests.
"""
return ("", 204, PREFLIGHT_HEADERS)

def respond(data, status=200):
"""
To be used to return responses to satisfy CORS requests.
"""
return (data, status, HEADERS)
21 changes: 5 additions & 16 deletions functions/cwvtech/main.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
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

@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
return respond_cors()

headers = {
"Access-Control-Allow-Origin": "*",
"cache-control": "public, max-age=21600"
Expand Down
28 changes: 28 additions & 0 deletions functions/geos/libs/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

"""
Network
Handles formatting responses to match the tuple pattern required by
the flask/GCP wrapper for Cloud Functions.
"""

PREFLIGHT_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}

def respond_cors():
"""
To be used to return OPTIONS responses to satisfy CORS preflight requests.
"""
return ("", 204, PREFLIGHT_HEADERS)

def respond(data, status=200):
"""
To be used to return responses to satisfy CORS requests.
"""
return (data, status, HEADERS)
20 changes: 4 additions & 16 deletions functions/geos/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,14 @@
from .libs.utils import output
from .libs.utils import ( COUNTRIES )
from .libs.result import Result
from .libs.network import respond_cors

@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
return respond_cors()

headers = {
"Access-Control-Allow-Origin": "*",
"cache-control": "public, max-age=21600"
Expand Down
28 changes: 28 additions & 0 deletions functions/lighthouse/libs/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

"""
Network
Handles formatting responses to match the tuple pattern required by
the flask/GCP wrapper for Cloud Functions.
"""

PREFLIGHT_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}

def respond_cors():
"""
To be used to return OPTIONS responses to satisfy CORS preflight requests.
"""
return ("", 204, PREFLIGHT_HEADERS)

def respond(data, status=200):
"""
To be used to return responses to satisfy CORS requests.
"""
return (data, status, HEADERS)
21 changes: 5 additions & 16 deletions functions/lighthouse/main.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
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

@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
return respond_cors()

headers = {
"Access-Control-Allow-Origin": "*",
"cache-control": "public, max-age=21600"
Expand Down
28 changes: 28 additions & 0 deletions functions/page-weight/libs/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

"""
Network
Handles formatting responses to match the tuple pattern required by
the flask/GCP wrapper for Cloud Functions.
"""

PREFLIGHT_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}

def respond_cors():
"""
To be used to return OPTIONS responses to satisfy CORS preflight requests.
"""
return ("", 204, PREFLIGHT_HEADERS)

def respond(data, status=200):
"""
To be used to return responses to satisfy CORS requests.
"""
return (data, status, HEADERS)
21 changes: 5 additions & 16 deletions functions/page-weight/main.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
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

@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
return respond_cors()

headers = {
"Access-Control-Allow-Origin": "*",
"cache-control": "public, max-age=21600"
Expand Down
28 changes: 28 additions & 0 deletions functions/ranks/libs/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

"""
Network
Handles formatting responses to match the tuple pattern required by
the flask/GCP wrapper for Cloud Functions.
"""

PREFLIGHT_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}

def respond_cors():
"""
To be used to return OPTIONS responses to satisfy CORS preflight requests.
"""
return ("", 204, PREFLIGHT_HEADERS)

def respond(data, status=200):
"""
To be used to return responses to satisfy CORS requests.
"""
return (data, status, HEADERS)
20 changes: 4 additions & 16 deletions functions/ranks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,14 @@
from .libs.utils import output
from .libs.utils import ( RANKS )
from .libs.result import Result
from .libs.network import respond_cors

@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
return respond_cors()

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

0 comments on commit bf9dbff

Please sign in to comment.