1- # Python SDK for [ CloudEvents] ( https://github.com/cloudevents/spec )
1+ # Python SDK v2 for [ CloudEvents] ( https://github.com/cloudevents/spec )
22
33[ ![ PyPI version] ( https://badge.fury.io/py/cloudevents.svg )] ( https://badge.fury.io/py/cloudevents )
44
@@ -10,12 +10,11 @@ will) break with every update.
1010This SDK current supports the following versions of CloudEvents:
1111
1212- v1.0
13- - v0.3
1413
1514## Python SDK
1615
17- Package ** cloudevents** provides primitives to work with CloudEvents specification:
18- https://github.com/cloudevents/spec .
16+ Package [ ** cloudevents** ] ( src/cloudevents ) provides primitives to work with
17+ [ CloudEvents specification ] ( https://github.com/cloudevents/spec ) .
1918
2019### Installing
2120
@@ -33,15 +32,15 @@ Below we will provide samples on how to send cloudevents using the popular
3332### Binary HTTP CloudEvent
3433
3534``` python
36- from cloudevents .http import CloudEvent
37- from cloudevents .conversion import to_binary
35+ from cloudevents_v1 .http import CloudEvent
36+ from cloudevents_v1 .conversion import to_binary
3837import requests
3938
4039# Create a CloudEvent
4140# - The CloudEvent "id" is generated if omitted. "specversion" defaults to "1.0".
4241attributes = {
43- " type" : " com.example.sampletype1" ,
44- " source" : " https://example.com/event-producer" ,
42+ " type" : " com.example.sampletype1" ,
43+ " source" : " https://example.com/event-producer" ,
4544}
4645data = {" message" : " Hello World!" }
4746event = CloudEvent(attributes, data)
@@ -56,15 +55,15 @@ requests.post("<some-url>", data=body, headers=headers)
5655### Structured HTTP CloudEvent
5756
5857``` python
59- from cloudevents .conversion import to_structured
60- from cloudevents .http import CloudEvent
58+ from cloudevents_v1 .conversion import to_structured
59+ from cloudevents_v1 .http import CloudEvent
6160import requests
6261
6362# Create a CloudEvent
6463# - The CloudEvent "id" is generated if omitted. "specversion" defaults to "1.0".
6564attributes = {
66- " type" : " com.example.sampletype2" ,
67- " source" : " https://example.com/event-producer" ,
65+ " type" : " com.example.sampletype2" ,
66+ " source" : " https://example.com/event-producer" ,
6867}
6968data = {" message" : " Hello World!" }
7069event = CloudEvent(attributes, data)
@@ -87,28 +86,28 @@ The code below shows how to consume a cloudevent using the popular python web fr
8786``` python
8887from flask import Flask, request
8988
90- from cloudevents .http import from_http
89+ from cloudevents_v1 .http import from_http
9190
9291app = Flask(__name__ )
9392
9493
9594# create an endpoint at http://localhost:/3000/
9695@app.route (" /" , methods = [" POST" ])
9796def home ():
98- # create a CloudEvent
99- event = from_http(request.headers, request.get_data())
97+ # create a CloudEvent
98+ event = from_http(request.headers, request.get_data())
10099
101- # you can access cloudevent fields as seen below
102- print (
103- f " Found { event[' id' ]} from { event[' source' ]} with type "
104- f " { event[' type' ]} and specversion { event[' specversion' ]} "
105- )
100+ # you can access cloudevent fields as seen below
101+ print (
102+ f " Found { event[' id' ]} from { event[' source' ]} with type "
103+ f " { event[' type' ]} and specversion { event[' specversion' ]} "
104+ )
106105
107- return " " , 204
106+ return " " , 204
108107
109108
110109if __name__ == " __main__" :
111- app.run(port = 3000 )
110+ app.run(port = 3000 )
112111```
113112
114113You can find a complete example of turning a CloudEvent into a HTTP request
@@ -162,18 +161,13 @@ with one of the project's SDKs, please send an email to
162161
163162## Maintenance
164163
165- We use [ black] [ black ] and [ isort] [ isort ] for autoformatting. We set up a [ tox] [ tox ]
166- environment to reformat the codebase.
167-
168- e.g.
169-
170- ``` bash
171- pip install tox
172- tox -e reformat
173- ```
164+ We use [ uv] [ uv ] for dependency and package management, [ ruff] [ ruff ] and [ isort] [ isort ]
165+ for autoformatting and [ pre-commit] [ pre-commit ] to automate those with commit
166+ hooks.
174167
175168For information on releasing version bumps see [ RELEASING.md] ( RELEASING.md )
176169
177- [ black ] : https://black.readthedocs.io/
170+ [ uv ] : https://docs.astral.sh/uv/
171+ [ ruff ] : https://docs.astral.sh/ruff
178172[ isort ] : https://pycqa.github.io/isort/
179- [ tox ] : https://tox.wiki/
173+ [ pre-commit ] : https://pre-commit.com
0 commit comments