Giter Site home page Giter Site logo

pycloudevents's Introduction

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

pycloudevents's People

Contributors

laipz8200 avatar

Watchers

 avatar

pycloudevents's Issues

Better repr

For now, print a CloudEvent object will show something like

<pycloudevents.cloudevent.CloudEvent object at 0x7fe6e72c4fd0> .

Let's change it to a better view like

<pycloudevents.CloudEvent (1.0) <type> <id> <source> [subject]> .

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.