in src/gcf/main.py [0:0]
def annotate_http(request):
"""HTTP Cloud Function.
Args:
request (flask.Request): The request object.
<https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
Returns:
The image anotation response as JSON or an HTTP error.
Response object is AnnotateImageResponse,
explained in: <https://cloud.google.com/vision/docs/reference/rest/v1/AnnotateImageResponse>
"""
# return display_gcs_http(request)
# move this into the global section
# log_level = os.environ.get("LOG_LEVEL", "WARNING")
# logging.getLogger().setLevel(log_level)
# 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, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}
return ("", 204, headers)
logging.info(
"REST API request path=%s, args=%s, form=%s",
request.path,
request.args,
request.form,
)
response = None
path_items = request.path.split("/")
if len(path_items) >= 2:
if path_items[1].lower() == "annotate":
response = handle_annotation(request)
elif path_items[1].lower() == "bucket" and request.method == "GET":
response = handle_bucket(request)
if not response:
response = make_response("Not supported.", 501)
# Set CORS headers for the main request
response.headers["Access-Control-Allow-Origin"] = "*"
logging.info("REST API response=%s", response.status_code)
return response