anydi 0.58.0__py3-none-any.whl → 0.59.0__py3-none-any.whl

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/ext/faststream.py CHANGED
@@ -3,16 +3,27 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import inspect
6
- from typing import Any, cast
6
+ from functools import cached_property
7
+ from typing import TYPE_CHECKING, Any, cast
7
8
 
8
- from fast_depends.dependencies import Depends
9
- from faststream import ContextRepo
10
- from faststream.broker.core.usecase import BrokerUsecase
9
+ from fast_depends.dependencies import Dependant
10
+ from faststream import BaseMiddleware, ContextRepo, StreamMessage
11
11
 
12
12
  from anydi import Container
13
13
  from anydi._types import Inject, ProvideMarker, set_provide_factory
14
14
 
15
- __all__ = ["install", "get_container", "Inject"]
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
+ ]
16
27
 
17
28
 
18
29
  def get_container(broker: BrokerUsecase[Any, Any]) -> Container:
@@ -20,13 +31,29 @@ def get_container(broker: BrokerUsecase[Any, Any]) -> Container:
20
31
  return cast(Container, getattr(broker, "_container")) # noqa
21
32
 
22
33
 
23
- class _ProvideMarker(Depends, ProvideMarker):
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):
24
51
  def __init__(self) -> None:
25
- super().__init__(dependency=self._dependency, use_cache=True, cast=True)
52
+ super().__init__(self._dependency, use_cache=True, cast=True, cast_result=True)
26
53
  ProvideMarker.__init__(self)
27
54
 
28
55
  async def _dependency(self, context: ContextRepo) -> Any:
29
- container = get_container(context.get("broker"))
56
+ container = get_container_from_context(context)
30
57
  return await container.aresolve(self.interface)
31
58
 
32
59
 
@@ -35,12 +62,7 @@ set_provide_factory(_ProvideMarker)
35
62
 
36
63
 
37
64
  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
- ]
65
+ return [subscriber.calls[0].handler for subscriber in broker.subscribers]
44
66
 
45
67
 
46
68
  def install(broker: BrokerUsecase[Any, Any], container: Container) -> None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: anydi
3
- Version: 0.58.0
3
+ Version: 0.59.0
4
4
  Summary: Dependency Injection library
5
5
  Keywords: dependency injection,dependencies,di,async,asyncio,application
6
6
  Author: Anton Ruhlov
@@ -12,14 +12,14 @@ anydi/_types.py,sha256=tLJS27j0lWJFd4fIIGlGbbfKBVTPTnWgPYqZlenktis,2939
12
12
  anydi/ext/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
13
  anydi/ext/django/__init__.py,sha256=Ve8lncLU9dPY_Vjt4zihPgsSxwAtFHACn0XvBM5JG8k,367
14
14
  anydi/ext/fastapi.py,sha256=TQoS1Vh9chV0Tj0qyxjgqoGTYEMC3jp0il9CDZCZLcc,2326
15
- anydi/ext/faststream.py,sha256=XT80r1FGL-xlU7r8urm9sNpUfl4OPMJseW4dade_fR4,1836
15
+ anydi/ext/faststream.py,sha256=i6uXE21UawmTYDBqRv7yqDMc-lRiYSYf45sD87O2wX4,2442
16
16
  anydi/ext/pydantic_settings.py,sha256=jVJZ1wPaPpsxdNPlJj9yq282ebqLZ9tckWpZ0eIwWLg,1533
17
17
  anydi/ext/pytest_plugin.py,sha256=M54DkA-KxD9GqLnXdoCyn-Qur2c44MB6d0AgJuYCZ5w,16171
18
18
  anydi/ext/starlette/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
19
19
  anydi/ext/starlette/middleware.py,sha256=9CQtGg5ZzUz2gFSzJr8U4BWzwNjK8XMctm3n52M77Z0,792
20
20
  anydi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
21
21
  anydi/testing.py,sha256=cHg3mMScZbEep9smRqSNQ81BZMQOkyugHe8TvKdPnEg,1347
22
- anydi-0.58.0.dist-info/WHEEL,sha256=eh7sammvW2TypMMMGKgsM83HyA_3qQ5Lgg3ynoecH3M,79
23
- anydi-0.58.0.dist-info/entry_points.txt,sha256=AgOcQYM5KyS4D37QcYb00tiid0QA-pD1VrjHHq4QAps,44
24
- anydi-0.58.0.dist-info/METADATA,sha256=KE1cG2fc8ZyuoUBMhGP0pUqcUkr2z8ZOGjPVy3-ILV0,7901
25
- anydi-0.58.0.dist-info/RECORD,,
22
+ anydi-0.59.0.dist-info/WHEEL,sha256=eh7sammvW2TypMMMGKgsM83HyA_3qQ5Lgg3ynoecH3M,79
23
+ anydi-0.59.0.dist-info/entry_points.txt,sha256=AgOcQYM5KyS4D37QcYb00tiid0QA-pD1VrjHHq4QAps,44
24
+ anydi-0.59.0.dist-info/METADATA,sha256=Hp5PaKyiY5MTVyJiaUG7KH6B7n_74IXnxTUdRVypHUY,7901
25
+ anydi-0.59.0.dist-info/RECORD,,
File without changes