Apache Kafka plugin for Spakky framework
Project description
Spakky Kafka
Apache Kafka plugin for Spakky Framework.
Installation
pip install spakky-kafka
Or install via Spakky extras:
pip install spakky[kafka]
Configuration
Set environment variables with the SPAKKY_KAFKA__ prefix:
export SPAKKY_KAFKA__GROUP_ID="my-consumer-group"
export SPAKKY_KAFKA__CLIENT_ID="my-app"
export SPAKKY_KAFKA__BOOTSTRAP_SERVERS="localhost:9092"
export SPAKKY_KAFKA__AUTO_OFFSET_RESET="earliest" # earliest, latest, none
SASL Authentication (Optional)
export SPAKKY_KAFKA__SECURITY_PROTOCOL="SASL_SSL"
export SPAKKY_KAFKA__SASL_MECHANISM="PLAIN"
export SPAKKY_KAFKA__SASL_USERNAME="username"
export SPAKKY_KAFKA__SASL_PASSWORD="password"
Topic Configuration (Optional)
export SPAKKY_KAFKA__NUMBER_OF_PARTITIONS="3"
export SPAKKY_KAFKA__REPLICATION_FACTOR="1"
Usage
Event Publishing
from spakky.core.common.mutability import immutable
from spakky.domain.models.event import AbstractIntegrationEvent
from spakky.event.event_publisher import IEventPublisher
from spakky.core.pod.annotations.pod import Pod
@immutable
class UserCreatedEvent(AbstractIntegrationEvent):
user_id: int
email: str
@Pod()
class UserService:
def __init__(self, publisher: IEventPublisher) -> None:
self.publisher = publisher
def create_user(self, email: str) -> User:
user = User(email=email)
self.publisher.publish(UserCreatedEvent(user_id=user.id, email=email))
return user
Event Consuming
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)
Async Variants
For async applications, use IAsyncEventPublisher:
from spakky.event.event_publisher import IAsyncEventPublisher
@Pod()
class AsyncUserService:
def __init__(self, publisher: IAsyncEventPublisher) -> None:
self.publisher = publisher
async def create_user(self, email: str) -> User:
user = User(email=email)
await self.publisher.publish(UserCreatedEvent(user_id=user.id, email=email))
return user
Features
- Automatic topic creation: Topics are created based on event type names
- Sync and Async support: Both synchronous and asynchronous publishers/consumers
- Background service pattern: Consumer polling runs as a background service
- Pydantic serialization: Events are serialized/deserialized using Pydantic
- Confluent Kafka client: Built on the robust
confluent-kafkalibrary
Components
| Component | Description |
|---|---|
KafkaEventTransport |
Synchronous event transport (IEventTransport) |
AsyncKafkaEventTransport |
Asynchronous event transport (IAsyncEventTransport) |
KafkaEventConsumer |
Synchronous event consumer (background service) |
AsyncKafkaEventConsumer |
Asynchronous event consumer (background service) |
KafkaConnectionConfig |
Configuration via environment variables |
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_kafka-6.1.3.tar.gz.
File metadata
- Download URL: spakky_kafka-6.1.3.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e9023e3013bdfba77cf8205a4d14c988fda627f33a735edb116df2263c0c6f3
|
|
| MD5 |
e601f5acb02fe9d3a3cbdca61bc9da09
|
|
| BLAKE2b-256 |
3a8dc0142842bd4a399ef5f5734e6fd0be1266c026f3d94faf874cd30a83835e
|
Provenance
The following attestation bundles were made for spakky_kafka-6.1.3.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_kafka-6.1.3.tar.gz -
Subject digest:
2e9023e3013bdfba77cf8205a4d14c988fda627f33a735edb116df2263c0c6f3 - Sigstore transparency entry: 1110385510
- Sigstore integration time:
-
Permalink:
E5presso/spakky-framework@e597ac5d16b5f9a964808fc66f1f6cc0a7d5df59 -
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@e597ac5d16b5f9a964808fc66f1f6cc0a7d5df59 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file spakky_kafka-6.1.3-py3-none-any.whl.
File metadata
- Download URL: spakky_kafka-6.1.3-py3-none-any.whl
- Upload date:
- Size: 11.0 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 |
7af04f863fbe7eca569378feb8f7d6d8f7e006047bf8d9f96fadacb986bc52f7
|
|
| MD5 |
acfc8e948f2214b34cc710793de305bd
|
|
| BLAKE2b-256 |
0089ff1a3cd3e3dd8d36e0e17d176e8a20112b891e8bf91df84801365842259e
|
Provenance
The following attestation bundles were made for spakky_kafka-6.1.3-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_kafka-6.1.3-py3-none-any.whl -
Subject digest:
7af04f863fbe7eca569378feb8f7d6d8f7e006047bf8d9f96fadacb986bc52f7 - Sigstore transparency entry: 1110385770
- Sigstore integration time:
-
Permalink:
E5presso/spakky-framework@e597ac5d16b5f9a964808fc66f1f6cc0a7d5df59 -
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@e597ac5d16b5f9a964808fc66f1f6cc0a7d5df59 -
Trigger Event:
workflow_dispatch
-
Statement type: