How to use the fdk.constants.CONTENT_TYPE 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 / http_stream.py View on Github external
log.log("in pure_handler")
        data = None
        if request.has_body:
            log.log("has body: {}".format(request.has_body))
            log.log("request comes with data")
            data = await request.content.read()
        response = await runner.handle_request(
            handle_func, constants.HTTPSTREAM,
            request=request, data=data)
        log.log("request execution completed")
        headers = response.context().GetResponseHeaders()

        response_content_type = headers.get(
            constants.CONTENT_TYPE, "application/json"
        )
        headers.set(constants.CONTENT_TYPE, response_content_type)
        kwargs = {
            "headers": headers.http_raw()
        }

        sdata = serialize_response_data(
            response.body(), response_content_type)

        if response.status() >= 500:
            kwargs.update(reason=sdata, status=500)
        else:
            kwargs.update(body=sdata, status=200)

        log.log("sending response back")
        try:
            resp = web.Response(**kwargs)
        except (Exception, BaseException) as ex:
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
        )
github fnproject / fdk-python / fdk / context.py View on Github external
:rtype: tuple
    """

    app_id = os.environ.get(constants.FN_APP_ID)
    fn_id = os.environ.get(constants.FN_ID)

    if format_def == constants.HTTPSTREAM:
        data = kwargs.get("data")
        headers = kwargs.get("headers")

        method = headers.get(constants.FN_HTTP_METHOD)
        request_url = headers.get(
            constants.FN_HTTP_REQUEST_URL)
        deadline = headers.get(constants.FN_DEADLINE)
        call_id = headers.get(constants.FN_CALL_ID)
        content_type = headers.get(constants.CONTENT_TYPE)

        ctx = InvokeContext(
            app_id, fn_id, call_id,
            content_type=content_type,
            deadline=deadline,
            config=os.environ,
            headers=headers,
            method=method,
            request_url=request_url,
            fn_format=constants.HTTPSTREAM,
        )

        return ctx, data
github fnproject / fdk-python / fdk / event_handler.py View on Github external
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 / headers.py View on Github external
def encap_headers(headers, status=None):
    new_headers = {}
    if headers is not None:
        for k, v in headers.items():
            k = k.lower()
            if k.startswith(constants.FN_HTTP_PREFIX):  # by default merge
                push_header(new_headers, k, v)
            if (k == constants.CONTENT_TYPE or
                    k == constants.FN_FDK_VERSION):  # but don't merge these
                new_headers[k] = v
            else:
                push_header(new_headers, constants.FN_HTTP_PREFIX + k, v)

    if status is not None:
        new_headers[constants.FN_HTTP_STATUS] = str(status)

    return new_headers
github fnproject / fdk-python / fdk / http_stream.py View on Github external
async def pure_handler(request):
        log.log("in pure_handler")
        data = None
        if request.has_body:
            log.log("has body: {}".format(request.has_body))
            log.log("request comes with data")
            data = await request.content.read()
        response = await runner.handle_request(
            handle_func, constants.HTTPSTREAM,
            request=request, data=data)
        log.log("request execution completed")
        headers = response.context().GetResponseHeaders()

        response_content_type = headers.get(
            constants.CONTENT_TYPE, "application/json"
        )
        headers.set(constants.CONTENT_TYPE, response_content_type)
        kwargs = {
            "headers": headers.http_raw()
        }

        sdata = serialize_response_data(
            response.body(), response_content_type)

        if response.status() >= 500:
            kwargs.update(reason=sdata, status=500)
        else:
            kwargs.update(body=sdata, status=200)

        log.log("sending response back")
        try:
github fnproject / fdk-python / fdk / http / routine.py View on Github external
async def write_error(ex: Exception,
                      connection: h11.Connection,
                      response_writer: asyncio.StreamWriter):
    """
    Turns an exception into an error
    :param ex: an exception
    :type ex: Exception
    :param connection: h11 request parser
    :type connection: h11.Connection
    :param response_writer: async stream writer
    :type response_writer: asyncio.StreamWriter
    :return: None
    """
    data = str(ex)
    headers = {
        constants.CONTENT_TYPE: "text/plain",
        constants.CONTENT_LENGTH: str(len(data))
    }
    response_writer.write(
        connection.send(h11.Response(
            status_code=502, headers=headers.items())
        )
    )

    response_writer.write(connection.send(
        h11.Data(
            data=data.encode("utf-8")))
    )
    response_writer.write(connection.send(h11.EndOfMessage()))