CloudEvents Python SDK
Actively maintained. Last release 5 months ago, commits in 6 of the last 26 weeks. A safe choice.
pip install cloudeventsPython SDK v2 for CloudEvents
Status
This SDK is still considered a work in progress, therefore things might (and will) break with every update.
This SDK current supports the following versions of CloudEvents:
- v1.0
- v0.3
Python SDK
Package cloudevents provides primitives to work with CloudEvents specification.
Installing
The CloudEvents SDK can be installed with pip:
pip install cloudevents
Sending CloudEvents
Below we will provide samples on how to send cloudevents using the popular
requests library.
Binary HTTP CloudEvent
from cloudevents.core.v1.event import CloudEvent
from cloudevents.core.bindings.http import to_binary_event
import requests
# Create a CloudEvent
# - The CloudEvent "id" is generated if omitted. "specversion" defaults to "1.0".
attributes = {
"type": "com.example.sampletype1",
"source": "https://example.com/event-producer",
}
data = {"message": "Hello World!"}
event = CloudEvent(attributes, data)
# Creates the HTTP request representation of the CloudEvent in binary content mode
message = to_binary_event(event)
# POST
requests.post("<some-url>", data=message.body, headers=message.headers)
Structured HTTP CloudEvent
from cloudevents.core.v1.event import CloudEvent
from cloudevents.core.bindings.http import to_structured_event
import requests
# Create a CloudEvent
# - The CloudEvent "id" is generated if omitted. "specversion" defaults to "1.0".
attributes = {
"type": "com.example.sampletype2",
"source": "https://example.com/event-producer",
}
data = {"message": "Hello World!"}
event = CloudEvent(attributes, data)
# Creates the HTTP request representation of the CloudEvent in structured content mode
message = to_structured_event(event)
# POST
requests.post("<some-url>", data=message.body, headers=message.headers)
You can find a complete example of turning a CloudEvent into a HTTP request in the samples' directory.