From 686f769b763fd69586feeeb5f33e1290d1453548 Mon Sep 17 00:00:00 2001 From: "Han Verstraete (OpenFaaS Ltd)" Date: Wed, 25 Oct 2023 16:28:44 +0200 Subject: [PATCH] Support multiple return types in the handler Allow users to use the Flask Response object and other functions like `send_file`. This makes it possible to stream a response or return files. For backwards compatibility the response is still formated like before when a dict object type is returned by the handler. Signed-off-by: Han Verstraete (OpenFaaS Ltd) --- template/python3-http-debian/index.py | 13 ++++++++----- template/python3-http/index.py | 11 +++++++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/template/python3-http-debian/index.py b/template/python3-http-debian/index.py index 2e2ec70..74340ba 100644 --- a/template/python3-http-debian/index.py +++ b/template/python3-http-debian/index.py @@ -57,14 +57,17 @@ def get_content_type(res): def format_response(res): if res == None: return ('', 200) + + if type(resp) is dict: + statusCode = format_status_code(res) + content_type = get_content_type(res) + body = format_body(res, content_type) - statusCode = format_status_code(res) - content_type = get_content_type(res) - body = format_body(res, content_type) + headers = format_headers(res) - headers = format_headers(res) + return (body, statusCode, headers) - return (body, statusCode, headers) + return res @app.route('/', defaults={'path': ''}, methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE']) @app.route('/', methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE']) diff --git a/template/python3-http/index.py b/template/python3-http/index.py index 5d2f33b..ac82b90 100644 --- a/template/python3-http/index.py +++ b/template/python3-http/index.py @@ -48,12 +48,15 @@ def format_headers(resp): def format_response(resp): if resp == None: return ('', 200) + + if type(resp) is dict: + statusCode = format_status_code(resp) + body = format_body(resp) + headers = format_headers(resp) - statusCode = format_status_code(resp) - body = format_body(resp) - headers = format_headers(resp) + return (body, statusCode, headers) - return (body, statusCode, headers) + return resp @app.route('/', defaults={'path': ''}, methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE']) @app.route('/', methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE'])