How to use the fdk.response.Response 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 / samples / echo / func.py View on Github external
def handler(ctx, data: io.BytesIO=None):
    name = "World"
    try:
        body = json.loads(data.getvalue())
        name = body.get("name")
    except (Exception, ValueError) as ex:
        print(str(ex))
        pass

    return response.Response(
        ctx, status_code=202, response_data=json.dumps(
            {"message": "Hello {0}".format(name)}),
        headers={"Content-Type": "application/json"}
    )
github fnproject / fdk-python / fdk / runner.py View on Github external
:param handler_code: customer's code
    :type handler_code: fdk.customer_code.Function
    :param format_def: function's format
    :type format_def: str
    :param kwargs: request-specific parameters
    :type kwargs: dict
    :return: function's response
    :rtype: fdk.response.Response
    """
    log.log("in handle_request")
    ctx, body = context.context_from_format(format_def, **kwargs)
    log.log("context provisioned")
    try:
        response_data = await with_deadline(ctx, handler_code, body)
        log.log("function result obtained")
        if isinstance(response_data, response.Response):
            return response_data

        headers = ctx.GetResponseHeaders()
        log.log("response headers obtained")
        return response.Response(
            ctx, response_data=response_data,
            headers=headers, status_code=200)

    except (Exception, TimeoutError) as ex:
        log.log("exception appeared: {0}".format(ex))
        traceback.print_exc(file=sys.stderr)
        return errors.DispatchException(ctx, 502, str(ex)).response()
github fnproject / fdk-python / fdk / runner.py View on Github external
:type kwargs: dict
    :return: function's response
    :rtype: fdk.response.Response
    """
    log.log("in handle_request")
    ctx, body = context.context_from_format(format_def, **kwargs)
    log.log("context provisioned")
    try:
        response_data = await with_deadline(ctx, handler_code, body)
        log.log("function result obtained")
        if isinstance(response_data, response.Response):
            return response_data

        headers = ctx.GetResponseHeaders()
        log.log("response headers obtained")
        return response.Response(
            ctx, response_data=response_data,
            headers=headers, status_code=200)

    except (Exception, TimeoutError) as ex:
        log.log("exception appeared: {0}".format(ex))
        traceback.print_exc(file=sys.stderr)
        return errors.DispatchException(ctx, 502, str(ex)).response()
github fnproject / fdk-python / fdk / errors.py View on Github external
def response(self):
        resp_headers = {
            constants.CONTENT_TYPE: "application/json; charset=utf-8",
        }
        return response.Response(
            self.ctx,
            response_data=self.message,
            headers=resp_headers,
            status_code=self.status
        )