Skip to content

Latest commit

 

History

History
67 lines (46 loc) · 2.11 KB

README.adoc

File metadata and controls

67 lines (46 loc) · 2.11 KB

uProtocol CloudEvents

1. Overview

uProtocol CloudEvents is a common message envelope that could be used to carry way to represent uProtocol transport layer information UUri (source), UPayload, and UAttributes. CloudEvents are used by a number of Device-2-Cloud and Cloud-2-Device based transports such as MQTT and HTTP, however it could also be used by any transport (ex. Binder).

1.1. CloudEventFactory

Factory class that builds the various types of CloudEvents for uProtocol (publish, notification, request, response)

2. Examples

2.1. Building an uuri

     uri = UUri(entity=UEntity(name="body.access"),
               resource=UResource(name="door", instance="front_left", message="Door"))
     source= LongUriSerializer().serialize(uri)

2.2. Build proto payload

ce_proto = CloudEvent(spec_version="1.0", source="https://example.com", id="hello", type="example.demo",
                          proto_data=any_pb2.Any())

any_obj = any_pb2.Any()
any_obj.Pack(ce_proto)
proto_payload = any_obj

2.3. Build UCloudEvent Attributes

u_cloud_event_attributes = UCloudEventAttributesBuilder().with_hash("somehash").with_priority(
UPriority.UPRIORITY_CS1).with_ttl(3).build()

2.4. Build publish cloud event

cloud_event = CloudEventFactory.publish(source, proto_payload, u_cloud_event_attributes)
# test all attributes
assertEqual("1.0", UCloudEvent.get_specversion(cloud_event))
assertIsNotNone(UCloudEvent.get_id(cloud_event))
assertEqual(source, UCloudEvent.get_source(cloud_event))
assertEqual(UCloudEvent.get_event_type(UMessageType.UMESSAGE_TYPE_PUBLISH),
                 UCloudEvent.get_type(cloud_event))
assertNotIn("sink", cloud_event.get_attributes())
assertEqual("somehash", UCloudEvent.get_hash(cloud_event))
assertEqual(UPriority.Name(UPriority.UPRIORITY_CS1), UCloudEvent.get_priority(cloud_event))
assertEqual(3, UCloudEvent.get_ttl(cloud_event))
assertEqual(proto_payload.SerializeToString(), cloud_event.get_data())