Skip to content

Commit bf9dbff

Browse files
committed
move options to libs
1 parent e68fd39 commit bf9dbff

File tree

15 files changed

+229
-110
lines changed

15 files changed

+229
-110
lines changed

functions/adoption/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import functions_framework
2+
23
from .libs.validator import Validator
34
from .libs.utils import output
45
from .libs.queries import list_data
5-
from .libs.network import respond_cors, respond
6+
from .libs.network import respond_cors
67

78
@functions_framework.http
89
def dispatcher(request):

functions/categories/libs/network.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
"""
3+
Network
4+
5+
Handles formatting responses to match the tuple pattern required by
6+
the flask/GCP wrapper for Cloud Functions.
7+
"""
8+
9+
PREFLIGHT_HEADERS = {
10+
"Access-Control-Allow-Origin": "*",
11+
"Access-Control-Allow-Methods": "GET",
12+
"Access-Control-Allow-Headers": "Content-Type",
13+
"Access-Control-Max-Age": "3600",
14+
}
15+
16+
HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}
17+
18+
def respond_cors():
19+
"""
20+
To be used to return OPTIONS responses to satisfy CORS preflight requests.
21+
"""
22+
return ("", 204, PREFLIGHT_HEADERS)
23+
24+
def respond(data, status=200):
25+
"""
26+
To be used to return responses to satisfy CORS requests.
27+
"""
28+
return (data, status, HEADERS)

functions/categories/main.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
import functions_framework
2+
23
from .libs.validator import Validator
34
from .libs.utils import output
45
from .libs.queries import list_data
6+
from .libs.network import respond_cors
57

68
@functions_framework.http
79
def dispatcher(request):
8-
# For more information about CORS and CORS preflight requests, see:
9-
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
1010

11-
# Set CORS headers for the preflight request
1211
if request.method == "OPTIONS":
13-
# Allows GET requests from any origin with the Content-Type
14-
# header and caches preflight response for an 3600s
15-
headers = {
16-
"Access-Control-Allow-Origin": "*",
17-
"Access-Control-Allow-Methods": "GET",
18-
"Access-Control-Allow-Headers": "Content-Type",
19-
"Access-Control-Max-Age": "3600",
20-
}
21-
22-
return ("", 204, headers)
12+
return respond_cors()
2313

2414
headers = {
2515
"Access-Control-Allow-Origin": "*",

functions/cwvtech/libs/network.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
"""
3+
Network
4+
5+
Handles formatting responses to match the tuple pattern required by
6+
the flask/GCP wrapper for Cloud Functions.
7+
"""
8+
9+
PREFLIGHT_HEADERS = {
10+
"Access-Control-Allow-Origin": "*",
11+
"Access-Control-Allow-Methods": "GET",
12+
"Access-Control-Allow-Headers": "Content-Type",
13+
"Access-Control-Max-Age": "3600",
14+
}
15+
16+
HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}
17+
18+
def respond_cors():
19+
"""
20+
To be used to return OPTIONS responses to satisfy CORS preflight requests.
21+
"""
22+
return ("", 204, PREFLIGHT_HEADERS)
23+
24+
def respond(data, status=200):
25+
"""
26+
To be used to return responses to satisfy CORS requests.
27+
"""
28+
return (data, status, HEADERS)

functions/cwvtech/main.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
import functions_framework
2+
23
from .libs.validator import Validator
34
from .libs.utils import output
45
from .libs.queries import list_data
6+
from .libs.network import respond_cors
57

68
@functions_framework.http
79
def dispatcher(request):
8-
# For more information about CORS and CORS preflight requests, see:
9-
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
10-
11-
# Set CORS headers for the preflight request
10+
1211
if request.method == "OPTIONS":
13-
# Allows GET requests from any origin with the Content-Type
14-
# header and caches preflight response for an 3600s
15-
headers = {
16-
"Access-Control-Allow-Origin": "*",
17-
"Access-Control-Allow-Methods": "GET",
18-
"Access-Control-Allow-Headers": "Content-Type",
19-
"Access-Control-Max-Age": "3600",
20-
}
21-
22-
return ("", 204, headers)
23-
24-
# Set CORS headers for the main request
12+
return respond_cors()
13+
2514
headers = {
2615
"Access-Control-Allow-Origin": "*",
2716
"cache-control": "public, max-age=21600"

functions/geos/libs/network.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
"""
3+
Network
4+
5+
Handles formatting responses to match the tuple pattern required by
6+
the flask/GCP wrapper for Cloud Functions.
7+
"""
8+
9+
PREFLIGHT_HEADERS = {
10+
"Access-Control-Allow-Origin": "*",
11+
"Access-Control-Allow-Methods": "GET",
12+
"Access-Control-Allow-Headers": "Content-Type",
13+
"Access-Control-Max-Age": "3600",
14+
}
15+
16+
HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}
17+
18+
def respond_cors():
19+
"""
20+
To be used to return OPTIONS responses to satisfy CORS preflight requests.
21+
"""
22+
return ("", 204, PREFLIGHT_HEADERS)
23+
24+
def respond(data, status=200):
25+
"""
26+
To be used to return responses to satisfy CORS requests.
27+
"""
28+
return (data, status, HEADERS)

