modern-di-fastapi 0.4.2__tar.gz → 0.6.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.4.2 → modern_di_fastapi-0.6.0}/PKG-INFO +2 -2
- {modern_di_fastapi-0.4.2 → modern_di_fastapi-0.6.0}/modern_di_fastapi/main.py +20 -5
- {modern_di_fastapi-0.4.2 → modern_di_fastapi-0.6.0}/pyproject.toml +0 -1
- {modern_di_fastapi-0.4.2 → modern_di_fastapi-0.6.0}/.gitignore +0 -0
- {modern_di_fastapi-0.4.2 → modern_di_fastapi-0.6.0}/README.md +0 -0
- {modern_di_fastapi-0.4.2 → modern_di_fastapi-0.6.0}/modern_di_fastapi/__init__.py +0 -0
- {modern_di_fastapi-0.4.2 → modern_di_fastapi-0.6.0}/modern_di_fastapi/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: modern-di-fastapi
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.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
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import contextlib
|
|
1
2
|
import dataclasses
|
|
2
3
|
import enum
|
|
3
4
|
import typing
|
|
4
5
|
|
|
5
6
|
import fastapi
|
|
7
|
+
from fastapi.routing import _merge_lifespan_context
|
|
6
8
|
from modern_di import Container, Scope, providers
|
|
7
9
|
from starlette.requests import HTTPConnection
|
|
8
10
|
|
|
@@ -10,13 +12,26 @@ from starlette.requests import HTTPConnection
|
|
|
10
12
|
T_co = typing.TypeVar("T_co", covariant=True)
|
|
11
13
|
|
|
12
14
|
|
|
13
|
-
def
|
|
14
|
-
|
|
15
|
-
return app.state.di_container
|
|
15
|
+
def fetch_di_container(app_: fastapi.FastAPI) -> Container:
|
|
16
|
+
return typing.cast(Container, app_.state.di_container)
|
|
16
17
|
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
@contextlib.asynccontextmanager
|
|
20
|
+
async def _lifespan_manager(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
|
|
21
|
+
async with fetch_di_container(app_):
|
|
22
|
+
yield
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def setup_di(app: fastapi.FastAPI, scope: enum.IntEnum = Scope.APP, container: Container | None = None) -> Container:
|
|
26
|
+
if not container:
|
|
27
|
+
container = Container(scope=scope)
|
|
28
|
+
app.state.di_container = container
|
|
29
|
+
old_lifespan_manager = app.router.lifespan_context
|
|
30
|
+
app.router.lifespan_context = _merge_lifespan_context(
|
|
31
|
+
old_lifespan_manager,
|
|
32
|
+
_lifespan_manager,
|
|
33
|
+
)
|
|
34
|
+
return container
|
|
20
35
|
|
|
21
36
|
|
|
22
37
|
async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[Container]:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|