def target(): # Deserialize the HTTP request into a CloudEvent try: logging.info('Deserializing CloudEvent in binary format') # Instantiate the Binary Format marshaller mb = marshaller.NewHTTPMarshaller( [binary.NewBinaryHTTPCloudEventConverter()]) ce = mb.FromRequest(v1.Event(), request.headers, request.stream.read(), lambda x: x) except: logging.warning( 'Deserializing application/cloudevents+json CloudEvent') # Instantiate the JSON Structured CloudEvent marshaller m = marshaller.NewHTTPMarshaller( [structured.NewJSONHTTPCloudEventConverter()]) ce = m.FromRequest() except: logging.warning('Could not deserialize CloudEvent') # Create a CloudEvent to send as response data = 'Data received was not understandable as a CloudEvent' event = ( v1.Event().SetContentType("text/plain").SetData(data).SetEventID( str(uuid.uuid4())).SetSource("from_your_own_target"). SetSubject("your_event_subject").SetEventTime( datetime.now( timezone.utc).astimezone().isoformat()).SetEventType( "io.triggermesh.target.byown")) return app.response_class(response=body, headers=headers, status=400) # Do your Transformation or Target work based on the eventype if ce.EventType() == "io.triggermesh.byown.create": logging.info("Create event type") elif ce.EventType() == "io.triggermesh.byown.delete": logging.info("Delete event type") else: logging.warning("Unknown event type %s" % ce.EventType()) # Create a CloudEvent to send as response data = 'this is some data' event = (v1.Event().SetContentType("text/plain").SetData(data).SetEventID( str(uuid.uuid4())).SetSource("from_your_own_target").SetSubject( "your_event_subject").SetEventTime( datetime.now( timezone.utc).astimezone().isoformat()).SetEventType( "io.triggermesh.target.byown")) # Prepare the Header and Body to send a request back as a CloudEvent m = marshaller.NewHTTPMarshaller( [structured.NewJSONHTTPCloudEventConverter()]) headers, body = m.ToRequest(event, converters.TypeStructured, lambda x: x) return app.response_class(response=body, headers=headers, status=200)
def test_from_request_cannot_read(binary_headers): with pytest.raises(exceptions.UnsupportedEventConverter): m = marshaller.HTTPMarshaller([binary.NewBinaryHTTPCloudEventConverter()]) m.FromRequest(v1.Event(), {}, "") with pytest.raises(exceptions.UnsupportedEventConverter): m = marshaller.HTTPMarshaller([structured.NewJSONHTTPCloudEventConverter()]) m.FromRequest(v1.Event(), binary_headers, "")
def test_binary_event_v1(): event = (v1.Event().SetContentType("application/octet-stream").SetData( b"\x00\x01")) m = marshaller.NewHTTPMarshaller( [structured.NewJSONHTTPCloudEventConverter()]) _, body = m.ToRequest(event, converters.TypeStructured, lambda x: x) assert isinstance(body, bytes) content = json.loads(body) assert "data" not in content assert content["data_base64"] == "AAE=", f"Content is: {content}"
def NewDefaultHTTPMarshaller() -> HTTPMarshaller: """ Creates the default HTTP marshaller with both structured and binary converters :return: an instance of HTTP marshaller :rtype: cloudevents.sdk.marshaller.HTTPMarshaller """ return HTTPMarshaller([ structured.NewJSONHTTPCloudEventConverter(), binary.NewBinaryHTTPCloudEventConverter(), ])
def make_cloud_event(event_type: str, data: Dict[str, Any]) -> Any: tz = get_tz() event = (v03.Event().SetContentType("application/json").SetData( data).SetEventID(str( uuid.uuid4())).SetSource("chaosiq-cloud").SetEventTime( tz.localize( datetime.now()).isoformat()).SetEventType(event_type)) m = marshaller.NewHTTPMarshaller( [structured.NewJSONHTTPCloudEventConverter()]) h, b = m.ToRequest(event, converters.TypeStructured, lambda x: x) return h, b.getvalue()
def test_unsupported_converter_v01(): m = marshaller.NewHTTPMarshaller( [structured.NewJSONHTTPCloudEventConverter()]) pytest.raises( exceptions.UnsupportedEventConverter, m.FromRequest, v01.Event, {}, None, lambda x: x, )
def test_structured_converter_v01(): m = marshaller.NewHTTPMarshaller( [structured.NewJSONHTTPCloudEventConverter()]) event = m.FromRequest( v01.Event(), {"Content-Type": "application/cloudevents+json"}, io.StringIO(json.dumps(data.json_ce[v02.Event])), lambda x: x.read(), ) assert event is not None assert event.Get("type") == (data.ce_type, True) assert event.Get("id") == (data.ce_id, True)
def test_structured_converter_upstream(event_class): m = marshaller.NewHTTPMarshaller( [structured.NewJSONHTTPCloudEventConverter()]) event = m.FromRequest( event_class(), {"Content-Type": "application/cloudevents+json"}, io.StringIO(json.dumps(data.json_ce[event_class])), lambda x: x.read(), ) assert event is not None assert event.EventType() == data.ce_type assert event.EventID() == data.ce_id assert event.ContentType() == data.contentType
def callback(message): print(message.data.decode()) print(sink_url) event = (v01.Event().SetContentType("application/json").SetData( message.data.decode()).SetEventID("my-id").SetSource( "from-galaxy-far-far-away").SetEventTime("tomorrow").SetEventType( "cloudevent.greet.you")) m = marshaller.NewHTTPMarshaller( [structured.NewJSONHTTPCloudEventConverter()]) headers, body = m.ToRequest(event, converters.TypeStructured, lambda x: x) requests.post(sink_url, data=body, headers=headers) message.ack()
def test_structured_event_to_request_v01(): copy_of_ce = copy.deepcopy(data.ce) m = marshaller.NewHTTPMarshaller( [structured.NewJSONHTTPCloudEventConverter()]) http_headers = {"content-type": "application/cloudevents+json"} event = m.FromRequest(v01.Event(), http_headers, io.StringIO(json.dumps(data.ce)), lambda x: x.read()) assert event is not None assert event.Get("type") == (data.ce_type, True) assert event.Get("id") == (data.ce_id, True) new_headers, _ = m.ToRequest(event, converters.TypeStructured, lambda x: x) for key in new_headers: if key == "content-type": assert new_headers[key] == http_headers[key] continue assert key in copy_of_ce
def test_event_pipeline_v01(): event = (v01.Event().SetContentType(data.contentType).SetData( data.body).SetEventID(data.ce_id).SetSource(data.source).SetEventTime( data.eventTime).SetEventType(data.ce_type)) m = marshaller.NewHTTPMarshaller( [structured.NewJSONHTTPCloudEventConverter()]) _, body = m.ToRequest(event, converters.TypeStructured, lambda x: x) assert isinstance(body, io.BytesIO) new_headers = json.load(io.TextIOWrapper(body, encoding="utf-8")) assert new_headers is not None assert "cloudEventsVersion" in new_headers assert "eventType" in new_headers assert "source" in new_headers assert "eventID" in new_headers assert "eventTime" in new_headers assert "contentType" in new_headers assert data.body == new_headers["data"]
def customCallback(client, userdata, message): logger.info("Received a new message: ") logger.info(message.payload) logger.info("from topic: ") logger.info(message.topic) logger.info("--------------\n\n") print(message.data.decode()) print(sink_url) event = (v01.Event().SetContentType("application/json").SetData( message.payload).SetEventID("my-id").SetSource("AWS IoT").SetEventTime( datetime.datetime.now()).SetEventType("cloudevent.greet.you")) m = marshaller.NewHTTPMarshaller( [structured.NewJSONHTTPCloudEventConverter()]) headers, body = m.ToRequest(event, converters.TypeStructured, lambda x: x) requests.post(sink_url, data=body, headers=headers) message.ack()
def test_to_request_invalid_converter(): with pytest.raises(exceptions.NoSuchConverter): m = marshaller.HTTPMarshaller([structured.NewJSONHTTPCloudEventConverter()]) m.ToRequest(v1.Event(), "")
from cloudevents.sdk.event import v1 from google.cloud import pubsub_v1 import requests subscriber = pubsub_v1.SubscriberClient() subscription_name = 'projects/{project_id}/subscriptions/{sub}'.format( project_id=os.getenv('GOOGLE_CLOUD_PROJECT'), sub=os.getenv('MY_SUBSCRIPTION_NAME'), ) K_SINK = os.getenv('K_SINK') m = marshaller.NewHTTPMarshaller([structured.NewJSONHTTPCloudEventConverter()]) def run_structured(event, url): structured_headers, structured_data = m.ToRequest( event, converters.TypeStructured, json.dumps) print("structured CloudEvent") print(structured_data.getvalue()) response = requests.post(url, headers=structured_headers, data=structured_data.getvalue()) response.raise_for_status() def callback(message):
def hello(cloudevent): m = marshaller.NewHTTPMarshaller([structured.NewJSONHTTPCloudEventConverter()]) headers, body = m.ToRequest(cloudevent, converters.TypeStructured, lambda x: x) text_file = open("function_output.json", "w") text_file.write(body.getvalue().decode("utf-8")) text_file.close()