Skip to content

Commit

Permalink
Support multiple return types in the handler
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
welteki authored and alexellis committed Jan 11, 2024
1 parent 0cf62f9 commit 686f769
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
13 changes: 8 additions & 5 deletions template/python3-http-debian/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('/<path:path>', methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE'])
Expand Down
11 changes: 7 additions & 4 deletions template/python3-http/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('/<path:path>', methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE'])
Expand Down

0 comments on commit 686f769

Please sign in to comment.