Skip to main content

Event handling stereotype for Spakky Framework (@EventHandler, @on_event)

Project description

Spakky Event

Event handling stereotype for Spakky Framework.

Installation

pip install spakky-event

Features

  • @EventHandler: Stereotype for event handler classes
  • @on_event: Decorator for marking methods as event handlers
  • Type-safe: Full type hint support for event types
  • Async support: Native async/await for event processing

Quick Start

Define Events

Events should extend AbstractDomainEvent from spakky-domain:

from dataclasses import dataclass

from spakky.domain.models.event import AbstractDomainEvent


@dataclass
class UserCreatedEvent(AbstractDomainEvent):
    user_id: str
    email: str


@dataclass
class UserDeletedEvent(AbstractDomainEvent):
    user_id: str

Create Event Handler

Use @EventHandler stereotype with @on_event decorators:

from spakky.event.stereotype.event_handler import EventHandler, on_event


@EventHandler()
class UserEventHandler:
    def __init__(self, notification_service: NotificationService) -> None:
        self.notification_service = notification_service

    @on_event(UserCreatedEvent)
    async def on_user_created(self, event: UserCreatedEvent) -> None:
        await self.notification_service.send_welcome_email(event.email)

    @on_event(UserDeletedEvent)
    async def on_user_deleted(self, event: UserDeletedEvent) -> None:
        await self.notification_service.send_goodbye_email(event.user_id)

Integration with Message Brokers

The @EventHandler stereotype works with Spakky's message broker plugins:

RabbitMQ

pip install spakky-rabbitmq
from spakky.core.application.application import SpakkyApplication
from spakky.core.application.application_context import ApplicationContext

app = (
    SpakkyApplication(ApplicationContext())
    .load_plugins()  # Loads spakky-rabbitmq plugin
    .scan()
    .start()
)

Kafka

pip install spakky-kafka
from spakky.core.application.application import SpakkyApplication
from spakky.core.application.application_context import ApplicationContext

app = (
    SpakkyApplication(ApplicationContext())
    .load_plugins()  # Loads spakky-kafka plugin
    .scan()
    .start()
)

API Reference

Stereotypes

Decorator Description
@EventHandler() Marks a class as an event handler (extends @Pod)
@on_event(EventType) Marks a method as handler for specific event type

Interfaces

Class Description
IEventPublisher Sync event publisher interface
IAsyncEventPublisher Async event publisher interface
IEventConsumer Sync event consumer interface
IAsyncEventConsumer Async event consumer interface

Types

Type Description
EventRoute Annotation class for event routing metadata
DomainEventT Type variable bound to AbstractDomainEvent
IEventHandlerCallback Type alias for event handler callbacks

Errors

Class Description
AbstractSpakkyEventError Base error for event operations
DuplicateEventHandlerError Raised when duplicate handlers registered
InvalidMessageError Raised when message is malformed

Related Packages

Package Description
spakky-domain DDD building blocks including AbstractDomainEvent
spakky-rabbitmq RabbitMQ implementation of event publisher/consumer
spakky-kafka Kafka implementation of event publisher/consumer

License

MIT License

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

spakky_event-5.0.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spakky_event-5.0.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file spakky_event-5.0.1.tar.gz.

File metadata

  • Download URL: spakky_event-5.0.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spakky_event-5.0.1.tar.gz
Algorithm Hash digest
SHA256 36d806882807e7d142ab9c5caf517e769da1647075e14da1b4e15a0fdb6f773c
MD5 c9ad2f2ee34fb30fa9811e40d6e6fce3
BLAKE2b-256 e3078ddb674379d6d9014f7e64ec3ca9c9faf7bfd3d17fa4ab714e98c610ac3c

See more details on using hashes here.

Provenance

The following attestation bundles were made for spakky_event-5.0.1.tar.gz:

Publisher: release.yml on E5presso/spakky-framework

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spakky_event-5.0.1-py3-none-any.whl.

File metadata

  • Download URL: spakky_event-5.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spakky_event-5.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e3d506a967ac5ea886343a0e2c1170e0238ee556542b8420ef13ad14ce7c6abd
MD5 e06076cd14fd1ce6716836bdfe12b30b
BLAKE2b-256 3ad50273969532c23803f332dfdddabf20ffc5fc8d4f54da9d5709b6786e21d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for spakky_event-5.0.1-py3-none-any.whl:

Publisher: release.yml on E5presso/spakky-framework

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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