Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/laipz8200/pycloudevents
A third party cloudevents 1.0 SDK for Python.
https://github.com/laipz8200/pycloudevents
Last synced: 21 days ago
JSON representation
A third party cloudevents 1.0 SDK for Python.
- Host: GitHub
- URL: https://github.com/laipz8200/pycloudevents
- Owner: laipz8200
- License: apache-2.0
- Created: 2024-04-09T09:52:28.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-04-10T02:10:08.000Z (7 months ago)
- Last Synced: 2024-04-10T11:01:28.417Z (7 months ago)
- Language: Python
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# PyCloudEvents
This Python library defines a CloudEvent class that represents a CloudEvent object according to the [CloudEvents specification](https://www.cncf.io/projects/cloudevents/).
## TOC
- [PyCloudEvents](#pycloudevents)
- [TOC](#toc)
- [Installation](#installation)
- [Usage](#usage)
- [Creating a CloudEvent object](#creating-a-cloudevent-object)
- [Serializing a CloudEvent object to JSON](#serializing-a-cloudevent-object-to-json)
- [Deserializing a JSON string to a CloudEvent object](#deserializing-a-json-string-to-a-cloudevent-object)
- [CloudEvent Attributes](#cloudevent-attributes)
- [Contributing](#contributing)## Installation
```shell
pip install pycloudevents
```## Usage
The `CloudEvent` class provides methods to create, serialize (convert to JSON), and deserialize CloudEvents.
### Creating a CloudEvent object
There are four ways to create a `CloudEvent` object:
Using keyword arguments in the constructor:
```python
from pycloudevents import CloudEventevent = CloudEvent(
id="my-id",
source="https://example.com/source",
type="com.cloudevents.example.extension",
data={"message": "Hello, world!"},
)
```From a dictionary:
```python
from pycloudevents import CloudEventdata = {
"id": "12345",
"specversion": "1.0",
"type": "com.cloudevents.example.extension",
"source": "https://example.com/source",
"data": {"message": "Hello, world!"},
}event = CloudEvent.from_dict(data)
```From a mapping object:
```python
from pycloudevents import CloudEventdata = {
"id": "12345",
"specversion": "1.0",
"type": "com.cloudevents.example.extension",
"source": "https://example.com/source",
"data": {"message": "Hello, world!"},
}event = CloudEvent.from_mapping(data)
```From a JSON string:
```python
from pycloudevents import CloudEventjson_string = '{"id": "12345", "specversion": "1.0", "type": "com.cloudevents.example.extension", "source": "https://example.com/source", "data": {"message": "Hello, world!"}}'
event = CloudEvent.from_json(json_string)
```### Serializing a CloudEvent object to JSON
The `to_structured` method converts a `CloudEvent` object to a JSON string:
```python
from pycloudevents import CloudEventevent = CloudEvent(...) # Create an event object
json_data = event.to_structured()
print(json_data)
```### Deserializing a JSON string to a CloudEvent object
The `from_json` class method creates a `CloudEvent` object from a JSON string:
```python
from pycloudevents import CloudEventjson_string = '{"specversion": "1.0", ...}' # Your JSON string
event = CloudEvent.from_json(json_string)
```## CloudEvent Attributes
The `CloudEvent` class includes the following attributes according to the CloudEvents specification:
- `id`: (str) The identifier of the event.
- `source`: (str) The source of the event.
- `specversion`: (str) The CloudEvents specification version (default is "1.0").
- `type`: (str) The type of the event.
- `datacontenttype`: (Optional[str]) The data content type (default is None).
- `dataschema`: (Optional[str]) The data schema (default is None).
- `subject`: (Optional[str]) The subject of the event (default is None).
- `time`: (Optional[str]) The timestamp of the event (default is None).
- `data`: (Any) The data associated with the event (default is None).
- `extensions`: (Hashable) Additional extensions for the event.## Contributing
See more in our [Contributing Guidelines](./CONTRIBUTING.md)