Skip to content

Commit 686f769

Browse files
weltekialexellis
authored andcommitted
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) <han@openfaas.com>
1 parent 0cf62f9 commit 686f769

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

template/python3-http-debian/index.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,17 @@ def get_content_type(res):
5757
def format_response(res):
5858
if res == None:
5959
return ('', 200)
60+
61+
if type(resp) is dict:
62+
statusCode = format_status_code(res)
63+
content_type = get_content_type(res)
64+
body = format_body(res, content_type)
6065

61-
statusCode = format_status_code(res)
62-
content_type = get_content_type(res)
63-
body = format_body(res, content_type)
66+
headers = format_headers(res)
6467

65-
headers = format_headers(res)
68+
return (body, statusCode, headers)
6669

67-
return (body, statusCode, headers)
70+
return res
6871

6972
@app.route('/', defaults={'path': ''}, methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE'])
7073
@app.route('/<path:path>', methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE'])

template/python3-http/index.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ def format_headers(resp):
4848
def format_response(resp):
4949
if resp == None:
5050
return ('', 200)
51+
52+
if type(resp) is dict:
53+
statusCode = format_status_code(resp)
54+
body = format_body(resp)
55+
headers = format_headers(resp)
5156

52-
statusCode = format_status_code(resp)
53-
body = format_body(resp)
54-
headers = format_headers(resp)
57+
return (body, statusCode, headers)
5558

56-
return (body, statusCode, headers)
59+
return resp
5760

5861
@app.route('/', defaults={'path': ''}, methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE'])
5962
@app.route('/<path:path>', methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE'])

0 commit comments

Comments
 (0)