diff --git a/src/cloudevents/v1/abstract/event.py b/src/cloudevents/v1/abstract/event.py index 18c6df19..e72b9cb5 100644 --- a/src/cloudevents/v1/abstract/event.py +++ b/src/cloudevents/v1/abstract/event.py @@ -25,6 +25,49 @@ class CloudEvent: The CloudEvent Python wrapper contract exposing generically-available properties and APIs. + A CloudEvent describes event data in a common, vendor-neutral way, as + defined by the CloudEvents specification + (https://github.com/cloudevents/spec). Every event is made of two + distinct parts: + + - Attributes (also called context attributes): metadata describing the + event. The CloudEvents v1.0 specification defines the required + attributes `id`, `source`, `type` and `specversion`, together with the + optional attributes `datacontenttype`, `dataschema`, `subject` and + `time`. Concrete implementations may also carry user-defined extension + attributes. + - Data: the optional event payload. The payload is not an attribute and is + kept separate from the attribute mapping. + + This class defines the read-only contract shared by concrete + implementations such as `cloudevents.v1.http.CloudEvent` and the Pydantic + variants. The base class can be instantiated directly. Its attribute and + payload access methods raise `NotImplementedError`. Use a concrete subclass + or its `create` factory to construct an event. + + Attributes can be read through a read-only, mapping-like interface + (`__getitem__`, `get`, `__iter__`, `__len__` and `__contains__`). Use + `get_data()` (or `.data` on an HTTP event) to read the payload. If an HTTP + event includes a separate attribute named `data`, both `event["data"]` + and `event.get("data")` return that attribute's value. If the attribute + is absent, indexing raises `KeyError` and `get` returns its default. + + Example: + >>> from cloudevents.v1.http import CloudEvent + >>> event = CloudEvent( + ... { + ... "type": "com.example.sampletype1", + ... "source": "https://example.com/event-producer", + ... }, + ... {"message": "Hello World!"}, + ... ) + >>> event["type"] + 'com.example.sampletype1' + >>> event.data + {'message': 'Hello World!'} + >>> "data" in event # This event has no attribute named "data". + False + Implementations might handle fields and have other APIs exposed but are obliged to follow this contract. """ @@ -96,11 +139,13 @@ def __getitem__(self, key: str) -> typing.Any: """ Returns a value of an attribute of the event denoted by the given `key`. - The `data` of the event should be accessed by the `.data` accessor rather - than this mapping. + Use `get_data()` to read the event payload. If an attribute named + `data` is present, this method returns that attribute for the key + `"data"`. :param key: The name of the event attribute to retrieve the value for. :returns: The event attribute value. + :raises KeyError: If no attribute with the given `key` exists. """ return self._get_attributes()[key] @@ -110,6 +155,10 @@ def get( """ Retrieves an event attribute value for the given `key`. + Use `get_data()` to read the event payload. If an attribute named + `data` is present, this method returns that attribute for the key + `"data"`. + Returns the `default` value if the attribute for the given key does not exist. The implementation MUST NOT throw an error when the key does not exist, but