Python SDK for CloudEvents
Go to file
Doug Davis ec0ae88f3b
Merge pull request #23 from duglin/editreadme
add helpful text to README
2020-03-20 10:19:46 -04:00
.github Docs, CI, etc. 2018-11-19 11:52:09 +02:00
cloudevents Fix typo in extensions (#21) 2019-06-07 15:43:41 +03:00
docs Sphinx docs 2018-11-19 12:07:30 +02:00
etc/docs_conf Sphinx docs 2018-11-19 12:07:30 +02:00
samples/python-requests Update sample scripts (#15) 2019-02-05 18:27:25 +02:00
.gitignore Make SDK compliant with CloudEvents SDK spec 2018-12-08 09:10:20 -05:00
LICENSE Initial commit 2018-09-21 18:42:34 -04:00
Makefile Sphinx docs 2018-11-19 12:07:30 +02:00
README.md add helpful text to README 2020-01-28 18:58:04 +00:00
circle.yml Docs, CI, etc. 2018-11-19 11:52:09 +02:00
docs-requirements.txt Move Sphinx dependency out of package depedency (#17) 2019-04-19 22:17:03 +03:00
release.sh Initial release: CloudEvents Python SDK 0.0.1a0 2018-11-19 11:52:09 +02:00
release_doc.md Initial release: CloudEvents Python SDK 0.0.1a0 2018-11-19 11:52:09 +02:00
requirements.txt Move Sphinx dependency out of package depedency (#17) 2019-04-19 22:17:03 +03:00
setup.cfg Add content-type for long-description. (#11) 2019-01-16 22:39:28 +02:00
setup.py Initial release: CloudEvents Python SDK 0.0.1a0 2018-11-19 11:52:09 +02:00
test-requirements.txt Adding web app tests (#13) 2019-01-17 02:26:55 +02:00
tox.ini Move Sphinx dependency out of package depedency (#17) 2019-04-19 22:17:03 +03:00

README.md

Python SDK for 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:

  • v0.2
  • v0.1

Python SDK

Package cloudevents provides primitives to work with CloudEvents specification: https://github.com/cloudevents/spec.

Parsing upstream Event from HTTP Request:

import io

from cloudevents.sdk.event import v02
from cloudevents.sdk import marshaller

m = marshaller.NewDefaultHTTPMarshaller()
event = m.FromRequest(
    v02.Event(),
    {
        "content-type": "application/cloudevents+json",
        "ce-specversion": "0.2",
        "ce-time": "2018-10-23T12:28:22.4579346Z",
        "ce-id": "96fb5f0b-001e-0108-6dfe-da6e2806f124",
        "ce-source": "<source-url>",
        "ce-type": "word.found.name",
    },
    io.BytesIO(b"this is where your CloudEvent data"), 
    lambda x: x.read()
)

Creating a minimal CloudEvent in version 0.1:

from cloudevents.sdk.event import v01

event = (
    v01.Event().
    SetContentType("application/json").
    SetData('{"name":"john"}').
    SetEventID("my-id").
    SetSource("from-galaxy-far-far-away").
    SetEventTime("tomorrow").
    SetEventType("cloudevent.greet.you")
)

Creating HTTP request from CloudEvent:

from cloudevents.sdk import converters
from cloudevents.sdk import marshaller
from cloudevents.sdk.converters import structured
from cloudevents.sdk.event import v01

event = (
    v01.Event().
    SetContentType("application/json").
    SetData('{"name":"john"}').
    SetEventID("my-id").
    SetSource("from-galaxy-far-far-away").
    SetEventTime("tomorrow").
    SetEventType("cloudevent.greet.you")
)
m = marshaller.NewHTTPMarshaller(
    [
        structured.NewJSONHTTPCloudEventConverter()
    ]
)

headers, body = m.ToRequest(event, converters.TypeStructured, lambda x: x)

HOWTOs with various Python HTTP frameworks

In this topic you'd find various example how to integrate an SDK with various HTTP frameworks.

Python requests

One of popular framework is 0.2-force-improvements.

CloudEvent to request

The code below shows how integrate both libraries in order to convert a CloudEvent into an HTTP request:

def run_binary(event, url):
    binary_headers, binary_data = http_marshaller.ToRequest(
        event, converters.TypeBinary, json.dumps)

    print("binary CloudEvent")
    for k, v in binary_headers.items():
        print("{0}: {1}\r\n".format(k, v))
    print(binary_data.getvalue())
    response = requests.post(url,
                             headers=binary_headers,
                             data=binary_data.getvalue())
    response.raise_for_status()


def run_structured(event, url):
    structured_headers, structured_data = http_marshaller.ToRequest(
        event, converters.TypeStructured, json.dumps
    )
    print("structured CloudEvent")
    print(structured_data.getvalue())

    response = requests.post(url,
                             headers=structured_headers,
                             data=structured_data.getvalue())
    response.raise_for_status()

Complete example of turning a CloudEvent into a request you can find here.

Request to CloudEvent

The code below shows how integrate both libraries in order to create a CloudEvent from an HTTP request:

    response = requests.get(url)
    response.raise_for_status()
    headers = response.headers
    data = io.BytesIO(response.content)
    event = v02.Event()
    http_marshaller = marshaller.NewDefaultHTTPMarshaller()
    event = http_marshaller.FromRequest(
        event, headers, data, json.load)

Complete example of turning a CloudEvent into a request you can find here.

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 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 to determine which week will have the call.
  • Slack: #cloudeventssdk channel under CNCF's Slack workspace.
  • Contact for additional information: Denis Makogon (@denysmakogon on slack).