Skip to content

Commit

Permalink
Enable binary responses in the http-templates
Browse files Browse the repository at this point in the history
This was not possible previously, and resulted in a byte array
being converted to a string and creating an invalid file.

To opt in, set the content type appropriately as:
application/octet-stream.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Jul 24, 2020
1 parent 0ca73dc commit 9fb5527
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 44 deletions.
54 changes: 33 additions & 21 deletions template/python3-http-armhf/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,50 @@ class Context:
def __init__(self):
self.hostname = os.getenv('HOSTNAME', 'localhost')

def format_status_code(resp):
if 'statusCode' in resp:
return resp['statusCode']
def format_status_code(res):
if 'statusCode' in res:
return res['statusCode']

return 200

def format_body(resp):
if 'body' not in resp:
def format_body(res, content_type):
if content_type == 'application/octet-stream':
return res['body']

if 'body' not in res:
return ""
elif type(resp['body']) == dict:
return jsonify(resp['body'])
elif type(res['body']) == dict:
return jsonify(res['body'])
else:
return str(resp['body'])
return str(res['body'])

def format_headers(resp):
if 'headers' not in resp:
def format_headers(res):
if 'headers' not in res:
return []
elif type(resp['headers']) == dict:
elif type(res['headers']) == dict:
headers = []
for key in resp['headers'].keys():
header_tuple = (key, resp['headers'][key])
for key in res['headers'].keys():
header_tuple = (key, res['headers'][key])
headers.append(header_tuple)
return headers

return resp['headers']
return res['headers']

def get_content_type(res):
content_type = ""
if 'headers' in res:
content_type = res['headers'].get('Content-type', '')
return content_type

def format_response(resp):
if resp == None:
def format_response(res):
if res == None:
return ('', 200)

statusCode = format_status_code(resp)
body = format_body(resp)
headers = format_headers(resp)
statusCode = format_status_code(res)
content_type = get_content_type(res)
body = format_body(res, content_type)

headers = format_headers(res)

return (body, statusCode, headers)

Expand All @@ -60,10 +71,11 @@ def format_response(resp):
def call_handler(path):
event = Event()
context = Context()

response_data = handler.handle(event, context)

resp = format_response(response_data)
return resp
res = format_response(response_data)
return res

if __name__ == '__main__':
serve(app, host='0.0.0.0', port=5000)
58 changes: 35 additions & 23 deletions template/python3-http-debian/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,52 @@ def __init__(self):

class Context:
def __init__(self):
self.hostname = os.environ['HOSTNAME']
self.hostname = os.getenv('HOSTNAME', 'localhost')

def format_status_code(resp):
if 'statusCode' in resp:
return resp['statusCode']
def format_status_code(res):
if 'statusCode' in res:
return res['statusCode']

return 200

def format_body(resp):
if 'body' not in resp:
def format_body(res, content_type):
if content_type == 'application/octet-stream':
return res['body']

if 'body' not in res:
return ""
elif type(resp['body']) == dict:
return jsonify(resp['body'])
elif type(res['body']) == dict:
return jsonify(res['body'])
else:
return str(resp['body'])
return str(res['body'])

def format_headers(resp):
if 'headers' not in resp:
def format_headers(res):
if 'headers' not in res:
return []
elif type(resp['headers']) == dict:
elif type(res['headers']) == dict:
headers = []
for key in resp['headers'].keys():
header_tuple = (key, resp['headers'][key])
for key in res['headers'].keys():
header_tuple = (key, res['headers'][key])
headers.append(header_tuple)
return headers

return resp['headers']
return res['headers']

def get_content_type(res):
content_type = ""
if 'headers' in res:
content_type = res['headers'].get('Content-type', '')
return content_type

def format_response(resp):
if resp == None:
def format_response(res):
if res == None:
return ('', 200)

statusCode = format_status_code(resp)
body = format_body(resp)
headers = format_headers(resp)
statusCode = format_status_code(res)
content_type = get_content_type(res)
body = format_body(res, content_type)

headers = format_headers(res)

return (body, statusCode, headers)

Expand All @@ -60,10 +71,11 @@ def format_response(resp):
def call_handler(path):
event = Event()
context = Context()

response_data = handler.handle(event, context)

resp = format_response(response_data)
return resp
res = format_response(response_data)
return res

if __name__ == '__main__':
serve(app, host='0.0.0.0', port=5000)
serve(app, host='0.0.0.0', port=5000)

0 comments on commit 9fb5527

Please sign in to comment.