openfaas / python-flask-template

HTTP and Flask-based OpenFaaS templates for Python 3
MIT License
85 stars 86 forks source link

pass complete flask request to handler.py #17

Closed dschulten closed 5 years ago

dschulten commented 5 years ago

Passing the entire request to the handler opens a number of possibilities, among them support for multipart/form-data. Based on this change, I can create a function that accepts multiple pdf files as multipart/form-data and joins them into a single pdf:

from PyPDF2 import PdfFileMerger, PdfFileReader
from flask import send_file
import tempfile

def handle(req):
    """handle a request to the function
    Args:
        req (request): flask request
    """
    files = req.files
    file_storages = files.getlist("files")
    merger = PdfFileMerger()

    for file_storage in file_storages:
        pdfFileReader = PdfFileReader(file_storage)
        merger.append(pdfFileReader)

    fout = tempfile.TemporaryFile()
    merger.write(fout)

    for file_storage in file_storages:
        file_storage.close()

    fout.seek(0)
    return send_file(fout, 'application/pdf')
derek[bot] commented 5 years ago

Thank you for your contribution. I've just checked and your commit doesn't appear to be signed-off. That's something we need before your Pull Request can be merged. Please see our contributing guide. Tip: if you only have one commit so far then run: git commit --amend --sign-off and then git push --force.

alexellis commented 5 years ago

Hi @dschulten as nice as this sounds, it's a breaking change and can't be merged due to the effect on our existing users.

Btw. all changes must be proposed through an issue and approved before being worked on.

Alex