Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions src/cloudevents/v1/abstract/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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]

Expand All @@ -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
Expand Down