Skip to main content

A toolkit for emitting and handling events, following the CloudEvent spec.

Project description

eventkit-py

ci-badge version-badge

A toolkit for emitting and handling events, following the CloudEvent spec.

Installation

poetry add outcome-eventkit

Usage

It's a good idea to be familiar with the CloudEvent v1.0 spec before using this library.

The library implements the following (but is extensible):

Event Example

from outcome.eventkit import CloudEvent, CloudEventData
from outcome.eventkit.formats.json import JSONCloudEventFormat
from outcome.eventkit.protocol_bindings.http import StructuredHTTPBinding, BinaryHTTPBinding

import requests

# Create a payload, specifying how it will be encoded
data = CloudEventData(
    data_content_type='application/json',
    data={
        'hello': 'world'
    }
)

# Create an event
event = CloudEvent(type='co.outcome.events.sample', source='example', data=data)

# Create a JSON representation of the event
serialised_event = JSONCloudEventFormat.encode(event)

# Or...

# Create a HTTP message with the event
http_message = BinaryHTTPBinding.to_http(event)

# Or...

# Create a structured HTTP message with the event and a JSON formatter
http_message = StructuredHTTPBinding.to_http(event, JSONCloudEventFormat)

# Post the event somewhere...
requests.post('http://example.org', headers=http_message.headers, data=http_message.body)

Dispatch Example

from outcome.eventkit import dispatch, CloudEvent

# You can register event handlers that will be called
# when a corresponding event is dispatched

# Explicitly
def my_event_handler(event: CloudEvent) -> None:
    ...

# Register a handler for an event type
dispatch.register_handler('co.outcome.event', my_event_handler)


# Or via a decorator
@dispatch.handles_events('co.outcome.event', 'co.outcome.other-event')
def my_other_handler(event: CloudEvent) -> None:
    ...


# Then notify all registered handlers of an event
ev = CloudEvent(type='co.outcome.event', source='example')
dispatch.dispatch(ev)

By default, all handlers are stored in a single registry, but you can provide your own registry to the register_handler/handles_events/dispatch methods with the registry keyword argument.

from outcome.eventkit import dispatch
from collections import defaultdict

my_registry: dispatch.CloudEventHandlerRegistry = defaultdict(list)

@dispatch.handles_events('co.outcome.event', registry=my_registry)
def my_handler(event: CloudEvent) -> None:
    ...

ev = CloudEvent(type='co.outcome.event', source='example')
dispatch.dispatch(ev, registry=my_registry)

Development

Remember to run ./pre-commit.sh when you clone the repository.

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

outcome-eventkit-0.2.14.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

outcome_eventkit-0.2.14-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file outcome-eventkit-0.2.14.tar.gz.

File metadata

  • Download URL: outcome-eventkit-0.2.14.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.6 Linux/5.4.0-1032-azure

File hashes

Hashes for outcome-eventkit-0.2.14.tar.gz
Algorithm Hash digest
SHA256 68531b962e563879e1a7d513f4a4c7b58c22509357f5140ba3dc5fe66f26d60e
MD5 a3b83c1705f85ba51bbf2938e4af088d
BLAKE2b-256 abebc4ec216d21d9382e0b63774e1b84d06906fb2fe37bca6e7c8981e5a766b7

See more details on using hashes here.

File details

Details for the file outcome_eventkit-0.2.14-py3-none-any.whl.

File metadata

  • Download URL: outcome_eventkit-0.2.14-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.6 Linux/5.4.0-1032-azure

File hashes

Hashes for outcome_eventkit-0.2.14-py3-none-any.whl
Algorithm Hash digest
SHA256 68212116e82288653ad6a32ccea24a6d0f7d5e13cb48e71dfcd1102a1697e098
MD5 85e24e9d2f20dc7bfc69c9ec2dfe2763
BLAKE2b-256 790abddbcf4f43a44d6d7794f180e88f2a80e861d1c8ed4504651cb2465a05bd

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