modern-di-fastapi 0.1.4__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: modern-di-fastapi
3
- Version: 0.1.4
3
+ Version: 0.3.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,8 @@
1
+ from modern_di_fastapi.main import Provide, fetch_di_container, setup_di
2
+
3
+
4
+ __all__ = [
5
+ "Provide",
6
+ "fetch_di_container",
7
+ "setup_di",
8
+ ]
@@ -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,27 +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
- import modern_di_fastapi
15
- from modern_di_fastapi import ContainerMiddleware, FromDI
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
- di_container = modern_di.Container(scope=modern_di.Scope.APP)
21
- modern_di_fastapi.setup_modern_di(container=di_container, app=app_)
22
- async with di_container:
18
+ container = setup_di(app_)
19
+ async with container:
23
20
  yield
24
21
 
25
22
 
26
23
  app = fastapi.FastAPI(lifespan=lifespan)
27
- app.add_middleware(ContainerMiddleware)
28
24
 
29
25
 
30
26
  @dataclasses.dataclass(kw_only=True, slots=True)
@@ -48,9 +44,9 @@ context_adapter = providers.ContextAdapter(Scope.REQUEST, context_adapter_functi
48
44
 
49
45
  @app.get("/")
50
46
  async def read_root(
51
- app_factory_instance: typing.Annotated[SimpleCreator, FromDI(app_factory)],
52
- request_factory_instance: typing.Annotated[DependentCreator, FromDI(request_factory)],
53
- method: typing.Annotated[str, FromDI(context_adapter)],
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)],
54
50
  ) -> str:
55
51
  assert isinstance(app_factory_instance, SimpleCreator)
56
52
  assert isinstance(request_factory_instance, DependentCreator)
@@ -60,7 +56,7 @@ async def read_root(
60
56
  @pytest.fixture(scope="session")
61
57
  async def client() -> typing.AsyncIterator[httpx.AsyncClient]:
62
58
  async with LifespanManager(app):
63
- yield httpx.AsyncClient(app=app, base_url="http://test")
59
+ yield httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test")
64
60
 
65
61
 
66
62
  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)