modern-di-fastapi 1.0.0a2__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,61 @@
|
|
|
1
|
+
import contextlib
|
|
2
|
+
import dataclasses
|
|
3
|
+
import typing
|
|
4
|
+
|
|
5
|
+
import fastapi
|
|
6
|
+
from fastapi.routing import _merge_lifespan_context
|
|
7
|
+
from modern_di import AsyncContainer, Scope, providers
|
|
8
|
+
from starlette.requests import HTTPConnection
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
T_co = typing.TypeVar("T_co", covariant=True)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def fetch_di_container(app_: fastapi.FastAPI) -> AsyncContainer:
|
|
15
|
+
return typing.cast(AsyncContainer, app_.state.di_container)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@contextlib.asynccontextmanager
|
|
19
|
+
async def _lifespan_manager(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
|
|
20
|
+
async with fetch_di_container(app_):
|
|
21
|
+
yield
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def setup_di(app: fastapi.FastAPI, container: AsyncContainer) -> AsyncContainer:
|
|
25
|
+
app.state.di_container = container
|
|
26
|
+
old_lifespan_manager = app.router.lifespan_context
|
|
27
|
+
app.router.lifespan_context = _merge_lifespan_context(
|
|
28
|
+
old_lifespan_manager,
|
|
29
|
+
_lifespan_manager,
|
|
30
|
+
)
|
|
31
|
+
return container
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[AsyncContainer]:
|
|
35
|
+
context: dict[type[typing.Any], typing.Any] = {}
|
|
36
|
+
scope: Scope | None = None
|
|
37
|
+
if isinstance(connection, fastapi.Request):
|
|
38
|
+
scope = Scope.REQUEST
|
|
39
|
+
context[fastapi.Request] = connection
|
|
40
|
+
elif isinstance(connection, fastapi.WebSocket):
|
|
41
|
+
context[fastapi.WebSocket] = connection
|
|
42
|
+
scope = Scope.SESSION
|
|
43
|
+
container: AsyncContainer = fetch_di_container(connection.app)
|
|
44
|
+
async with container.build_child_container(context=context, scope=scope) as request_container:
|
|
45
|
+
yield request_container
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclasses.dataclass(slots=True, frozen=True)
|
|
49
|
+
class Dependency(typing.Generic[T_co]):
|
|
50
|
+
dependency: providers.AbstractProvider[T_co] | type[T_co]
|
|
51
|
+
|
|
52
|
+
async def __call__(
|
|
53
|
+
self, request_container: typing.Annotated[AsyncContainer, fastapi.Depends(build_di_container)]
|
|
54
|
+
) -> T_co:
|
|
55
|
+
if isinstance(self.dependency, providers.AbstractProvider):
|
|
56
|
+
return await request_container.resolve_provider(self.dependency)
|
|
57
|
+
return await request_container.resolve(dependency_type=self.dependency)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def FromDI(dependency: providers.AbstractProvider[T_co] | type[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
|
|
61
|
+
return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
|
|
File without changes
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: modern-di-fastapi
|
|
3
|
+
Version: 1.0.0a2
|
|
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>=1.0.0alpha
|
|
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=t4ox50Pc4k7bDNprfg3_ujqj4dwidTTuzV9WRNH-EOs,190
|
|
2
|
+
modern_di_fastapi/main.py,sha256=21y8wmPHmaQpZE-IxvJVul0lHuM9k7PaEL9DmqWi9rw,2270
|
|
3
|
+
modern_di_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
modern_di_fastapi-1.0.0a2.dist-info/METADATA,sha256=b9Drh_vqXNW8ApsdO2z4uEDRey_EnvQaISEBmZkUL9k,940
|
|
5
|
+
modern_di_fastapi-1.0.0a2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
6
|
+
modern_di_fastapi-1.0.0a2.dist-info/RECORD,,
|