anydi 0.58.0__tar.gz → 0.59.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {anydi-0.58.0 → anydi-0.59.0}/PKG-INFO +1 -1
- anydi-0.59.0/anydi/ext/faststream.py +74 -0
- {anydi-0.58.0 → anydi-0.59.0}/pyproject.toml +2 -2
- anydi-0.58.0/anydi/ext/faststream.py +0 -52
- {anydi-0.58.0 → anydi-0.59.0}/README.md +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/__init__.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/_async_lock.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/_container.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/_context.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/_decorators.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/_injector.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/_module.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/_provider.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/_resolver.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/_scanner.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/_types.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/ext/__init__.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/ext/django/__init__.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/ext/fastapi.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/ext/pydantic_settings.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/ext/pytest_plugin.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/ext/starlette/__init__.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/ext/starlette/middleware.py +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/py.typed +0 -0
- {anydi-0.58.0 → anydi-0.59.0}/anydi/testing.py +0 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""AnyDI FastStream extension."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import inspect
|
|
6
|
+
from functools import cached_property
|
|
7
|
+
from typing import TYPE_CHECKING, Any, cast
|
|
8
|
+
|
|
9
|
+
from fast_depends.dependencies import Dependant
|
|
10
|
+
from faststream import BaseMiddleware, ContextRepo, StreamMessage
|
|
11
|
+
|
|
12
|
+
from anydi import Container
|
|
13
|
+
from anydi._types import Inject, ProvideMarker, set_provide_factory
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from faststream._internal.basic_types import AsyncFuncAny
|
|
17
|
+
from faststream._internal.broker import BrokerUsecase
|
|
18
|
+
from faststream._internal.types import AnyMsg
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"install",
|
|
22
|
+
"get_container",
|
|
23
|
+
"get_container_from_context",
|
|
24
|
+
"Inject",
|
|
25
|
+
"RequestScopedMiddleware",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def get_container(broker: BrokerUsecase[Any, Any]) -> Container:
|
|
30
|
+
"""Get the AnyDI container from a FastStream broker."""
|
|
31
|
+
return cast(Container, getattr(broker, "_container")) # noqa
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def get_container_from_context(context: ContextRepo) -> Container:
|
|
35
|
+
return get_container(context.broker)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class RequestScopedMiddleware(BaseMiddleware):
|
|
39
|
+
@cached_property
|
|
40
|
+
def container(self) -> Container:
|
|
41
|
+
return get_container_from_context(self.context)
|
|
42
|
+
|
|
43
|
+
async def consume_scope(
|
|
44
|
+
self, call_next: AsyncFuncAny, msg: StreamMessage[AnyMsg]
|
|
45
|
+
) -> Any:
|
|
46
|
+
async with self.container.arequest_context():
|
|
47
|
+
return await call_next(msg)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class _ProvideMarker(Dependant, ProvideMarker):
|
|
51
|
+
def __init__(self) -> None:
|
|
52
|
+
super().__init__(self._dependency, use_cache=True, cast=True, cast_result=True)
|
|
53
|
+
ProvideMarker.__init__(self)
|
|
54
|
+
|
|
55
|
+
async def _dependency(self, context: ContextRepo) -> Any:
|
|
56
|
+
container = get_container_from_context(context)
|
|
57
|
+
return await container.aresolve(self.interface)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
# Configure Inject() and Provide[T] to use FastStream-specific marker
|
|
61
|
+
set_provide_factory(_ProvideMarker)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _get_broker_handlers(broker: BrokerUsecase[Any, Any]) -> list[Any]:
|
|
65
|
+
return [subscriber.calls[0].handler for subscriber in broker.subscribers]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def install(broker: BrokerUsecase[Any, Any], container: Container) -> None:
|
|
69
|
+
"""Install AnyDI into a FastStream broker."""
|
|
70
|
+
broker._container = container # type: ignore
|
|
71
|
+
for handler in _get_broker_handlers(broker):
|
|
72
|
+
call = handler._original_call # noqa
|
|
73
|
+
for parameter in inspect.signature(call, eval_str=True).parameters.values():
|
|
74
|
+
container.validate_injected_parameter(parameter, call=call)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "anydi"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.59.0"
|
|
4
4
|
description = "Dependency Injection library"
|
|
5
5
|
authors = [{ name = "Anton Ruhlov", email = "antonruhlov@gmail.com" }]
|
|
6
6
|
requires-python = ">=3.10.0, <3.15"
|
|
@@ -58,7 +58,7 @@ dev = [
|
|
|
58
58
|
"starlette>=0.37.2",
|
|
59
59
|
"fastapi>=0.100.0",
|
|
60
60
|
"httpx>=0.26.0",
|
|
61
|
-
"faststream>=0.
|
|
61
|
+
"faststream>=0.6,<0.7",
|
|
62
62
|
"redis>=5.0.4,<6",
|
|
63
63
|
"pydantic-settings>=2.4.0,<3",
|
|
64
64
|
]
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
"""AnyDI FastStream extension."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
import inspect
|
|
6
|
-
from typing import Any, cast
|
|
7
|
-
|
|
8
|
-
from fast_depends.dependencies import Depends
|
|
9
|
-
from faststream import ContextRepo
|
|
10
|
-
from faststream.broker.core.usecase import BrokerUsecase
|
|
11
|
-
|
|
12
|
-
from anydi import Container
|
|
13
|
-
from anydi._types import Inject, ProvideMarker, set_provide_factory
|
|
14
|
-
|
|
15
|
-
__all__ = ["install", "get_container", "Inject"]
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
def get_container(broker: BrokerUsecase[Any, Any]) -> Container:
|
|
19
|
-
"""Get the AnyDI container from a FastStream broker."""
|
|
20
|
-
return cast(Container, getattr(broker, "_container")) # noqa
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
class _ProvideMarker(Depends, ProvideMarker):
|
|
24
|
-
def __init__(self) -> None:
|
|
25
|
-
super().__init__(dependency=self._dependency, use_cache=True, cast=True)
|
|
26
|
-
ProvideMarker.__init__(self)
|
|
27
|
-
|
|
28
|
-
async def _dependency(self, context: ContextRepo) -> Any:
|
|
29
|
-
container = get_container(context.get("broker"))
|
|
30
|
-
return await container.aresolve(self.interface)
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
# Configure Inject() and Provide[T] to use FastStream-specific marker
|
|
34
|
-
set_provide_factory(_ProvideMarker)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
def _get_broker_handlers(broker: BrokerUsecase[Any, Any]) -> list[Any]:
|
|
38
|
-
if (handlers := getattr(broker, "handlers", None)) is not None:
|
|
39
|
-
return [handler.calls[0][0] for handler in handlers.values()]
|
|
40
|
-
return [
|
|
41
|
-
subscriber.calls[0].handler
|
|
42
|
-
for subscriber in broker._subscribers.values() # noqa
|
|
43
|
-
]
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
def install(broker: BrokerUsecase[Any, Any], container: Container) -> None:
|
|
47
|
-
"""Install AnyDI into a FastStream broker."""
|
|
48
|
-
broker._container = container # type: ignore
|
|
49
|
-
for handler in _get_broker_handlers(broker):
|
|
50
|
-
call = handler._original_call # noqa
|
|
51
|
-
for parameter in inspect.signature(call, eval_str=True).parameters.values():
|
|
52
|
-
container.validate_injected_parameter(parameter, call=call)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|