How to use the fdk.context 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 / request.py View on Github external
# todo: parse query parameters carefully
            params = parse_query_params(path)

            if headers.get('transfer-encoding', 'identity') == 'chunked':
                self.body_stream = ChunkedStream(self.stream)
            elif 'content-length' in headers:
                self.body_stream = ContentLengthStream(
                    self.stream, int(headers.get("content-length")))
            else:
                # With no way of knowing when the input is complete,
                # we must read everything remaining
                self.body_stream = self.stream
                self.stream = None

            ctx = context.HTTPContext(
                os.environ.get("FN_APP_NAME"),
                os.environ.get("FN_PATH"),
                headers.get('fn_call_id'),
                deadline=headers.get("fn_deadline"),
                config=os.environ,
                method=method,
                url=path,
                query_parameters=params,
                headers=headers,
                version=(major, minor))

            return ctx, self.body_stream
        except ValueError:
            ctx = context.HTTPContext(
                os.environ.get("FN_APP_NAME"),
                os.environ.get("FN_PATH"), "",
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"), "",
            )
            raise errors.JSONDispatchException(ctx, 500, str(ex))
github fnproject / fdk-python / fdk / http / request.py View on Github external
ctx = context.HTTPContext(
                os.environ.get("FN_APP_NAME"),
                os.environ.get("FN_PATH"),
                headers.get('fn_call_id'),
                deadline=headers.get("fn_deadline"),
                config=os.environ,
                method=method,
                url=path,
                query_parameters=params,
                headers=headers,
                version=(major, minor))

            return ctx, self.body_stream
        except ValueError:
            ctx = context.HTTPContext(
                os.environ.get("FN_APP_NAME"),
                os.environ.get("FN_PATH"), "",
            )
            raise errors.HTTPDispatchException(ctx, 500, "No request supplied")
github fnproject / fdk-python / fdk / runner.py View on Github external
async def handle_request(handler_code, format_def, **kwargs):
    """
    Handles a function's request
    :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)