functions/geos/main.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,14 @@
33
from .libs.utils import output
44
from .libs.utils import ( COUNTRIES )
55
from .libs.result import Result
6+
from .libs.network import respond_cors
67

78
@functions_framework.http
89
def dispatcher(request):
9-
# For more information about CORS and CORS preflight requests, see:
10-
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
11-
12-
# Set CORS headers for the preflight request
10+
1311
if request.method == "OPTIONS":
14-
# Allows GET requests from any origin with the Content-Type
15-
# header and caches preflight response for an 3600s
16-
headers = {
17-
"Access-Control-Allow-Origin": "*",
18-
"Access-Control-Allow-Methods": "GET",
19-
"Access-Control-Allow-Headers": "Content-Type",
20-
"Access-Control-Max-Age": "3600",
21-
}
22-
23-
return ("", 204, headers)
24-
25-
# Set CORS headers for the main request
12+
return respond_cors()
13+
2614
headers = {
2715
"Access-Control-Allow-Origin": "*",
2816
"cache-control": "public, max-age=21600"

functions/lighthouse/libs/network.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
"""
3+
Network
4+
5+
Handles formatting responses to match the tuple pattern required by
6+
the flask/GCP wrapper for Cloud Functions.
7+
"""
8+
9+
PREFLIGHT_HEADERS = {
10+
"Access-Control-Allow-Origin": "*",
11+
"Access-Control-Allow-Methods": "GET",
12+
"Access-Control-Allow-Headers": "Content-Type",
13+
"Access-Control-Max-Age": "3600",
14+
}
15+
16+
HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}
17+
18+
def respond_cors():
19+
"""
20+
To be used to return OPTIONS responses to satisfy CORS preflight requests.
21+
"""
22+
return ("", 204, PREFLIGHT_HEADERS)
23+
24+
def respond(data, status=200):
25+
"""
26+
To be used to return responses to satisfy CORS requests.
27+
"""
28+
return (data, status, HEADERS)

functions/lighthouse/main.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
import functions_framework
2+
23
from .libs.validator import Validator
34
from .libs.utils import output
45
from .libs.queries import list_data
6+
from .libs.network import respond_cors
57

68
@functions_framework.http
79
def dispatcher(request):
8-
# For more information about CORS and CORS preflight requests, see:
9-
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
10-
11-
# Set CORS headers for the preflight request
10+
1211
if request.method == "OPTIONS":
13-
# Allows GET requests from any origin with the Content-Type
14-
# header and caches preflight response for an 3600s
15-
headers = {
16-
"Access-Control-Allow-Origin": "*",
17-
"Access-Control-Allow-Methods": "GET",
18-
"Access-Control-Allow-Headers": "Content-Type",
19-
"Access-Control-Max-Age": "3600",
20-
}
21-
22-
return ("", 204, headers)
23-
24-
# Set CORS headers for the main request
12+
return respond_cors()
13+
2514
headers = {
2615
"Access-Control-Allow-Origin": "*",
2716
"cache-control": "public, max-age=21600"

functions/page-weight/libs/network.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
"""
3+
Network
4+
5+
Handles formatting responses to match the tuple pattern required by
6+
the flask/GCP wrapper for Cloud Functions.
7+
"""
8+
9+
PREFLIGHT_HEADERS = {
10+
"Access-Control-Allow-Origin": "*",
11+
"Access-Control-Allow-Methods": "GET",
12+
"Access-Control-Allow-Headers": "Content-Type",
13+
"Access-Control-Max-Age": "3600",
14+
}
15+
16+
HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}
17+
18+
def respond_cors():
19+
"""
20+
To be used to return OPTIONS responses to satisfy CORS preflight requests.
21+
"""
22+
return ("", 204, PREFLIGHT_HEADERS)
23+
24+
def respond(data, status=200):
25+
"""
26+
To be used to return responses to satisfy CORS requests.
27+
"""
28+
return (data, status, HEADERS)

0 commit comments

Comments
 (0)