modern-di integration for FastStream
Project description
modern-di-faststream
Modern-DI integration for FastStream.
Installation
uv add modern-di-faststream # or: pip install modern-di-faststream
Usage
setup_di registers the container and installs a broker middleware that builds a per-message child container; FromDI resolves a provider (or type) into a subscriber parameter.
import dataclasses
import faststream
from faststream.nats import NatsBroker
from modern_di import Container, Group, Scope, providers
from modern_di_faststream import FromDI, setup_di
@dataclasses.dataclass(kw_only=True)
class Settings:
debug: bool = True
@dataclasses.dataclass(kw_only=True)
class GreetingHandler:
settings: Settings # auto-injected by type
class Dependencies(Group):
settings = providers.Factory(scope=Scope.APP, creator=Settings)
handler = providers.Factory(scope=Scope.REQUEST, creator=GreetingHandler)
broker = NatsBroker()
app = faststream.FastStream(broker)
container = Container(groups=[Dependencies], validate=True)
setup_di(app, container)
@broker.subscriber("greetings")
async def handle(name: str, handler: GreetingHandler = FromDI(Dependencies.handler)) -> None:
print(name, handler.settings.debug)
The current StreamMessage is resolvable within DI via the pre-built faststream_message_provider context provider.
API
setup_di(app, container)— stores the container in the app context, registers a shutdown hook, and adds the DI middleware to the brokerFromDI(dependency, *, use_cache=True, cast=False)— FastStreamDependsthat resolves a provider (or type) from the per-message child containerfetch_di_container(app)— returns the app-scoped container from the app contextfaststream_message_provider—ContextProviderfor the currentfaststream.StreamMessage
📦 PyPI
📝 License
Part of modern-python
Built on modern-di, a dependency-injection framework with IoC container and scopes.
Browse the full list of templates and libraries in
modern-python — see the org profile for the categorized index.
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 modern_di_faststream-2.8.3.tar.gz.
File metadata
- Download URL: modern_di_faststream-2.8.3.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45a662501e04fe3aad068bd7aa2da93915aca50ed8cad9cf2243fba4ab94817e
|
|
| MD5 |
a2ee6c2a6daa36db76468dc14f5021ba
|
|
| BLAKE2b-256 |
4c00e80e07877611a000fdaaa1a79b03b36e7ec8372565cb1fa34c609996893c
|
File details
Details for the file modern_di_faststream-2.8.3-py3-none-any.whl.
File metadata
- Download URL: modern_di_faststream-2.8.3-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8010052371a7eba4a2e4743f5544fc405991dd30654545478acdb09572c0fd80
|
|
| MD5 |
82eb030886b8d39d5b9e6ed4d1058993
|
|
| BLAKE2b-256 |
4f50f988554e2c550461ef95bab2157d7336633533575f3e6de3a6251f979eaa
|