/
main.py
172 lines (131 loc) · 5.15 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START functions_http_xml]
import json
# [END functions_http_xml]
# [START functions_http_form_data]
import os
import tempfile
# [END functions_http_form_data]
# [START functions_http_cors]
# [START functions_http_cors_auth]
# [START functions_http_xml]
# [START functions_http_form_data]
import functions_framework
# [END functions_http_form_data]
# [END functions_http_xml]
# [END functions_http_cors_auth]
# [END functions_http_cors]
# [START functions_http_form_data]
from werkzeug.utils import secure_filename
# [END functions_http_form_data]
# [START functions_http_xml]
import xmltodict
# [END functions_http_xml]
# [START functions_http_xml]
@functions_framework.http
def parse_xml(request):
"""Parses a document of type 'text/xml'
Args:
request (flask.Request): The request object.
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`
<http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
"""
data = xmltodict.parse(request.data)
return json.dumps(data, indent=2)
# [END functions_http_xml]
# [START functions_http_form_data]
# Helper function that computes the filepath to save files to
def get_file_path(filename):
# Note: tempfile.gettempdir() points to an in-memory file system
# on GCF. Thus, any files in it must fit in the instance's memory.
file_name = secure_filename(filename)
return os.path.join(tempfile.gettempdir(), file_name)
@functions_framework.http
def parse_multipart(request):
"""Parses a 'multipart/form-data' upload request
Args:
request (flask.Request): The request object.
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`
<http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
"""
# This code will process each non-file field in the form
fields = {}
data = request.form.to_dict()
for field in data:
fields[field] = data[field]
print("Processed field: %s" % field)
# This code will process each file uploaded
files = request.files.to_dict()
for file_name, file in files.items():
# Note: GCF may not keep files saved locally between invocations.
# If you want to preserve the uploaded files, you should save them
# to another location (such as a Cloud Storage bucket).
file.save(get_file_path(file_name))
print("Processed file: %s" % file_name)
# Clear temporary directory
for file_name in files:
file_path = get_file_path(file_name)
os.remove(file_path)
return "Done!"
# [END functions_http_form_data]
# [START functions_http_cors]
@functions_framework.http
def cors_enabled_function(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
headers = {"Access-Control-Allow-Origin": "*"}
return ("Hello World!", 200, headers)
# [END functions_http_cors]
# [START functions_http_cors_auth]
@functions_framework.http
def cors_enabled_function_auth(request):
# For more information about CORS and CORS preflight requests, see
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
# for more information.
# Set CORS headers for preflight requests
if request.method == "OPTIONS":
# Allows GET requests from origin https://mydomain.com with
# Authorization header
headers = {
"Access-Control-Allow-Origin": "https://mydomain.com",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Authorization",
"Access-Control-Max-Age": "3600",
"Access-Control-Allow-Credentials": "true",
}
return ("", 204, headers)
# Set CORS headers for main requests
headers = {
"Access-Control-Allow-Origin": "https://mydomain.com",
"Access-Control-Allow-Credentials": "true",
}
return ("Hello World!", 200, headers)
# [END functions_http_cors_auth]