The python3-http templates use an Event abstraction which represents the incoming request.
Currently the Event only holds the request.data as body, which is the body as byte array.
Suggestion: Leverage Flask's support for multipart/form-data, application/json (and x-www-form-urlencoded) by adding the following attributes to the Event object:
request.files and request.form - they are needed for multipart/form-data requests.
The request.files attribute is an ImmutableMultiDict of FileStorage (which is a file-ish ducktype) where every entry is a list of files.
The request.form is an ImmutableMultiDict of strings which represent the textual multiparts which have no filename in their content-disposition (or the values of an x-www-form-urlencoded body).
We already preserve ImmutableMultiDict in Event.query, so it seems straightforward to add files and form, too
request.get_json() - it only gets filled when the request is of Content-Type: application/json
class Event:
def __init__(self):
self.body = request.get_data()
self.json = request.get_json()
self.form = request.form
self.files = request.files
self.headers = request.headers
self.method = request.method
self.query = request.args
self.path = request.path
The format_body method also needs to be adjusted so that it uses Flask's support for file responses if the handler returns a file.
return send_file(fout, 'application/pdf')
This gives us the ability to upload files to such functions and download the function result (would also enable us to have uploads in the OpenFaaS Dashboard). Depending on Flask's implementation this also gives us support for large files.
I am preparing a PR for the python3-http template, and I could do likewise for the IRequest in the Java Template.
Please let me know if you approve of the general idea.
The python3-http templates use an
Eventabstraction which represents the incoming request.Currently the
Eventonly holds therequest.dataas body, which is the body as byte array.Suggestion: Leverage Flask's support for multipart/form-data, application/json (and x-www-form-urlencoded) by adding the following attributes to the
Eventobject:request.filesandrequest.form- they are needed formultipart/form-datarequests.The
request.filesattribute is anImmutableMultiDictofFileStorage(which is a file-ish ducktype) where every entry is a list of files.The
request.formis anImmutableMultiDictof strings which represent the textual multiparts which have no filename in their content-disposition (or the values of an x-www-form-urlencoded body).We already preserve ImmutableMultiDict in Event.query, so it seems straightforward to add
filesandform, toorequest.get_json()- it only gets filled when the request is ofContent-Type: application/jsonThe format_body method also needs to be adjusted so that it uses Flask's support for file responses if the handler returns a file.
This gives us the ability to upload files to such functions and download the function result (would also enable us to have uploads in the OpenFaaS Dashboard). Depending on Flask's implementation this also gives us support for large files.
I am preparing a PR for the python3-http template, and I could do likewise for the IRequest in the Java Template.
Please let me know if you approve of the general idea.