Beispiel #1
0
 def response(self):
     resp_headers = headers.GoLikeHeaders({})
     resp_headers.set("content-type", "application/json")
     app = os.environ.get("FN_APP_NAME")
     path = os.environ.get("FN_PATH")
     return response.CloudEventResponse(
         context.CloudEventContext(app,
                                   path,
                                   "",
                                   cloudevent={
                                       "cloudEventsVersion": "0.1",
                                       "eventID": uuid.uuid4().hex,
                                       "source": "fdk-python",
                                       "eventType": "fdk-python-error",
                                       "eventTypeVersion": "0.1",
                                       "schemaURL": "",
                                       "contentType": "application/json",
                                       "extensions": {
                                           "protocol": {}
                                       },
                                   }),
         response_data={"error": {
             "message": self.message,
         }},
         headers=resp_headers,
         status_code=self.status)
Beispiel #2
0
 def response(self):
     resp_headers = headers.GoLikeHeaders({})
     resp_headers.set("content-type", "application/json; charset=utf-8")
     return response.HTTPStreamResponse(self.ctx,
                                        response_data=self.message,
                                        headers=resp_headers,
                                        status_code=self.status)
Beispiel #3
0
def context_from_format(format_def, **kwargs) -> (InvokeContext, object):
    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")
        request = kwargs.get("request")

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

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

        return ctx, data
Beispiel #4
0
 def response(self):
     resp_headers = headers.GoLikeHeaders({})
     resp_headers.set("content-type", "text/plain; charset=utf-8")
     return response.JSONResponse(
         None,
         response_data={"error": {
             "message": self.message,
         }},
         headers=resp_headers,
         status_code=self.status)
Beispiel #5
0
 def __init__(self,
              context,
              response_data=None,
              headers=None,
              status_code=200):
     """
     JSON response object
     :param response_data: response data (any JSON-serializable object)
     :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 = response_data if response_data else ""
     self.headers = hrs.GoLikeHeaders({})
     if isinstance(headers, dict):
         self.headers = hrs.GoLikeHeaders(headers)
     if isinstance(headers, hrs.GoLikeHeaders):
         self.headers = headers
Beispiel #6
0
def set_response_headers(current_headers, new_headers,
                         status_code, content_type=None):
    if isinstance(new_headers, dict):
        new_headers = hs.GoLikeHeaders(new_headers)
    elif isinstance(new_headers, hs.GoLikeHeaders):
        pass
    else:
        raise TypeError(
            "Invalid headers type: {}, only dict allowed."
            .format(type(new_headers))
        )

    new_headers = hs.encap_headers(
        new_headers,
        status=status_code,
        content_type=content_type
    )
    for k, v in new_headers.items():
        current_headers.set(k, v)

    return current_headers
Beispiel #7
0
 def __init__(self, app_id, fn_id, call_id,
              content_type="application/octet-stream",
              deadline=None, config=None,
              headers=None, arguments=None,
              request_url=None, method="POST",
              fn_format=None):
     """
     Request context here to be a placeholder
     for request-specific attributes
     """
     self.__app_id = app_id
     self.__fn_id = fn_id
     self.__call_id = call_id
     self.__config = config if config else {}
     self.__headers = headers if headers else {}
     self._arguments = {} if not arguments else arguments
     self.__deadline = deadline
     self.__content_type = content_type
     self._request_url = request_url
     self._method = method
     self.__response_headers = hs.GoLikeHeaders({})
     self.__fn_format = fn_format
Beispiel #8
0
 def __init__(self, invoke_context: InvokeContext):
     self.__headers = hs.decap_headers(invoke_context.Headers())
     self.__invoke_context = invoke_context
     self.__response_headers = hs.GoLikeHeaders({})
Beispiel #9
0
def context_from_format(format_def, stream) -> (RequestContext, object):
    app = os.environ.get("FN_APP_NAME")
    path = os.environ.get("FN_PATH")

    if format_def == "cloudevent":
        incoming_request = parser.read_json(stream)
        call_id = incoming_request.get("eventID")
        content_type = incoming_request.get("contentType")
        extensions = incoming_request.get("extensions")
        deadline = extensions.get("deadline")
        protocol = incoming_request.get(
            "protocol", {
                "headers": {},
                "type": "http",
                "method": "GET",
                "request_url": "http://localhost:8080/r/{0}{1}".format(
                    app, path),
            })
        json_headers = headers.GoLikeHeaders(protocol.get("headers"))
        data = incoming_request.get("data", "{}")
        del incoming_request["data"]

        ctx = CloudEventContext(
            app,
            path,
            call_id,
            content_type=content_type,
            execution_type=os.getenv("FN_TYPE"),
            deadline=deadline,
            config=os.environ,
            headers=json_headers,
            request_url=protocol.get("request_url"),
            cloudevent=incoming_request,
        )
        return ctx, data

    if format_def == "json":
        incoming_request = parser.read_json(stream)
        call_id = incoming_request.get("call_id")

        content_type = incoming_request.get("content_type")
        protocol = incoming_request.get(
            "protocol", {
                "headers": {},
                "type": "http",
                "method": "GET",
                "request_url": "http://localhost:8080/r/{0}{1}".format(
                    app, path),
            })

        json_headers = headers.GoLikeHeaders(protocol.get("headers"))
        call_type = json_headers.get("fn-type", "sync")

        ctx = JSONContext(app,
                          path,
                          call_id,
                          content_type=content_type,
                          execution_type=call_type,
                          deadline=incoming_request.get("deadline"),
                          config=os.environ,
                          headers=json_headers,
                          request_url=protocol.get("request_url"))
        return ctx, incoming_request.get("body", "{}")

    if format_def not in ["cloudevent", "json"]:
        print("incompatible function format!", file=sys.stderr, flush=True)
        sys.exit("incompatible function format!")