Skip to main content

A third party cloudevents 1.0 SDK for Python.

Project description

PyCloudEvents

This Python library defines a CloudEvent class that represents a CloudEvent object according to the CloudEvents specification.

TOC

Installation

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:

from pycloudevents import CloudEvent

event = CloudEvent(
    id="my-id",
    source="https://example.com/source",
    type="com.cloudevents.example.extension",
    data={"message": "Hello, world!"},
)

From a dictionary:

from pycloudevents import CloudEvent

data = {
    "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:

from pycloudevents import CloudEvent

data = {
    "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:

from pycloudevents import CloudEvent

json_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:

from pycloudevents import CloudEvent

event = 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:

from pycloudevents import CloudEvent

json_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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycloudevents-0.1.4.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

pycloudevents-0.1.4-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file pycloudevents-0.1.4.tar.gz.

File metadata

  • Download URL: pycloudevents-0.1.4.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.14.0 CPython/3.10.12 Linux/6.5.0-1017-azure

File hashes

Hashes for pycloudevents-0.1.4.tar.gz
Algorithm Hash digest
SHA256 e1ee031e9ec8c4ee81bd2c4bf677021a625529f79c10ccab0a0de17cca8b089b
MD5 81180745e2729eec2f4fc3a400bdc28f
BLAKE2b-256 95b72f0ea8685ad1303842bf830852a8cff2b59887f268569bb649be3f0e0cbb

See more details on using hashes here.

File details

Details for the file pycloudevents-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: pycloudevents-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.14.0 CPython/3.10.12 Linux/6.5.0-1017-azure

File hashes

Hashes for pycloudevents-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 97a62222f3a2e6456b9e71bb185de8fffd8fc4bb0118892326ae293c3e07f0ca
MD5 36cdd68551fe8ad188148d22b671159b
BLAKE2b-256 e536d232f0d99ba03ece8cdc4359e8dcd342519eac5bf32a317bd218a4ca7cd6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page