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.0.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.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: spakky_event-5.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 80d954ac04bddd6ac585934401bb466d034baf58edb28e4997eb031688385813
MD5 aef7bdcfb5de2e9d5acc77ae0cc71559
BLAKE2b-256 dc197fd718abeb7da6e87adaf9ec79d7032eff6fee1f8cbfe8eae679c0b0586e

See more details on using hashes here.

Provenance

The following attestation bundles were made for spakky_event-5.0.0.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.0-py3-none-any.whl.

File metadata

  • Download URL: spakky_event-5.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e70f05dddfded143e3fc172641f4fd5672c271ac0af7a81f986b1c4d7d026ebe
MD5 b6e7a0d86107430de403b0d72dee2c9e
BLAKE2b-256 c1212533865ebde6225e5c144d34cb5d1694df03a34b00d37813edaebf6c725d

See more details on using hashes here.

Provenance

The following attestation bundles were made for spakky_event-5.0.0-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