Skip to content

Conversation

@welteki
Copy link
Member

@welteki welteki commented Oct 25, 2023

Description

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 formatted like before when a dict object type is returned by the handler.

Motivation and Context

  • I have raised an issue to propose this change (required)
  • My issue has received approval from the maintainers or lead with the design/approved label

Returning large files and binary response was not supported by the python3-http template.

How Has This Been Tested?

Tested manually by converting a function example that creates a video-preview for a function using FFmpeg and returns the resulting video.

import os
import json
import logging
import tempfile
import ffmpeg
from flask import send_file

from .preview import generate_video_preview, calculate_sample_seconds

debug = os.getenv("debug", "false").lower() == "true"

def handle(event, context):
    request_data = json.loads(event.body)

    data, status_code, message = parse_request(request_data)
    if data is None:
        return {
            "statusCode": status_code,
            "body": message
        }

    input_url = data["input_url"]
    sample_duration = data["sample_duration"]
    sample_seconds = data["sample_seconds"]
    scale = data["scale"]
    format = data["format"]

    out_file = tempfile.NamedTemporaryFile(delete=True)
    
    # Generate video preview
    try:
        generate_video_preview(input_url, out_file.name, sample_duration, sample_seconds, scale, format, quiet=not debug)
    except Exception as e:
        logging.error(e)
        return {
            "statusCode": 500,
            "body": "failed to generate video preview"
        }

    return send_file(out_file.name)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I've read the CONTRIBUTION guide
  • I have signed-off my commits with git commit -s
  • I have added tests to cover my changes.
  • All new and existing tests passed.

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>
@alexellis alexellis merged commit 686f769 into openfaas:master Jan 11, 2024
@welteki welteki deleted the support-multiple-return-types branch January 11, 2024 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants