Example #1
0
    def view_func(path):
        if event_conversion.is_convertable_cloud_event(request):
            # Convert this CloudEvent to the equivalent background event data and context.
            data, context = event_conversion.cloud_event_to_background_event(
                request)
            function(data, context)
        elif is_binary(request.headers):
            # Support CloudEvents in binary content mode, with data being the
            # whole request body and context attributes retrieved from request
            # headers.
            data = request.get_data()
            context = Context(
                eventId=request.headers.get("ce-eventId"),
                timestamp=request.headers.get("ce-timestamp"),
                eventType=request.headers.get("ce-eventType"),
                resource=request.headers.get("ce-resource"),
            )
            function(data, context)
        else:
            # This is a regular CloudEvent
            event_data = event_conversion.marshal_background_event_data(
                request)
            if not event_data:
                flask.abort(400)
            event_object = BackgroundEvent(**event_data)
            data = event_object.data
            context = Context(**event_object.context)
            function(data, context)

        return "OK"
Example #2
0
def test_marshal_background_event_data_with_topic_path(
        raw_pubsub_request, marshalled_pubsub_request):
    req = flask.Request.from_values(
        json=raw_pubsub_request,
        path="x/projects/sample-project/topics/gcf-test?pubsub_trigger=true",
    )
    payload = event_conversion.marshal_background_event_data(req)

    # Remove timestamps as they are generated on the fly.
    del marshalled_pubsub_request["context"]["timestamp"]
    del payload["context"]["timestamp"]

    assert payload == marshalled_pubsub_request
Example #3
0
def test_marshal_background_event_data_without_topic_in_path(
        raw_pubsub_request, marshalled_pubsub_request):
    req = flask.Request.from_values(json=raw_pubsub_request, path="/myfunc/")
    payload = event_conversion.marshal_background_event_data(req)

    # Remove timestamps as they get generates on the fly
    del marshalled_pubsub_request["context"]["timestamp"]
    del payload["context"]["timestamp"]

    # Resource name is set to empty string when it cannot be parsed from the request path
    marshalled_pubsub_request["context"]["resource"]["name"] = ""

    assert payload == marshalled_pubsub_request