modern-di-fastapi 0.3.0__py3-none-any.whl → 0.4.1__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 +2 -1
- modern_di_fastapi/main.py +16 -6
- {modern_di_fastapi-0.3.0.dist-info → modern_di_fastapi-0.4.1.dist-info}/METADATA +1 -1
- modern_di_fastapi-0.4.1.dist-info/RECORD +6 -0
- modern_di_fastapi-0.3.0.dist-info/RECORD +0 -6
- {modern_di_fastapi-0.3.0.dist-info → modern_di_fastapi-0.4.1.dist-info}/WHEEL +0 -0
modern_di_fastapi/__init__.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
from modern_di_fastapi.main import Provide, fetch_di_container, setup_di
|
|
1
|
+
from modern_di_fastapi.main import Provide, build_di_container, fetch_di_container, setup_di
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
__all__ = [
|
|
5
5
|
"Provide",
|
|
6
|
+
"build_di_container",
|
|
6
7
|
"fetch_di_container",
|
|
7
8
|
"setup_di",
|
|
8
9
|
]
|
modern_di_fastapi/main.py
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import dataclasses
|
|
2
|
+
import enum
|
|
2
3
|
import typing
|
|
3
4
|
|
|
4
5
|
import fastapi
|
|
5
6
|
from modern_di import Container, Scope, providers
|
|
7
|
+
from starlette.requests import HTTPConnection
|
|
6
8
|
|
|
7
9
|
|
|
8
10
|
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
11
|
|
|
10
12
|
|
|
11
|
-
def setup_di(app: fastapi.FastAPI) -> Container:
|
|
12
|
-
app.state.di_container = Container(scope=
|
|
13
|
+
def setup_di(app: fastapi.FastAPI, scope: enum.IntEnum = Scope.APP) -> Container:
|
|
14
|
+
app.state.di_container = Container(scope=scope)
|
|
13
15
|
return app.state.di_container
|
|
14
16
|
|
|
15
17
|
|
|
@@ -17,9 +19,17 @@ def fetch_di_container(app: fastapi.FastAPI) -> Container:
|
|
|
17
19
|
return typing.cast(Container, app.state.di_container)
|
|
18
20
|
|
|
19
21
|
|
|
20
|
-
async def
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[Container]:
|
|
23
|
+
context: dict[str, typing.Any] = {}
|
|
24
|
+
scope: Scope | None = None
|
|
25
|
+
if isinstance(connection, fastapi.Request):
|
|
26
|
+
scope = Scope.REQUEST
|
|
27
|
+
context["request"] = connection
|
|
28
|
+
elif isinstance(connection, fastapi.WebSocket):
|
|
29
|
+
context["websocket"] = connection
|
|
30
|
+
scope = Scope.SESSION
|
|
31
|
+
container: Container = fetch_di_container(connection.app)
|
|
32
|
+
async with container.build_child_container(context=context, scope=scope) as request_container:
|
|
23
33
|
yield request_container
|
|
24
34
|
|
|
25
35
|
|
|
@@ -28,7 +38,7 @@ class Dependency(typing.Generic[T_co]):
|
|
|
28
38
|
dependency: providers.AbstractProvider[T_co]
|
|
29
39
|
|
|
30
40
|
async def __call__(
|
|
31
|
-
self, request_container: typing.Annotated[Container, fastapi.Depends(
|
|
41
|
+
self, request_container: typing.Annotated[Container, fastapi.Depends(build_di_container)]
|
|
32
42
|
) -> T_co:
|
|
33
43
|
return await self.dependency.async_resolve(request_container)
|
|
34
44
|
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
modern_di_fastapi/__init__.py,sha256=LSjAug5Mj6Bw7dAR7lWJpRjV9WMT8r9vSGJRHghXF_s,192
|
|
2
|
+
modern_di_fastapi/main.py,sha256=20znQkJZmsAmhehZ3-Cv2D13--SiCz2EkqjPuTkfpCI,1654
|
|
3
|
+
modern_di_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
modern_di_fastapi-0.4.1.dist-info/METADATA,sha256=rlULSVlNCw1r00_vaNDXruYNUgQirkpMJPwTNkitrCc,926
|
|
5
|
+
modern_di_fastapi-0.4.1.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
|
6
|
+
modern_di_fastapi-0.4.1.dist-info/RECORD,,
|
|
@@ -1,6 +0,0 @@
|
|
|
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,,
|
|
File without changes
|