Python SDK for CloudEvents
Go to file
Tudor Plugaru a73c8702d4
feat: base `CloudEvent` class as per v1 specs, including attribute validation (#242)
* feat: base `CloudEvent` class as per v1 specs, including attribute validation

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* chore: add typings and docstrings

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* chore: Add support for custom extension names and validate them

Signed-off-by: Tudor <plugaru.tudor@protonmail.com>

* chore: Add copyright and fix missing type info

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* chore: Add getters for attributes and test happy path

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* fix: typing

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* chore: Split validation logic into smaller methods

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* chore: Add method to extract extension by name

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* chore: configure ruff to sort imports also

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* chore: Returns all the errors at ones instead of raising early. Improve tests

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* fix missing type info

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* chore: Improve exceptions handling. Have exceptions grouped by attribute name and typed exceptions

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* chore: Skip type checing for getters of required attributes

We can't use TypedDict here becuase it does not allow for arbitrary keys which we need in order to support custom extension attributes.

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* fix: missing type

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* chore: Improve exceptions and introduce a new one for invalid values

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* fix: str representation for validation error

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: Fix missing type definitions

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* small fix

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

* remove cast of defaultdict to dict

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>

---------

Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>
Signed-off-by: Tudor <plugaru.tudor@protonmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-11-16 21:20:57 +02:00
.github chore: V2 migrate to uv (#239) 2024-09-30 21:42:13 +03:00
samples chore: V2 migrate to uv (#239) 2024-09-30 21:42:13 +03:00
src/cloudevents feat: base `CloudEvent` class as per v1 specs, including attribute validation (#242) 2024-11-16 21:20:57 +02:00
tests feat: base `CloudEvent` class as per v1 specs, including attribute validation (#242) 2024-11-16 21:20:57 +02:00
.clomonitor.yml rename (#160) 2022-04-27 12:48:44 -04:00
.coveragerc refactor: create abstract cloudevent (#186) 2022-08-06 14:52:22 +03:00
.gitignore Make SDK compliant with CloudEvents SDK spec 2018-12-08 09:10:20 -05:00
.pre-commit-config.yaml chore: V2 migrate to uv (#239) 2024-09-30 21:42:13 +03:00
CHANGELOG.md Release/v1.11.0 (#237) 2024-06-20 09:31:13 +03:00
CONTRIBUTING.md Add some CLO stuff (#158) 2022-04-26 14:38:38 -04:00
LICENSE Initial commit 2018-09-21 18:42:34 -04:00
MAINTAINERS.md Governance docs per CE PR 1226 (#221) 2023-09-21 22:59:54 +03:00
MANIFEST.in Introduce typings (#207) 2023-01-04 08:29:41 -07:00
README.md chore: V2 migrate to uv (#239) 2024-09-30 21:42:13 +03:00
RELEASING.md chore: V2 migrate to uv (#239) 2024-09-30 21:42:13 +03:00
pyproject.toml feat: base `CloudEvent` class as per v1 specs, including attribute validation (#242) 2024-11-16 21:20:57 +02:00
tox.ini Update CI and tooling (#236) 2024-05-26 21:49:35 +03:00
uv.lock chore: V2 migrate to uv (#239) 2024-09-30 21:42:13 +03:00

README.md

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

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_v1.http import CloudEvent
from cloudevents_v1.conversion import to_binary
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
headers, body = to_binary(event)

# POST
requests.post("<some-url>", data=body, headers=headers)

Structured HTTP CloudEvent

from cloudevents_v1.conversion import to_structured
from cloudevents_v1.http import CloudEvent
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
headers, body = to_structured(event)

# POST
requests.post("<some-url>", data=body, headers=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_v1.http import from_http

app = Flask(__name__)


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

  # you can access cloudevent fields as seen below
  print(
    f"Found {event['id']} from {event['source']} with type "
    f"{event['type']} and specversion {event['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