Skip to content

cloudevents/sdk-python

Repository files navigation

Python SDK 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: https://github.com/cloudevents/spec.

Sending CloudEvents

Below we will provide samples on how to send cloudevents using the popular requests library.

Binary HTTP CloudEvent

from cloudevents.sdk import converters
from cloudevents.sdk.http_events import CloudEvent
import requests


# This data defines a binary cloudevent
attributes = {
    "type": "com.example.sampletype1",
    "source": "https://example.com/event-producer",
}
data = {"message": "Hello World!"}

event = CloudEvent(attributes, data)
headers, body = event.to_http(converters.TypeBinary)

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

Structured HTTP CloudEvent

from cloudevents.sdk.http_events import CloudEvent
import requests


# This data defines a structured cloudevent
attributes = {
    "type": "com.example.sampletype2",
    "source": "https://example.com/event-producer",
}
data = {"message": "Hello World!"}
event = CloudEvent(attributes, data)
headers, body = event.to_http()

# 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.

Request to CloudEvent

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

from flask import Flask, request

from cloudevents.sdk.http_events import CloudEvent

app = Flask(__name__)


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

    # you can access cloudevent fields as seen below
    print(f"Found CloudEvent from {event['source']} with specversion {event['specversion']}")

    if event['type'] == 'com.example.sampletype1':
        print(f"CloudEvent {event['id']} is binary")

    elif event['type'] == 'com.example.sampletype2':
        print(f"CloudEvent {event['id']} is structured")

    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

Maintenance

We use black and isort for autoformatting. We setup a tox environment to reformat the codebase.

e.g.

pip install tox
tox -e reformat