File tree Expand file tree Collapse file tree 2 files changed +30
-13
lines changed Expand file tree Collapse file tree 2 files changed +30
-13
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change 2
2
from .libs .validator import Validator
3
3
from .libs .utils import output
4
4
from .libs .queries import list_data
5
+ from .libs .network import respond_cors , respond
5
6
6
7
@functions_framework .http
7
8
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
9
11
- # Set CORS headers for the preflight request
12
10
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 )
11
+ return respond_cors ()
23
12
24
13
# Set CORS headers for the main request
25
14
headers = {"Access-Control-Allow-Origin" : "*" }
You can’t perform that action at this time.
0 commit comments