# Python SDK for [CloudEvents](https://github.com/cloudevents/spec)
[![PyPI version](https://badge.fury.io/py/cloudevents.svg)](https://badge.fury.io/py/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.
### 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`](http://docs.python-requests.org) library.
### Binary HTTP CloudEvent
```python
from cloudevents.http import CloudEvent
from cloudevents.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
```python
from cloudevents.conversion import to_structured
from cloudevents.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](samples/http-json-cloudevents/client.py).
## Receiving CloudEvents
The code below shows how to consume a cloudevent using the popular python web framework
[flask](https://flask.palletsprojects.com/en/2.2.x/quickstart/):
```python
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](samples/http-json-cloudevents/json_sample_server.py).
## 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
- There are bi-weekly calls immediately following the [Serverless/CloudEvents
call](https://github.com/cloudevents/spec#meeting-time) at
9am PT (US Pacific). Which means they will typically start at 10am PT, but
if the other call ends early then the SDK call will start early as well.
See the [CloudEvents meeting minutes](https://docs.google.com/document/d/1OVF68rpuPK5shIHILK9JOqlZBbfe91RNzQ7u_P7YCDE/edit#)
to determine which week will have the call.
- Slack: #cloudeventssdk channel under
[CNCF's Slack workspace](https://slack.cncf.io/).
- Email: https://lists.cncf.io/g/cncf-cloudevents-sdk
- Contact for additional information: Denis Makogon (`@denysmakogon` on slack).
Each SDK may have its own unique processes, tooling and guidelines, common
governance related material can be found in the
[CloudEvents `docs`](https://github.com/cloudevents/spec/tree/main/docs)
directory. In particular, in there you will find information concerning
how SDK projects are
[managed](https://github.com/cloudevents/spec/blob/main/docs/GOVERNANCE.md),
[guidelines](https://github.com/cloudevents/spec/blob/main/docs/SDK-maintainer-guidelines.md)
for how PR reviews and approval, and our
[Code of Conduct](https://github.com/cloudevents/spec/blob/main/docs/GOVERNANCE.md#additional-information)
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](mailto:cncf-cloudevents-security@lists.cncf.io).
## Additional SDK Resources
- [List of current active maintainers](MAINTAINERS.md)
- [How to contribute to the project](CONTRIBUTING.md)
- [SDK's License](LICENSE)
- [SDK's Release process](RELEASING.md)
## Maintenance
We use [black][black] and [isort][isort] for autoformatting. We set up a [tox][tox]
environment to reformat the codebase.
e.g.
```bash
pip install tox
tox -e reformat
```
For information on releasing version bumps see [RELEASING.md](RELEASING.md)
[black]: https://black.readthedocs.io/
[isort]: https://pycqa.github.io/isort/
[tox]: https://tox.wiki/
Raw data
{
"_id": null,
"home_page": "https://github.com/cloudevents/sdk-python",
"name": "cloudevents",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "CloudEvents Eventing Serverless",
"author": "The Cloud Events Contributors",
"author_email": "cncfcloudevents@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/93/41/97a7448adf5888d394a22d491749fb55b1e06e95870bd9edc3d58889bb8a/cloudevents-1.11.0.tar.gz",
"platform": null,
"description": "# Python SDK for [CloudEvents](https://github.com/cloudevents/spec)\n\n[![PyPI version](https://badge.fury.io/py/cloudevents.svg)](https://badge.fury.io/py/cloudevents)\n\n## Status\n\nThis SDK is still considered a work in progress, therefore things might (and\nwill) break with every update.\n\nThis SDK current supports the following versions of CloudEvents:\n\n- v1.0\n- v0.3\n\n## Python SDK\n\nPackage **cloudevents** provides primitives to work with CloudEvents specification:\nhttps://github.com/cloudevents/spec.\n\n### Installing\n\nThe CloudEvents SDK can be installed with pip:\n\n```\npip install cloudevents\n```\n\n## Sending CloudEvents\n\nBelow we will provide samples on how to send cloudevents using the popular\n[`requests`](http://docs.python-requests.org) library.\n\n### Binary HTTP CloudEvent\n\n```python\nfrom cloudevents.http import CloudEvent\nfrom cloudevents.conversion import to_binary\nimport requests\n\n# Create a CloudEvent\n# - The CloudEvent \"id\" is generated if omitted. \"specversion\" defaults to \"1.0\".\nattributes = {\n \"type\": \"com.example.sampletype1\",\n \"source\": \"https://example.com/event-producer\",\n}\ndata = {\"message\": \"Hello World!\"}\nevent = CloudEvent(attributes, data)\n\n# Creates the HTTP request representation of the CloudEvent in binary content mode\nheaders, body = to_binary(event)\n\n# POST\nrequests.post(\"<some-url>\", data=body, headers=headers)\n```\n\n### Structured HTTP CloudEvent\n\n```python\nfrom cloudevents.conversion import to_structured\nfrom cloudevents.http import CloudEvent\nimport requests\n\n# Create a CloudEvent\n# - The CloudEvent \"id\" is generated if omitted. \"specversion\" defaults to \"1.0\".\nattributes = {\n \"type\": \"com.example.sampletype2\",\n \"source\": \"https://example.com/event-producer\",\n}\ndata = {\"message\": \"Hello World!\"}\nevent = CloudEvent(attributes, data)\n\n# Creates the HTTP request representation of the CloudEvent in structured content mode\nheaders, body = to_structured(event)\n\n# POST\nrequests.post(\"<some-url>\", data=body, headers=headers)\n```\n\nYou can find a complete example of turning a CloudEvent into a HTTP request\n[in the samples' directory](samples/http-json-cloudevents/client.py).\n\n## Receiving CloudEvents\n\nThe code below shows how to consume a cloudevent using the popular python web framework\n[flask](https://flask.palletsprojects.com/en/2.2.x/quickstart/):\n\n```python\nfrom flask import Flask, request\n\nfrom cloudevents.http import from_http\n\napp = Flask(__name__)\n\n\n# create an endpoint at http://localhost:/3000/\n@app.route(\"/\", methods=[\"POST\"])\ndef home():\n # create a CloudEvent\n event = from_http(request.headers, request.get_data())\n\n # you can access cloudevent fields as seen below\n print(\n f\"Found {event['id']} from {event['source']} with type \"\n f\"{event['type']} and specversion {event['specversion']}\"\n )\n\n return \"\", 204\n\n\nif __name__ == \"__main__\":\n app.run(port=3000)\n```\n\nYou can find a complete example of turning a CloudEvent into a HTTP request\n[in the samples' directory](samples/http-json-cloudevents/json_sample_server.py).\n\n## SDK versioning\n\nThe goal of this package is to provide support for all released versions of CloudEvents,\nideally while maintaining the same API. It will use semantic versioning\nwith following rules:\n\n- MAJOR version increments when backwards incompatible changes is introduced.\n- MINOR version increments when backwards compatible feature is introduced\n INCLUDING support for new CloudEvents version.\n- PATCH version increments when a backwards compatible bug fix is introduced.\n\n## Community\n\n- There are bi-weekly calls immediately following the [Serverless/CloudEvents\n call](https://github.com/cloudevents/spec#meeting-time) at\n 9am PT (US Pacific). Which means they will typically start at 10am PT, but\n if the other call ends early then the SDK call will start early as well.\n See the [CloudEvents meeting minutes](https://docs.google.com/document/d/1OVF68rpuPK5shIHILK9JOqlZBbfe91RNzQ7u_P7YCDE/edit#)\n to determine which week will have the call.\n- Slack: #cloudeventssdk channel under\n [CNCF's Slack workspace](https://slack.cncf.io/).\n- Email: https://lists.cncf.io/g/cncf-cloudevents-sdk\n- Contact for additional information: Denis Makogon (`@denysmakogon` on slack).\n\nEach SDK may have its own unique processes, tooling and guidelines, common\ngovernance related material can be found in the\n[CloudEvents `docs`](https://github.com/cloudevents/spec/tree/main/docs)\ndirectory. In particular, in there you will find information concerning\nhow SDK projects are\n[managed](https://github.com/cloudevents/spec/blob/main/docs/GOVERNANCE.md),\n[guidelines](https://github.com/cloudevents/spec/blob/main/docs/SDK-maintainer-guidelines.md)\nfor how PR reviews and approval, and our\n[Code of Conduct](https://github.com/cloudevents/spec/blob/main/docs/GOVERNANCE.md#additional-information)\ninformation.\n\nIf there is a security concern with one of the CloudEvents specifications, or\nwith one of the project's SDKs, please send an email to\n[cncf-cloudevents-security@lists.cncf.io](mailto:cncf-cloudevents-security@lists.cncf.io).\n\n## Additional SDK Resources\n\n- [List of current active maintainers](MAINTAINERS.md)\n- [How to contribute to the project](CONTRIBUTING.md)\n- [SDK's License](LICENSE)\n- [SDK's Release process](RELEASING.md)\n\n## Maintenance\n\nWe use [black][black] and [isort][isort] for autoformatting. We set up a [tox][tox]\nenvironment to reformat the codebase.\n\ne.g.\n\n```bash\npip install tox\ntox -e reformat\n```\n\nFor information on releasing version bumps see [RELEASING.md](RELEASING.md)\n\n[black]: https://black.readthedocs.io/\n[isort]: https://pycqa.github.io/isort/\n[tox]: https://tox.wiki/\n",
"bugtrack_url": null,
"license": "https://www.apache.org/licenses/LICENSE-2.0",
"summary": "CloudEvents Python SDK",
"version": "1.11.0",
"project_urls": {
"Homepage": "https://github.com/cloudevents/sdk-python"
},
"split_keywords": [
"cloudevents",
"eventing",
"serverless"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "cf0e268a75b712e4dd504cff19e4b987942cd93532d1680009d6492c9d41bdac",
"md5": "ffc0bff9dae997d1982956713ec8172e",
"sha256": "77edb4f2b01f405c44ea77120c3213418dbc63d8859f98e9e85de875502b8a76"
},
"downloads": -1,
"filename": "cloudevents-1.11.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ffc0bff9dae997d1982956713ec8172e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 55088,
"upload_time": "2024-06-20T13:47:30",
"upload_time_iso_8601": "2024-06-20T13:47:30.066513Z",
"url": "https://files.pythonhosted.org/packages/cf/0e/268a75b712e4dd504cff19e4b987942cd93532d1680009d6492c9d41bdac/cloudevents-1.11.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "934197a7448adf5888d394a22d491749fb55b1e06e95870bd9edc3d58889bb8a",
"md5": "8b49d32a90938c04111008799ce1f402",
"sha256": "5be990583e99f3b08af5a709460e20b25cb169270227957a20b47a6ec8635e66"
},
"downloads": -1,
"filename": "cloudevents-1.11.0.tar.gz",
"has_sig": false,
"md5_digest": "8b49d32a90938c04111008799ce1f402",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 33670,
"upload_time": "2024-06-20T13:47:32",
"upload_time_iso_8601": "2024-06-20T13:47:32.051350Z",
"url": "https://files.pythonhosted.org/packages/93/41/97a7448adf5888d394a22d491749fb55b1e06e95870bd9edc3d58889bb8a/cloudevents-1.11.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-20 13:47:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "cloudevents",
"github_project": "sdk-python",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"tox": true,
"lcname": "cloudevents"
}