How to use the fdk.async_http.response.HTTPResponse function in fdk

To help you get started, we’ve selected a few fdk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fnproject / fdk-python / fdk / async_http / response.py View on Github external
def text(
    body, status=200, headers=None, content_type="text/plain; charset=utf-8"
):
    """
    Returns response object with body in text format.

    :param body: Response data to be encoded.
    :param status: Response code.
    :param headers: Custom Headers.
    :param content_type: the content type (string) of the response
    """
    return HTTPResponse(
        body, status=status, headers=headers, content_type=content_type
    )
github fnproject / fdk-python / fdk / async_http / app.py View on Github external
# -------------------------------------------- #
            # Execute Handler
            # -------------------------------------------- #

            # Fetch handler from router
            handler, uri = self.router.get(
                request.path, request.method)

            request.uri_template = uri
            response = handler(request)
            logger.info("got response from function")
            res = await response
            body = res.body
            headers = res.headers
            status = res.status
            response = HTTPResponse(
                body_bytes=body, status=status, headers=headers,
            )
        except CancelledError:
            response = None
            cancelled = True
        except Exception as e:
            if isinstance(e, AsyncHTTPException):
                response = self.error_handler.default(
                    request=request, exception=e
                )
            elif self.debug:
                response = HTTPResponse(
                    "Error while handling error: {}\nStack: {}".format(
                        e, format_exc()
                    ),
                    status=502,
github fnproject / fdk-python / fdk / async_http / response.py View on Github external
:param to: path or fully qualified URL to redirect to
    :param headers: optional dict of headers to include in the new request
    :param status: status code (int) of the new request, defaults to 302
    :param content_type: the content type (string) of the response
    :returns: the redirecting Response
    """
    headers = headers or {}

    # URL Quote the URL before redirecting
    safe_to = quote_plus(to, safe=":/%#?&=@[]!$&'()*+,;")

    # According to RFC 7231, a relative URI is now permitted.
    headers["Location"] = safe_to

    return HTTPResponse(
        status=status, headers=headers, content_type=content_type
    )
github fnproject / fdk-python / fdk / async_http / response.py View on Github external
body,
    status=200,
    headers=None,
    content_type="application/json",
    dumps=json_dumps,
    **kwargs
):
    """
    Returns response object with body in json format.

    :param body: Response data to be serialized.
    :param status: Response code.
    :param headers: Custom Headers.
    :param kwargs: Remaining arguments that are passed to the json encoder.
    """
    return HTTPResponse(
        dumps(body, **kwargs),
        headers=headers,
        status=status,
        content_type=content_type,
    )
github fnproject / fdk-python / fdk / event_handler.py View on Github external
async def pure_handler(request):
        from fdk import runner
        logger.info("in pure_handler")
        headers = dict(request.headers)
        log_frame_header(headers)
        func_response = await runner.handle_request(
            handle_code, constants.HTTPSTREAM,
            headers=headers, data=io.BytesIO(request.body))
        logger.info("request execution completed")

        headers = func_response.context().GetResponseHeaders()
        status = func_response.status()
        if status not in constants.FN_ENFORCED_RESPONSE_CODES:
            status = constants.FN_DEFAULT_RESPONSE_CODE

        return response.HTTPResponse(
            headers=headers,
            status=status,
            content_type=headers.get(constants.CONTENT_TYPE),
            body_bytes=func_response.body_bytes(),
        )
github fnproject / fdk-python / fdk / async_http / response.py View on Github external
def html(body, status=200, headers=None):
    """
    Returns response object with body in html format.

    :param body: Response data to be encoded.
    :param status: Response code.
    :param headers: Custom Headers.
    """
    return HTTPResponse(
        body,
        status=status,
        headers=headers,
        content_type="text/html; charset=utf-8",
    )
github fnproject / fdk-python / fdk / async_http / response.py View on Github external
def raw(
    body, status=200, headers=None, content_type="application/octet-stream"
):
    """
    Returns response object without encoding the body.

    :param body: Response data.
    :param status: Response code.
    :param headers: Custom Headers.
    :param content_type: the content type (string) of the response.
    """
    return HTTPResponse(
        body_bytes=body,
        status=status,
        headers=headers,
        content_type=content_type,
    )
github fnproject / fdk-python / fdk / async_http / app.py View on Github external
response = None
            cancelled = True
        except Exception as e:
            if isinstance(e, AsyncHTTPException):
                response = self.error_handler.default(
                    request=request, exception=e
                )
            elif self.debug:
                response = HTTPResponse(
                    "Error while handling error: {}\nStack: {}".format(
                        e, format_exc()
                    ),
                    status=502,
                )
            else:
                response = HTTPResponse(
                    "An error occurred while handling an error", status=502
                )
        finally:
            if cancelled:
                raise CancelledError()

        if isinstance(response, StreamingHTTPResponse):
            await stream_callback(response)
        else:
            write_callback(response)
github fnproject / fdk-python / fdk / async_http / app.py View on Github external
body = res.body
            headers = res.headers
            status = res.status
            response = HTTPResponse(
                body_bytes=body, status=status, headers=headers,
            )
        except CancelledError:
            response = None
            cancelled = True
        except Exception as e:
            if isinstance(e, AsyncHTTPException):
                response = self.error_handler.default(
                    request=request, exception=e
                )
            elif self.debug:
                response = HTTPResponse(
                    "Error while handling error: {}\nStack: {}".format(
                        e, format_exc()
                    ),
                    status=502,
                )
            else:
                response = HTTPResponse(
                    "An error occurred while handling an error", status=502
                )
        finally:
            if cancelled:
                raise CancelledError()

        if isinstance(response, StreamingHTTPResponse):
            await stream_callback(response)
        else: