Skip to main content

CloudEvents Python SDK

Project description

Python SDK v2 for CloudEvents

PyPI version

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.

Receiving CloudEvents

The code below shows how to consume a cloudevent using the popular python web framework flask:

from flask import Flask, request

from cloudevents.core.bindings.http import from_http_event, HTTPMessage

app = Flask(__name__)


# create an endpoint at http://localhost:/3000/
@app.route("/", methods=["POST"])
def home():
  # create a CloudEvent
  event = from_http_event(HTTPMessage(dict(request.headers), request.get_data()))

  # you can access cloudevent fields as seen below
  print(
    f"Found {event.get_id()} from {event.get_source()} with type "
    f"{event.get_type()} and specversion {event.get_specversion()}"
  )

  return "", 204


if __name__ == "__main__":
  app.run(port=3000)

You can find a complete example of turning a CloudEvent into a HTTP request in the samples' directory.

SDK versioning

The goal of this package is to provide support for all released versions of CloudEvents, ideally while maintaining the same API. It will use semantic versioning with following rules:

  • MAJOR version increments when backwards incompatible changes is introduced.
  • MINOR version increments when backwards compatible feature is introduced INCLUDING support for new CloudEvents version.
  • PATCH version increments when a backwards compatible bug fix is introduced.

Community

Each SDK may have its own unique processes, tooling and guidelines, common governance related material can be found in the CloudEvents docs directory. In particular, in there you will find information concerning how SDK projects are managed, guidelines for how PR reviews and approval, and our Code of Conduct information.

If there is a security concern with one of the CloudEvents specifications, or with one of the project's SDKs, please send an email to cncf-cloudevents-security@lists.cncf.io.

Additional SDK Resources

Maintenance

We use uv for dependency and package management, ruff and isort for autoformatting and pre-commit to automate those with commit hooks.

For information on releasing version bumps see RELEASING.md

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cloudevents-2.1.0.tar.gz (48.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cloudevents-2.1.0-py3-none-any.whl (97.9 kB view details)

Uploaded Python 3

File details

Details for the file cloudevents-2.1.0.tar.gz.

File metadata

  • Download URL: cloudevents-2.1.0.tar.gz
  • Upload date:
  • Size: 48.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cloudevents-2.1.0.tar.gz
Algorithm Hash digest
SHA256 df0a105c10d74d0da667013c42931b883cf37af1c639a0cbe1aa32c3761de830
MD5 6a6d5365f2643f3a49bd1a63588e174d
BLAKE2b-256 1244072fdbbdab9e79d91c0d8bc6196a749779b4769e8f0bbba98993aadcdef1

See more details on using hashes here.

File details

Details for the file cloudevents-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: cloudevents-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 97.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cloudevents-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43ecec250ce05b8f80105e9959b082ef3f603bf090cef235de9b4819d4387492
MD5 43103aa1dd010c1766935d22b0a357ca
BLAKE2b-256 44236f3d09073edabfa21f34e8dcd1069278ebe20bba6c4c7f713f33a4b95689

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page