How to use the fdk.headers 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 / json / request.py View on Github external
def parse_raw_request(self):
        """
        Parses raw JSON request into its context and body
        :return: tuple of request context and body
        :rtype: tuple
        """
        if self.stream is None:
            raise EOFError("Previous stream had no terminator")
        try:
            incoming_json = readline(self.stream)
            print("After JSON parsing: {}".format(incoming_json),
                  file=sys.stderr, flush=True)
            json_headers = headers.GoLikeHeaders(
                incoming_json.get('protocol', {"headers": {}}).get("headers"))
            ctx = context.JSONContext(os.environ.get("FN_APP_NAME"),
                                      os.environ.get("FN_PATH"),
                                      incoming_json.get("call_id"),
                                      execution_type=incoming_json.get(
                                          "type", "sync"),
                                      deadline=incoming_json.get("deadline"),
                                      config=os.environ, headers=json_headers)
            return ctx, incoming_json.get('body')
        except Exception as ex:
            print("Error while parsing JSON: {}".format(str(ex)),
                  file=sys.stderr, flush=True)
            ctx = context.JSONContext(
                os.environ.get("FN_APP_NAME"),
                os.environ.get("FN_PATH"), "",
            )
github fnproject / fdk-python / fdk / json / handle.py View on Github external
:param context: request context
    :type context: request.RequestContext
    :param data: request body
    :type data: io.BufferedIOBase
    :param loop: asyncio event loop
    :type loop: asyncio.AbstractEventLoop
    :return: raw response
    :rtype: response.RawResponse
    """
    rs = app(context, data=data, loop=loop)
    if isinstance(rs, response.RawResponse):
        return rs
    elif isinstance(rs, types.CoroutineType):
        return loop.run_until_complete(rs)
    elif isinstance(rs, str):
        hs = headers.GoLikeHeaders({})
        hs.set('content-type', 'text/plain')
        return response.RawResponse(context, response_data=rs)
    elif isinstance(rs, bytes):
        hs = headers.GoLikeHeaders({})
        hs.set('content-type', 'application/octet-stream')
        return response.RawResponse(
            context,
            response_data=rs.decode("utf8"),
            headers=hs,
            status_code=200
        )
    else:
        hs = headers.GoLikeHeaders({})
        hs.set('content-type', 'application/json')
        return response.RawResponse(
            context,
github fnproject / fdk-python / fdk / json / handle.py View on Github external
:param loop: asyncio event loop
    :type loop: asyncio.AbstractEventLoop
    :return: raw response
    :rtype: response.RawResponse
    """
    rs = app(context, data=data, loop=loop)
    if isinstance(rs, response.RawResponse):
        return rs
    elif isinstance(rs, types.CoroutineType):
        return loop.run_until_complete(rs)
    elif isinstance(rs, str):
        hs = headers.GoLikeHeaders({})
        hs.set('content-type', 'text/plain')
        return response.RawResponse(context, response_data=rs)
    elif isinstance(rs, bytes):
        hs = headers.GoLikeHeaders({})
        hs.set('content-type', 'application/octet-stream')
        return response.RawResponse(
            context,
            response_data=rs.decode("utf8"),
            headers=hs,
            status_code=200
        )
    else:
        hs = headers.GoLikeHeaders({})
        hs.set('content-type', 'application/json')
        return response.RawResponse(
            context,
            response_data=ujson.dumps(rs),
            headers=hs,
            status_code=200,
        )
github fnproject / fdk-python / fdk / json / response.py View on Github external
def __init__(self, response_data=None, headers=None, status_code=200):
        """
        JSON response object
        :param response_data: JSON response data (dict, str)
        :type response_data: object
        :param headers: JSON response HTTP headers
        :type headers: fdk.headers.GoLikeHeaders
        :param status_code: JSON response HTTP status code
        :type status_code: int
        """
        self.status_code = status_code
        self.response_data = ujson.dumps(response_data)
        self.headers = rh.GoLikeHeaders({})
        if isinstance(headers, dict):
            self.headers = rh.GoLikeHeaders(headers)
        if isinstance(headers, rh.GoLikeHeaders):
            self.headers = headers
github fnproject / fdk-python / fdk / json / response.py View on Github external
def __init__(self, response_data=None, headers=None, status_code=200):
        """
        JSON response object
        :param response_data: JSON response data (dict, str)
        :type response_data: object
        :param headers: JSON response HTTP headers
        :type headers: fdk.headers.GoLikeHeaders
        :param status_code: JSON response HTTP status code
        :type status_code: int
        """
        self.status_code = status_code
        self.response_data = ujson.dumps(response_data)
        self.headers = rh.GoLikeHeaders({})
        if isinstance(headers, dict):
            self.headers = rh.GoLikeHeaders(headers)
        if isinstance(headers, rh.GoLikeHeaders):
            self.headers = headers
github fnproject / fdk-python / fdk / context.py View on Github external
self.__call_id = call_id
        self.__config = config if config else {}
        self.__headers = headers if headers else {}
        self.__http_headers = {}
        self.__deadline = deadline
        self.__content_type = content_type
        self._request_url = request_url
        self._method = method
        self.__response_headers = {}
        self.__fn_format = fn_format

        log.log("request headers. gateway: {0} {1}"
                .format(self.__is_gateway(), headers))

        if self.__is_gateway():
            self.__headers = hs.decap_headers(headers, True)
            self.__http_headers = hs.decap_headers(headers, False)