modern-di-fastapi 0.1.4__py3-none-any.whl → 0.2.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/__init__.py +4 -4
- modern_di_fastapi/main.py +38 -0
- {modern_di_fastapi-0.1.4.dist-info → modern_di_fastapi-0.2.0.dist-info}/METADATA +1 -1
- modern_di_fastapi-0.2.0.dist-info/RECORD +6 -0
- modern_di_fastapi/depends.py +0 -24
- modern_di_fastapi/middleware.py +0 -27
- modern_di_fastapi-0.1.4.dist-info/RECORD +0 -7
- {modern_di_fastapi-0.1.4.dist-info → modern_di_fastapi-0.2.0.dist-info}/WHEEL +0 -0
modern_di_fastapi/__init__.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
from modern_di_fastapi.
|
|
2
|
-
from modern_di_fastapi.middleware import ContainerMiddleware
|
|
1
|
+
from modern_di_fastapi.main import FromDI, enter_di_request_scope, fetch_di_container, save_di_container
|
|
3
2
|
|
|
4
3
|
|
|
5
4
|
__all__ = [
|
|
6
|
-
"ContainerMiddleware",
|
|
7
5
|
"FromDI",
|
|
8
|
-
"
|
|
6
|
+
"enter_di_request_scope",
|
|
7
|
+
"fetch_di_container",
|
|
8
|
+
"save_di_container",
|
|
9
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
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
modern_di_fastapi/__init__.py,sha256=8Djsc0SZVfYQkXlkYpaZ6tcYVQvsK4kJXWwr1nEg4js,216
|
|
2
|
+
modern_di_fastapi/main.py,sha256=gX4amXitXg-zTqIQXComCgkcF5XkWDRkKqhxebM6uEI,1301
|
|
3
|
+
modern_di_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
modern_di_fastapi-0.2.0.dist-info/METADATA,sha256=0rwzKbi7rv7CEGDS9uHJgrgs2vgkC_EYH6mjf-fGlTs,926
|
|
5
|
+
modern_di_fastapi-0.2.0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
|
6
|
+
modern_di_fastapi-0.2.0.dist-info/RECORD,,
|
modern_di_fastapi/depends.py
DELETED
|
@@ -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
|
modern_di_fastapi/middleware.py
DELETED
|
@@ -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)
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
modern_di_fastapi/__init__.py,sha256=L-WPazuGrU4OB-ME-1BxMtfK1n2H8cg5yDZPUSEYcgA,203
|
|
2
|
-
modern_di_fastapi/depends.py,sha256=zu7i4iL2nySrVLzyTa_AV7fhV720TbvARd_iPsu-i6o,762
|
|
3
|
-
modern_di_fastapi/middleware.py,sha256=R9Yau5TWWyU2nBb84ba5MQOxg3w0bc7FR6-JjgvgUQ8,857
|
|
4
|
-
modern_di_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
-
modern_di_fastapi-0.1.4.dist-info/METADATA,sha256=W98ITZ8vdsZzfS3ppz2cvgQmSca8S6qefsOSWyG3yLM,926
|
|
6
|
-
modern_di_fastapi-0.1.4.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
|
7
|
-
modern_di_fastapi-0.1.4.dist-info/RECORD,,
|
|
File without changes
|