Python SDK for CloudEvents
Go to file
Yurii Serhiichuk 8a88ffee10
chore: cleanup codebase and fix flake errors (#188)
* deps: `flake8-strict` and  `flake8-import-order` are not compatible with Black and modern Python anymore

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: Cleanup imports and remove obsolete `#noqa`.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: sort imports.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: Define `__all__`

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: Fix licenses and add __all__ to imports.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: Fix formatting

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: Export `from_http`

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* fix: Do not export functions of other modules from this one.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: Resolve more flake8 errors

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: Fix more warnings

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* docs: add a note in the changelog about the fixes.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* fix: imports in tests.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

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

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

* fix: more import fixes.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* fix: use proper implementations as replacements.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

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

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

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-08-14 15:47:38 -07:00
.github ci: migrate to `main` branch (#180) 2022-07-15 11:22:29 +03:00
cloudevents chore: cleanup codebase and fix flake errors (#188) 2022-08-14 15:47:38 -07:00
requirements chore: cleanup codebase and fix flake errors (#188) 2022-08-14 15:47:38 -07:00
samples chore: unify copyright with other SDKs and update/add it where needed. (#170) 2022-07-11 12:56:07 -07: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 Feat/dev env cleanup (#167) 2022-07-10 12:53:37 +03:00
CHANGELOG.md chore: cleanup codebase and fix flake errors (#188) 2022-08-14 15:47:38 -07: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
OWNERS Feat/dev env cleanup (#167) 2022-07-10 12:53:37 +03:00
README.md Feat/dev env cleanup (#167) 2022-07-10 12:53:37 +03:00
RELEASING.md ci: migrate to `main` branch (#180) 2022-07-15 11:22:29 +03:00
pypi_packaging.py ci: migrate to `main` branch (#180) 2022-07-15 11:22:29 +03:00
pyproject.toml Feat/dev env cleanup (#167) 2022-07-10 12:53:37 +03:00
setup.py feat: pydantic (#182) 2022-08-13 21:35:53 +03:00
tox.ini chore: drop `docs` and related files (#168) 2022-07-11 20:05:45 +03:00

README.md

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

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.http import CloudEvent, 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.http import CloudEvent, to_structured
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.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.

Maintenance

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

e.g.

pip install tox
tox -e reformat

For information on releasing version bumps see RELEASING.md