def test_unsupported_event_configuration(): m = marshaller.NewHTTPMarshaller( [binary.NewBinaryHTTPCloudEventConverter()]) pytest.raises( exceptions.UnsupportedEventConverter, m.FromRequest, v01.Event(), {"Content-Type": "application/cloudevents+json"}, io.StringIO(json.dumps(data.json_ce[v02.Event])), lambda x: x.read(), )
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 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 POST(self): """Accept the post data and return it.""" connection = pika.BlockingConnection( pika.ConnectionParameters( os.getenv('HOST', 'rabbitmq'), int(os.getenv('PORT', '5672')), os.getenv('VHOST', '/'), pika.PlainCredentials( os.getenv('USER', 'guest'), os.getenv('PASS', 'guest') ) ) ) main_channel = connection.channel() main_channel.exchange_declare(exchange='gov.pnnl.datahub.events', durable=True, exchange_type='direct') headers = cherrypy.request.headers headers['Content-Type'] = 'application/cloudevents+json' raw_data = cherrypy.request.body.read().decode('utf8') print(raw_data) data = io.StringIO(raw_data) event = v01.Event() http_marshaller = marshaller.NewDefaultHTTPMarshaller() event = http_marshaller.FromRequest(event, headers, data, lambda x: x) print(event.EventType()) main_channel.basic_publish( exchange='gov.pnnl.datahub.events', routing_key=event.EventType(), body=json.dumps(event.Data()), properties=pika.BasicProperties( content_type='application/json', headers={ 'cloudEvents:specversion': event.CloudEventVersion(), 'cloudEvents:type': event.EventType(), 'cloudEvents:id': event.EventID(), 'cloudEvents:source': event.Source() } ) ) connection.close() return {'message': 'success'}
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_invalid_data_marshaller(): m = marshaller.NewDefaultHTTPMarshaller() pytest.raises(exceptions.InvalidDataMarshaller, m.ToRequest, v01.Event(), "blah", None)
def test_invalid_data_unmarshaller(): m = marshaller.NewDefaultHTTPMarshaller() pytest.raises(exceptions.InvalidDataUnmarshaller, m.FromRequest, v01.Event(), {}, None, None)