modern-di-fastapi 0.2.0__tar.gz → 0.3.0__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.
- {modern_di_fastapi-0.2.0 → modern_di_fastapi-0.3.0}/PKG-INFO +1 -1
- modern_di_fastapi-0.3.0/modern_di_fastapi/__init__.py +8 -0
- modern_di_fastapi-0.3.0/modern_di_fastapi/main.py +37 -0
- {modern_di_fastapi-0.2.0 → modern_di_fastapi-0.3.0}/tests/test_fastapi_di.py +7 -10
- modern_di_fastapi-0.2.0/modern_di_fastapi/__init__.py +0 -9
- modern_di_fastapi-0.2.0/modern_di_fastapi/main.py +0 -38
- {modern_di_fastapi-0.2.0 → modern_di_fastapi-0.3.0}/.gitignore +0 -0
- {modern_di_fastapi-0.2.0 → modern_di_fastapi-0.3.0}/Justfile +0 -0
- {modern_di_fastapi-0.2.0 → modern_di_fastapi-0.3.0}/README.md +0 -0
- {modern_di_fastapi-0.2.0 → modern_di_fastapi-0.3.0}/modern_di_fastapi/py.typed +0 -0
- {modern_di_fastapi-0.2.0 → modern_di_fastapi-0.3.0}/pyproject.toml +0 -0
- {modern_di_fastapi-0.2.0 → modern_di_fastapi-0.3.0}/tests/__init__.py +0 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import dataclasses
|
|
2
|
+
import typing
|
|
3
|
+
|
|
4
|
+
import fastapi
|
|
5
|
+
from modern_di import Container, Scope, providers
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def setup_di(app: fastapi.FastAPI) -> Container:
|
|
12
|
+
app.state.di_container = Container(scope=Scope.APP)
|
|
13
|
+
return app.state.di_container
|
|
14
|
+
|
|
15
|
+
|
|
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
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclasses.dataclass(slots=True, frozen=True)
|
|
27
|
+
class Dependency(typing.Generic[T_co]):
|
|
28
|
+
dependency: providers.AbstractProvider[T_co]
|
|
29
|
+
|
|
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)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def Provide(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
|
|
37
|
+
return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
|
|
@@ -4,26 +4,23 @@ import typing
|
|
|
4
4
|
|
|
5
5
|
import fastapi
|
|
6
6
|
import httpx
|
|
7
|
-
import modern_di
|
|
8
7
|
import pytest
|
|
9
8
|
from asgi_lifespan import LifespanManager
|
|
10
9
|
from modern_di import Scope, providers
|
|
11
10
|
from starlette import status
|
|
12
11
|
from starlette.requests import Request
|
|
13
12
|
|
|
14
|
-
from modern_di_fastapi import
|
|
15
|
-
from modern_di_fastapi.main import enter_di_request_scope
|
|
13
|
+
from modern_di_fastapi import Provide, setup_di
|
|
16
14
|
|
|
17
15
|
|
|
18
16
|
@contextlib.asynccontextmanager
|
|
19
17
|
async def lifespan(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
async with di_container:
|
|
18
|
+
container = setup_di(app_)
|
|
19
|
+
async with container:
|
|
23
20
|
yield
|
|
24
21
|
|
|
25
22
|
|
|
26
|
-
app = fastapi.FastAPI(lifespan=lifespan
|
|
23
|
+
app = fastapi.FastAPI(lifespan=lifespan)
|
|
27
24
|
|
|
28
25
|
|
|
29
26
|
@dataclasses.dataclass(kw_only=True, slots=True)
|
|
@@ -47,9 +44,9 @@ context_adapter = providers.ContextAdapter(Scope.REQUEST, context_adapter_functi
|
|
|
47
44
|
|
|
48
45
|
@app.get("/")
|
|
49
46
|
async def read_root(
|
|
50
|
-
app_factory_instance: typing.Annotated[SimpleCreator,
|
|
51
|
-
request_factory_instance: typing.Annotated[DependentCreator,
|
|
52
|
-
method: typing.Annotated[str,
|
|
47
|
+
app_factory_instance: typing.Annotated[SimpleCreator, Provide(app_factory)],
|
|
48
|
+
request_factory_instance: typing.Annotated[DependentCreator, Provide(request_factory)],
|
|
49
|
+
method: typing.Annotated[str, Provide(context_adapter)],
|
|
53
50
|
) -> str:
|
|
54
51
|
assert isinstance(app_factory_instance, SimpleCreator)
|
|
55
52
|
assert isinstance(request_factory_instance, DependentCreator)
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import dataclasses
|
|
2
|
-
import typing
|
|
3
|
-
|
|
4
|
-
import fastapi
|
|
5
|
-
from modern_di import Container, providers
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def save_di_container(obj: fastapi.FastAPI | fastapi.Request, container: Container) -> None:
|
|
12
|
-
obj.state.di_container = container
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def fetch_di_container(obj: fastapi.FastAPI | fastapi.Request) -> Container:
|
|
16
|
-
return typing.cast(Container, obj.state.di_container)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
@dataclasses.dataclass(slots=True, frozen=True)
|
|
20
|
-
class Dependency(typing.Generic[T_co]):
|
|
21
|
-
dependency: providers.AbstractProvider[T_co]
|
|
22
|
-
|
|
23
|
-
async def __call__(self, request: fastapi.Request) -> T_co:
|
|
24
|
-
return await self.dependency.async_resolve(fetch_di_container(request))
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
def FromDI(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
|
|
28
|
-
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|