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 one of the event base classes from spakky-domain:
- Use
AbstractDomainEventfor events within a bounded context (in-process domain events) - Use
AbstractIntegrationEventfor cross-boundary events (message broker integration)
When using message broker plugins (RabbitMQ / Kafka), define events as AbstractIntegrationEvent.
from spakky.core.common.mutability import immutable
from spakky.domain.models.event import AbstractIntegrationEvent
@immutable
class UserCreatedEvent(AbstractIntegrationEvent):
user_id: str
email: str
@immutable
class UserDeletedEvent(AbstractIntegrationEvent):
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
| Interface | Description |
|---|---|
IEventPublisher / IAsyncEventPublisher |
Event publish entry point (type-based routing) |
IEventBus / IAsyncEventBus |
Integration event send entry point (Outbox seam) |
IEventTransport / IAsyncEventTransport |
Actual message broker transport |
IEventConsumer / IAsyncEventConsumer |
Handler callback registration |
IEventDispatcher / IAsyncEventDispatcher |
In-process handler dispatch |
Implementations
| Class | Description |
|---|---|
EventMediator / AsyncEventMediator |
Consumer + Dispatcher combined (in-process) |
EventPublisher / AsyncEventPublisher |
Type-based router (DomainEvent→Mediator, IntegrationEvent→EventBus) |
DirectEventBus / AsyncDirectEventBus |
Default EventBus → EventTransport delegation |
EventHandlerRegistrationPostProcessor |
Auto-registers @EventHandler methods |
Types
| Type | Description |
|---|---|
EventRoute |
Annotation class for event routing metadata |
EventT_contra |
TypeVar bound to AbstractEvent |
EventHandlerCallback |
Type alias for sync event callbacks |
AsyncEventHandlerCallback |
Type alias for async event callbacks |
Errors
| Class | Description |
|---|---|
AbstractSpakkyEventError |
Base error for event operations |
InvalidMessageError |
Raised when message is malformed |
Related Packages
| Package | Description |
|---|---|
spakky-domain |
DDD building blocks including AbstractEvent, AbstractDomainEvent, AbstractIntegrationEvent |
spakky-rabbitmq |
RabbitMQ transport (RabbitMQEventTransport) |
spakky-kafka |
Kafka transport (KafkaEventTransport) |
In-process Domain Event Publishing
For events within a bounded context (DomainEvents), use the in-process publisher:
from spakky.core.application.application import SpakkyApplication
from spakky.core.application.application_context import ApplicationContext
from spakky.event import IAsyncEventPublisher
# Bootstrap application (load_plugins auto-registers event components)
app = (
SpakkyApplication(ApplicationContext())
.load_plugins()
.scan()
.start()
)
# Get publisher from container
publisher = app.container.get(IAsyncEventPublisher)
await publisher.publish(UserCreatedEvent(user_id="123", email="test@example.com"))
Architecture (ISP Compliant)
The in-process event system follows Interface Segregation Principle:
- Consumer: Registers event handlers (
register()method) - Dispatcher: Dispatches events to handlers (
dispatch()method) - Mediator: Combines both interfaces in a single implementation
- Publisher: Depends only on Dispatcher (not Consumer)
┌─────────────────┐ ┌─────────────────┐
│ Publisher │────▶│ Dispatcher │
└─────────────────┘ └────────┬────────┘
│
┌────────▼────────┐
│ Mediator │
│ (Consumer + │
│ Dispatcher) │
└────────┬────────┘
│
┌────────▼────────┐
│ EventHandler │
│ @on_event() │
└─────────────────┘
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spakky_event-6.1.1.tar.gz.
File metadata
- Download URL: spakky_event-6.1.1.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
671df8e3a7270f50bc8dbe2764eb5ef28f0f96f4aa20aade037424c5aae606f3
|
|
| MD5 |
505f46a89b4d7bb030a6baccd8d2245c
|
|
| BLAKE2b-256 |
b5174f9f12da8b410072e53687a8d943c414c47a92bf946ef61f17d349c9f8b2
|
Provenance
The following attestation bundles were made for spakky_event-6.1.1.tar.gz:
Publisher:
release.yml on E5presso/spakky-framework
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spakky_event-6.1.1.tar.gz -
Subject digest:
671df8e3a7270f50bc8dbe2764eb5ef28f0f96f4aa20aade037424c5aae606f3 - Sigstore transparency entry: 1110094122
- Sigstore integration time:
-
Permalink:
E5presso/spakky-framework@193a772f1b560c3aec0863e54f0a0fb25e12a47b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/E5presso
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@193a772f1b560c3aec0863e54f0a0fb25e12a47b -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file spakky_event-6.1.1-py3-none-any.whl.
File metadata
- Download URL: spakky_event-6.1.1-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d191c6beb07cb60e97226eb0ed1008e9b05f0fc211623add1867d9da77d3adc2
|
|
| MD5 |
82a719e4676fea9f2a104347d925a0d2
|
|
| BLAKE2b-256 |
97a55068706db2037ebb9342ad40b2d604316943035c48ceb8c9a036215aaca2
|
Provenance
The following attestation bundles were made for spakky_event-6.1.1-py3-none-any.whl:
Publisher:
release.yml on E5presso/spakky-framework
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spakky_event-6.1.1-py3-none-any.whl -
Subject digest:
d191c6beb07cb60e97226eb0ed1008e9b05f0fc211623add1867d9da77d3adc2 - Sigstore transparency entry: 1110094133
- Sigstore integration time:
-
Permalink:
E5presso/spakky-framework@193a772f1b560c3aec0863e54f0a0fb25e12a47b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/E5presso
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@193a772f1b560c3aec0863e54f0a0fb25e12a47b -
Trigger Event:
workflow_dispatch
-
Statement type: