|
5 | 5 | Handles formatting responses to match the tuple pattern required by
|
6 | 6 | the flask/GCP wrapper for Cloud Functions.
|
7 | 7 | """
|
| 8 | +import json |
| 9 | +from .utils import convert_to_hashes |
8 | 10 |
|
9 | 11 | PREFLIGHT_HEADERS = {
|
10 | 12 | "Access-Control-Allow-Origin": "*",
|
11 | 13 | "Access-Control-Allow-Methods": "GET",
|
12 |
| - "Access-Control-Allow-Headers": "Content-Type", |
| 14 | + "Access-Control-Allow-Headers": "Content-Type, Timing-Allow-Origin", |
13 | 15 | "Access-Control-Max-Age": "3600",
|
14 | 16 | }
|
15 | 17 |
|
16 |
| -HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"} |
| 18 | +HEADERS = { |
| 19 | + "Access-Control-Allow-Origin": "*", |
| 20 | + "Content-Type": "application/json", |
| 21 | + "cache-control": "public, max-age=21600", |
| 22 | + "Timing-Allow-Origin": "*" |
| 23 | + } |
17 | 24 |
|
18 | 25 | def respond_cors():
|
19 | 26 | """
|
20 | 27 | To be used to return OPTIONS responses to satisfy CORS preflight requests.
|
21 | 28 | """
|
22 | 29 | return ("", 204, PREFLIGHT_HEADERS)
|
23 | 30 |
|
24 |
| -def respond(data, status=200): |
| 31 | +def respond(result, headers=HEADERS): |
25 | 32 | """
|
26 | 33 | To be used to return responses to satisfy CORS requests.
|
27 | 34 | """
|
28 |
| - return (data, status, HEADERS) |
| 35 | + status = 200 if result.success() else 400 |
| 36 | + payload = result.result if result.success() else convert_to_hashes(result.errors) |
| 37 | + return (json.dumps(payload), status, headers) |
0 commit comments