modern-di-fastapi 1.1.0__tar.gz → 2.0.0a0__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: modern-di-fastapi
3
- Version: 1.1.0
3
+ Version: 2.0.0a0
4
4
  Summary: Modern-DI integration for FastAPI
5
5
  Project-URL: repository, https://github.com/modern-python/modern-di
6
6
  Project-URL: docs, https://modern-di.readthedocs.io
@@ -0,0 +1,46 @@
1
+ import dataclasses
2
+ import typing
3
+
4
+ import fastapi
5
+ from modern_di import Container, Scope, providers
6
+ from starlette.requests import HTTPConnection
7
+
8
+
9
+ T_co = typing.TypeVar("T_co", covariant=True)
10
+
11
+
12
+ def fetch_di_container(app_: fastapi.FastAPI) -> Container:
13
+ return typing.cast(Container, app_.state.di_container)
14
+
15
+
16
+ def setup_di(app: fastapi.FastAPI, container: Container) -> Container:
17
+ app.state.di_container = container
18
+ return container
19
+
20
+
21
+ async def build_di_container(connection: HTTPConnection) -> Container:
22
+ context: dict[type[typing.Any], typing.Any] = {}
23
+ scope: Scope | None = None
24
+ if isinstance(connection, fastapi.Request):
25
+ scope = Scope.REQUEST
26
+ context[fastapi.Request] = connection
27
+ elif isinstance(connection, fastapi.WebSocket):
28
+ context[fastapi.WebSocket] = connection
29
+ scope = Scope.SESSION
30
+ return fetch_di_container(connection.app).build_child_container(context=context, scope=scope)
31
+
32
+
33
+ @dataclasses.dataclass(slots=True, frozen=True)
34
+ class Dependency(typing.Generic[T_co]):
35
+ dependency: providers.AbstractProvider[T_co] | type[T_co]
36
+
37
+ async def __call__(
38
+ self, request_container: typing.Annotated[Container, fastapi.Depends(build_di_container)]
39
+ ) -> T_co:
40
+ if isinstance(self.dependency, providers.AbstractProvider):
41
+ return request_container.resolve_provider(self.dependency)
42
+ return request_container.resolve(dependency_type=self.dependency)
43
+
44
+
45
+ def FromDI(dependency: providers.AbstractProvider[T_co] | type[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
46
+ return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
@@ -16,7 +16,7 @@ classifiers = [
16
16
  "Topic :: Software Development :: Libraries",
17
17
  ]
18
18
  dependencies = ["fastapi>=0.100", "modern-di>=1.0.0alpha"]
19
- version = "1.1.0"
19
+ version = "2.0.0a0"
20
20
 
21
21
  [project.urls]
22
22
  repository = "https://github.com/modern-python/modern-di"
@@ -1,61 +0,0 @@
1
- import contextlib
2
- import dataclasses
3
- import typing
4
-
5
- import fastapi
6
- from fastapi.routing import _merge_lifespan_context
7
- from modern_di import AsyncContainer, Scope, providers
8
- from starlette.requests import HTTPConnection
9
-
10
-
11
- T_co = typing.TypeVar("T_co", covariant=True)
12
-
13
-
14
- def fetch_di_container(app_: fastapi.FastAPI) -> AsyncContainer:
15
- return typing.cast(AsyncContainer, app_.state.di_container)
16
-
17
-
18
- @contextlib.asynccontextmanager
19
- async def _lifespan_manager(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
20
- async with fetch_di_container(app_):
21
- yield
22
-
23
-
24
- def setup_di(app: fastapi.FastAPI, container: AsyncContainer) -> AsyncContainer:
25
- app.state.di_container = container
26
- old_lifespan_manager = app.router.lifespan_context
27
- app.router.lifespan_context = _merge_lifespan_context(
28
- old_lifespan_manager,
29
- _lifespan_manager,
30
- )
31
- return container
32
-
33
-
34
- async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[AsyncContainer]:
35
- context: dict[type[typing.Any], typing.Any] = {}
36
- scope: Scope | None = None
37
- if isinstance(connection, fastapi.Request):
38
- scope = Scope.REQUEST
39
- context[fastapi.Request] = connection
40
- elif isinstance(connection, fastapi.WebSocket):
41
- context[fastapi.WebSocket] = connection
42
- scope = Scope.SESSION
43
- container: AsyncContainer = fetch_di_container(connection.app)
44
- async with container.build_child_container(context=context, scope=scope) as request_container:
45
- yield request_container
46
-
47
-
48
- @dataclasses.dataclass(slots=True, frozen=True)
49
- class Dependency(typing.Generic[T_co]):
50
- dependency: providers.AbstractProvider[T_co] | type[T_co]
51
-
52
- async def __call__(
53
- self, request_container: typing.Annotated[AsyncContainer, fastapi.Depends(build_di_container)]
54
- ) -> T_co:
55
- if isinstance(self.dependency, providers.AbstractProvider):
56
- return await request_container.resolve_provider(self.dependency)
57
- return await request_container.resolve(dependency_type=self.dependency)
58
-
59
-
60
- def FromDI(dependency: providers.AbstractProvider[T_co] | type[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
61
- return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))