modern-di-fastapi 0.2.0__py3-none-any.whl → 0.3.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.
- modern_di_fastapi/__init__.py +3 -4
- modern_di_fastapi/main.py +17 -18
- {modern_di_fastapi-0.2.0.dist-info → modern_di_fastapi-0.3.0.dist-info}/METADATA +1 -1
- modern_di_fastapi-0.3.0.dist-info/RECORD +6 -0
- modern_di_fastapi-0.2.0.dist-info/RECORD +0 -6
- {modern_di_fastapi-0.2.0.dist-info → modern_di_fastapi-0.3.0.dist-info}/WHEEL +0 -0
modern_di_fastapi/__init__.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
from modern_di_fastapi.main import
|
|
1
|
+
from modern_di_fastapi.main import Provide, fetch_di_container, setup_di
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
__all__ = [
|
|
5
|
-
"
|
|
6
|
-
"enter_di_request_scope",
|
|
5
|
+
"Provide",
|
|
7
6
|
"fetch_di_container",
|
|
8
|
-
"
|
|
7
|
+
"setup_di",
|
|
9
8
|
]
|
modern_di_fastapi/main.py
CHANGED
|
@@ -2,37 +2,36 @@ import dataclasses
|
|
|
2
2
|
import typing
|
|
3
3
|
|
|
4
4
|
import fastapi
|
|
5
|
-
from modern_di import Container, providers
|
|
5
|
+
from modern_di import Container, Scope, providers
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
def
|
|
12
|
-
|
|
11
|
+
def setup_di(app: fastapi.FastAPI) -> Container:
|
|
12
|
+
app.state.di_container = Container(scope=Scope.APP)
|
|
13
|
+
return app.state.di_container
|
|
13
14
|
|
|
14
15
|
|
|
15
|
-
def fetch_di_container(
|
|
16
|
-
return typing.cast(Container,
|
|
16
|
+
def fetch_di_container(app: fastapi.FastAPI) -> Container:
|
|
17
|
+
return typing.cast(Container, app.state.di_container)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
async def _build_request_container(request: fastapi.Request) -> typing.AsyncIterator[Container]:
|
|
21
|
+
container: Container = fetch_di_container(request.app)
|
|
22
|
+
async with container.build_child_container(context={"request": request}) as request_container:
|
|
23
|
+
yield request_container
|
|
17
24
|
|
|
18
25
|
|
|
19
26
|
@dataclasses.dataclass(slots=True, frozen=True)
|
|
20
27
|
class Dependency(typing.Generic[T_co]):
|
|
21
28
|
dependency: providers.AbstractProvider[T_co]
|
|
22
29
|
|
|
23
|
-
async def __call__(
|
|
24
|
-
|
|
30
|
+
async def __call__(
|
|
31
|
+
self, request_container: typing.Annotated[Container, fastapi.Depends(_build_request_container)]
|
|
32
|
+
) -> T_co:
|
|
33
|
+
return await self.dependency.async_resolve(request_container)
|
|
25
34
|
|
|
26
35
|
|
|
27
|
-
def
|
|
36
|
+
def Provide(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
|
|
28
37
|
return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
async def enter_di_request_scope(request: fastapi.Request) -> typing.AsyncIterator[None]:
|
|
32
|
-
container: Container = fetch_di_container(request.app)
|
|
33
|
-
async with container.build_child_container(context={"request": request}) as request_container:
|
|
34
|
-
save_di_container(request, request_container)
|
|
35
|
-
try:
|
|
36
|
-
yield
|
|
37
|
-
finally:
|
|
38
|
-
del request.state.di_container
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
modern_di_fastapi/__init__.py,sha256=krEdX8j8pMZHQuTXSo3dpdZFnCGqoNl7UGp_7HNHhYE,146
|
|
2
|
+
modern_di_fastapi/main.py,sha256=f3_rcs35Gxbam3wzxdqRsUv1zbwvNZPimc4o1fIyhS4,1261
|
|
3
|
+
modern_di_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
modern_di_fastapi-0.3.0.dist-info/METADATA,sha256=G5FrUvBRR5X6K7e6cgB9UbZGLb58gyWZE6dnvFw3uxA,926
|
|
5
|
+
modern_di_fastapi-0.3.0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
|
6
|
+
modern_di_fastapi-0.3.0.dist-info/RECORD,,
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
modern_di_fastapi/__init__.py,sha256=8Djsc0SZVfYQkXlkYpaZ6tcYVQvsK4kJXWwr1nEg4js,216
|
|
2
|
-
modern_di_fastapi/main.py,sha256=gX4amXitXg-zTqIQXComCgkcF5XkWDRkKqhxebM6uEI,1301
|
|
3
|
-
modern_di_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
-
modern_di_fastapi-0.2.0.dist-info/METADATA,sha256=0rwzKbi7rv7CEGDS9uHJgrgs2vgkC_EYH6mjf-fGlTs,926
|
|
5
|
-
modern_di_fastapi-0.2.0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
|
6
|
-
modern_di_fastapi-0.2.0.dist-info/RECORD,,
|
|
File without changes
|