modern-di-fastapi 0.6.0__py3-none-any.whl → 0.7.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/main.py
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
import contextlib
|
|
2
2
|
import dataclasses
|
|
3
|
-
import enum
|
|
4
3
|
import typing
|
|
5
4
|
|
|
6
5
|
import fastapi
|
|
7
6
|
from fastapi.routing import _merge_lifespan_context
|
|
8
|
-
from modern_di import
|
|
7
|
+
from modern_di import AsyncContainer, Scope, providers
|
|
9
8
|
from starlette.requests import HTTPConnection
|
|
10
9
|
|
|
11
10
|
|
|
12
11
|
T_co = typing.TypeVar("T_co", covariant=True)
|
|
13
12
|
|
|
14
13
|
|
|
15
|
-
def fetch_di_container(app_: fastapi.FastAPI) ->
|
|
16
|
-
return typing.cast(
|
|
14
|
+
def fetch_di_container(app_: fastapi.FastAPI) -> AsyncContainer:
|
|
15
|
+
return typing.cast(AsyncContainer, app_.state.di_container)
|
|
17
16
|
|
|
18
17
|
|
|
19
18
|
@contextlib.asynccontextmanager
|
|
@@ -22,9 +21,9 @@ async def _lifespan_manager(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]
|
|
|
22
21
|
yield
|
|
23
22
|
|
|
24
23
|
|
|
25
|
-
def setup_di(app: fastapi.FastAPI, scope:
|
|
24
|
+
def setup_di(app: fastapi.FastAPI, scope: Scope = Scope.APP, container: AsyncContainer | None = None) -> AsyncContainer:
|
|
26
25
|
if not container:
|
|
27
|
-
container =
|
|
26
|
+
container = AsyncContainer(scope=scope)
|
|
28
27
|
app.state.di_container = container
|
|
29
28
|
old_lifespan_manager = app.router.lifespan_context
|
|
30
29
|
app.router.lifespan_context = _merge_lifespan_context(
|
|
@@ -34,16 +33,16 @@ def setup_di(app: fastapi.FastAPI, scope: enum.IntEnum = Scope.APP, container: C
|
|
|
34
33
|
return container
|
|
35
34
|
|
|
36
35
|
|
|
37
|
-
async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[
|
|
38
|
-
context: dict[
|
|
36
|
+
async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[AsyncContainer]:
|
|
37
|
+
context: dict[type[typing.Any], typing.Any] = {}
|
|
39
38
|
scope: Scope | None = None
|
|
40
39
|
if isinstance(connection, fastapi.Request):
|
|
41
40
|
scope = Scope.REQUEST
|
|
42
|
-
context[
|
|
41
|
+
context[fastapi.Request] = connection
|
|
43
42
|
elif isinstance(connection, fastapi.WebSocket):
|
|
44
|
-
context[
|
|
43
|
+
context[fastapi.WebSocket] = connection
|
|
45
44
|
scope = Scope.SESSION
|
|
46
|
-
container:
|
|
45
|
+
container: AsyncContainer = fetch_di_container(connection.app)
|
|
47
46
|
async with container.build_child_container(context=context, scope=scope) as request_container:
|
|
48
47
|
yield request_container
|
|
49
48
|
|
|
@@ -53,9 +52,9 @@ class Dependency(typing.Generic[T_co]):
|
|
|
53
52
|
dependency: providers.AbstractProvider[T_co]
|
|
54
53
|
|
|
55
54
|
async def __call__(
|
|
56
|
-
self, request_container: typing.Annotated[
|
|
55
|
+
self, request_container: typing.Annotated[AsyncContainer, fastapi.Depends(build_di_container)]
|
|
57
56
|
) -> T_co:
|
|
58
|
-
return await self.dependency
|
|
57
|
+
return await request_container.resolve_provider(self.dependency)
|
|
59
58
|
|
|
60
59
|
|
|
61
60
|
def FromDI(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
modern_di_fastapi/__init__.py,sha256=t4ox50Pc4k7bDNprfg3_ujqj4dwidTTuzV9WRNH-EOs,190
|
|
2
|
+
modern_di_fastapi/main.py,sha256=LR1vN9_x76qnMpidoCBkpFp6rpi3rAPtOys4F7XIMYA,2202
|
|
3
|
+
modern_di_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
modern_di_fastapi-0.7.0.dist-info/METADATA,sha256=VWAQRGXvCuOCxsAV7XJLePORVL7edRo9XJPxqGA1S0Y,926
|
|
5
|
+
modern_di_fastapi-0.7.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
6
|
+
modern_di_fastapi-0.7.0.dist-info/RECORD,,
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
modern_di_fastapi/__init__.py,sha256=t4ox50Pc4k7bDNprfg3_ujqj4dwidTTuzV9WRNH-EOs,190
|
|
2
|
-
modern_di_fastapi/main.py,sha256=9P8ljSnIN86KeTkL7me7pm5iT_r8DHW3s6EIbqONEzo,2148
|
|
3
|
-
modern_di_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
-
modern_di_fastapi-0.6.0.dist-info/METADATA,sha256=qBOKjLpu4jR-MQx0fbtPxthvcEAyrQgDXQqZZ-FCktE,926
|
|
5
|
-
modern_di_fastapi-0.6.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
6
|
-
modern_di_fastapi-0.6.0.dist-info/RECORD,,
|
|
File without changes
|