diff --git a/functions/adoption/main.py b/functions/adoption/main.py index 53f25bb..7faece9 100644 --- a/functions/adoption/main.py +++ b/functions/adoption/main.py @@ -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): diff --git a/functions/categories/libs/network.py b/functions/categories/libs/network.py new file mode 100644 index 0000000..59532bc --- /dev/null +++ b/functions/categories/libs/network.py @@ -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) diff --git a/functions/categories/main.py b/functions/categories/main.py index e5813df..f69e8c2 100644 --- a/functions/categories/main.py +++ b/functions/categories/main.py @@ -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": "*", diff --git a/functions/cwvtech/libs/network.py b/functions/cwvtech/libs/network.py new file mode 100644 index 0000000..59532bc --- /dev/null +++ b/functions/cwvtech/libs/network.py @@ -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) diff --git a/functions/cwvtech/main.py b/functions/cwvtech/main.py index c1e3455..0450183 100644 --- a/functions/cwvtech/main.py +++ b/functions/cwvtech/main.py @@ -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" diff --git a/functions/geos/libs/network.py b/functions/geos/libs/network.py new file mode 100644 index 0000000..59532bc --- /dev/null +++ b/functions/geos/libs/network.py @@ -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) diff --git a/functions/geos/main.py b/functions/geos/main.py index 0bcca02..0d1c686 100644 --- a/functions/geos/main.py +++ b/functions/geos/main.py @@ -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" diff --git a/functions/lighthouse/libs/network.py b/functions/lighthouse/libs/network.py new file mode 100644 index 0000000..59532bc --- /dev/null +++ b/functions/lighthouse/libs/network.py @@ -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) diff --git a/functions/lighthouse/main.py b/functions/lighthouse/main.py index c1e3455..0450183 100644 --- a/functions/lighthouse/main.py +++ b/functions/lighthouse/main.py @@ -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" diff --git a/functions/page-weight/libs/network.py b/functions/page-weight/libs/network.py new file mode 100644 index 0000000..59532bc --- /dev/null +++ b/functions/page-weight/libs/network.py @@ -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) diff --git a/functions/page-weight/main.py b/functions/page-weight/main.py index c1e3455..0450183 100644 --- a/functions/page-weight/main.py +++ b/functions/page-weight/main.py @@ -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" diff --git a/functions/ranks/libs/network.py b/functions/ranks/libs/network.py new file mode 100644 index 0000000..59532bc --- /dev/null +++ b/functions/ranks/libs/network.py @@ -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) diff --git a/functions/ranks/main.py b/functions/ranks/main.py index 575239f..a742748 100644 --- a/functions/ranks/main.py +++ b/functions/ranks/main.py @@ -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" diff --git a/functions/technologies/libs/network.py b/functions/technologies/libs/network.py new file mode 100644 index 0000000..59532bc --- /dev/null +++ b/functions/technologies/libs/network.py @@ -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) diff --git a/functions/technologies/main.py b/functions/technologies/main.py index f0ed571..6bcdd8c 100644 --- a/functions/technologies/main.py +++ b/functions/technologies/main.py @@ -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"