modern-di-fastapi 0.1.4__tar.gz → 0.2.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: modern-di-fastapi
3
- Version: 0.1.4
3
+ Version: 0.2.0
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,9 @@
1
+ from modern_di_fastapi.main import FromDI, enter_di_request_scope, fetch_di_container, save_di_container
2
+
3
+
4
+ __all__ = [
5
+ "FromDI",
6
+ "enter_di_request_scope",
7
+ "fetch_di_container",
8
+ "save_di_container",
9
+ ]
@@ -0,0 +1,38 @@
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
@@ -11,20 +11,19 @@ from modern_di import Scope, providers
11
11
  from starlette import status
12
12
  from starlette.requests import Request
13
13
 
14
- import modern_di_fastapi
15
- from modern_di_fastapi import ContainerMiddleware, FromDI
14
+ from modern_di_fastapi import FromDI, save_di_container
15
+ from modern_di_fastapi.main import enter_di_request_scope
16
16
 
17
17
 
18
18
  @contextlib.asynccontextmanager
19
19
  async def lifespan(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
20
20
  di_container = modern_di.Container(scope=modern_di.Scope.APP)
21
- modern_di_fastapi.setup_modern_di(container=di_container, app=app_)
21
+ save_di_container(app_, di_container)
22
22
  async with di_container:
23
23
  yield
24
24
 
25
25
 
26
- app = fastapi.FastAPI(lifespan=lifespan)
27
- app.add_middleware(ContainerMiddleware)
26
+ app = fastapi.FastAPI(lifespan=lifespan, dependencies=[fastapi.Depends(enter_di_request_scope)])
28
27
 
29
28
 
30
29
  @dataclasses.dataclass(kw_only=True, slots=True)
@@ -60,7 +59,7 @@ async def read_root(
60
59
  @pytest.fixture(scope="session")
61
60
  async def client() -> typing.AsyncIterator[httpx.AsyncClient]:
62
61
  async with LifespanManager(app):
63
- yield httpx.AsyncClient(app=app, base_url="http://test")
62
+ yield httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test")
64
63
 
65
64
 
66
65
  async def test_read_main(client: httpx.AsyncClient) -> None:
@@ -1,9 +0,0 @@
1
- from modern_di_fastapi.depends import FromDI, setup_modern_di
2
- from modern_di_fastapi.middleware import ContainerMiddleware
3
-
4
-
5
- __all__ = [
6
- "ContainerMiddleware",
7
- "FromDI",
8
- "setup_modern_di",
9
- ]
@@ -1,24 +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
- @dataclasses.dataclass(slots=True, frozen=True)
12
- class Dependency(typing.Generic[T_co]):
13
- dependency: providers.AbstractProvider[T_co]
14
-
15
- async def __call__(self, request: fastapi.Request) -> T_co:
16
- return await self.dependency.async_resolve(request.state.modern_di_container)
17
-
18
-
19
- def FromDI(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
20
- return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
21
-
22
-
23
- def setup_modern_di(container: Container, app: fastapi.FastAPI) -> None:
24
- app.state.modern_di_container = container
@@ -1,27 +0,0 @@
1
- import typing
2
-
3
- import modern_di
4
- from starlette.requests import Request
5
- from starlette.types import ASGIApp, Receive, Scope, Send
6
-
7
-
8
- class ContainerMiddleware:
9
- def __init__(self, app: ASGIApp) -> None:
10
- self.app = app
11
-
12
- async def __call__(
13
- self,
14
- scope: Scope,
15
- receive: Receive,
16
- send: Send,
17
- ) -> None:
18
- if scope["type"] != "http":
19
- return await self.app(scope, receive, send)
20
-
21
- request = Request(scope, receive=receive, send=send)
22
- context: dict[str, typing.Any] = {"request": request}
23
-
24
- container: modern_di.Container = request.app.state.modern_di_container
25
- async with container.build_child_container(context=context) as request_container:
26
- request.state.modern_di_container = request_container
27
- return await self.app(scope, receive, send)