modern-di-fastapi 0.1.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.
@@ -0,0 +1,9 @@
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
+ ]
@@ -0,0 +1,24 @@
1
+ import dataclasses
2
+ import typing
3
+
4
+ import fastapi
5
+ import modern_di
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: modern_di.resolvers.AbstractResolver[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: modern_di.resolvers.AbstractResolver[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: modern_di.Container, app: fastapi.FastAPI) -> None:
24
+ app.state.modern_di_container = container
@@ -0,0 +1,27 @@
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)
@@ -0,0 +1,26 @@
1
+ Metadata-Version: 2.3
2
+ Name: modern-di-fastapi
3
+ Version: 0.1.0
4
+ Summary: Modern-DI integration for FastAPI
5
+ Project-URL: repository, https://github.com/modern-python/modern-di
6
+ Project-URL: docs, https://modern-di.readthedocs.io
7
+ Author-email: Artur Shiriev <me@shiriev.ru>
8
+ License-Expression: MIT
9
+ Keywords: DI,FastAPI,dependency injector,ioc-container,python
10
+ Classifier: Programming Language :: Python :: 3.10
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Topic :: Software Development :: Libraries
15
+ Classifier: Typing :: Typed
16
+ Requires-Python: <4,>=3.10
17
+ Requires-Dist: fastapi>=0.100
18
+ Requires-Dist: modern-di
19
+ Description-Content-Type: text/markdown
20
+
21
+ "Modern-DI-FastAPI"
22
+ ==
23
+
24
+ Integration of [Modern-DI](https://github.com/modern-python/modern-di) to FastAPI
25
+
26
+ 📚 [Documentation](https://modern-di.readthedocs.io)
@@ -0,0 +1,6 @@
1
+ modern_di_fastapi/__init__.py,sha256=L-WPazuGrU4OB-ME-1BxMtfK1n2H8cg5yDZPUSEYcgA,203
2
+ modern_di_fastapi/depends.py,sha256=PKCQ6Sprxp5-KgCNXVqQnWlOtA3BABPT-sGJOH0L0_k,766
3
+ modern_di_fastapi/middleware.py,sha256=R9Yau5TWWyU2nBb84ba5MQOxg3w0bc7FR6-JjgvgUQ8,857
4
+ modern_di_fastapi-0.1.0.dist-info/METADATA,sha256=jH4QJ8RP7lxcX4Xq5JmEfR5u55PosoWbXhpYkwcZi-k,926
5
+ modern_di_fastapi-0.1.0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
6
+ modern_di_fastapi-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.25.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any