modern-di-fastapi 2.0.0a0__py3-none-any.whl → 2.0.0a1__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/main.py CHANGED
@@ -1,7 +1,9 @@
1
+ import contextlib
1
2
  import dataclasses
2
3
  import typing
3
4
 
4
5
  import fastapi
6
+ from fastapi.routing import _merge_lifespan_context
5
7
  from modern_di import Container, Scope, providers
6
8
  from starlette.requests import HTTPConnection
7
9
 
@@ -9,16 +11,35 @@ from starlette.requests import HTTPConnection
9
11
  T_co = typing.TypeVar("T_co", covariant=True)
10
12
 
11
13
 
14
+ fastapi_request = providers.ContextProvider(scope=Scope.REQUEST, context_type=fastapi.Request)
15
+ fastapi_websocket = providers.ContextProvider(scope=Scope.REQUEST, context_type=fastapi.WebSocket)
16
+
17
+
12
18
  def fetch_di_container(app_: fastapi.FastAPI) -> Container:
13
19
  return typing.cast(Container, app_.state.di_container)
14
20
 
15
21
 
22
+ @contextlib.asynccontextmanager
23
+ async def _lifespan_manager(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
24
+ container = fetch_di_container(app_)
25
+ try:
26
+ yield
27
+ finally:
28
+ await container.close_async()
29
+
30
+
16
31
  def setup_di(app: fastapi.FastAPI, container: Container) -> Container:
17
32
  app.state.di_container = container
33
+ container.providers_registry.add_providers(fastapi_request=fastapi_request, fastapi_websocket=fastapi_websocket)
34
+ old_lifespan_manager = app.router.lifespan_context
35
+ app.router.lifespan_context = _merge_lifespan_context(
36
+ old_lifespan_manager,
37
+ _lifespan_manager,
38
+ )
18
39
  return container
19
40
 
20
41
 
21
- async def build_di_container(connection: HTTPConnection) -> Container:
42
+ async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[Container]:
22
43
  context: dict[type[typing.Any], typing.Any] = {}
23
44
  scope: Scope | None = None
24
45
  if isinstance(connection, fastapi.Request):
@@ -27,7 +48,11 @@ async def build_di_container(connection: HTTPConnection) -> Container:
27
48
  elif isinstance(connection, fastapi.WebSocket):
28
49
  context[fastapi.WebSocket] = connection
29
50
  scope = Scope.SESSION
30
- return fetch_di_container(connection.app).build_child_container(context=context, scope=scope)
51
+ container = fetch_di_container(connection.app).build_child_container(context=context, scope=scope)
52
+ try:
53
+ yield container
54
+ finally:
55
+ await container.close_async()
31
56
 
32
57
 
33
58
  @dataclasses.dataclass(slots=True, frozen=True)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: modern-di-fastapi
3
- Version: 2.0.0a0
3
+ Version: 2.0.0a1
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
@@ -16,7 +16,7 @@ Classifier: Topic :: Software Development :: Libraries
16
16
  Classifier: Typing :: Typed
17
17
  Requires-Python: <4,>=3.10
18
18
  Requires-Dist: fastapi>=0.100
19
- Requires-Dist: modern-di>=1.0.0alpha
19
+ Requires-Dist: modern-di>=2.0.0alpha1
20
20
  Description-Content-Type: text/markdown
21
21
 
22
22
  "Modern-DI-FastAPI"
@@ -0,0 +1,6 @@
1
+ modern_di_fastapi/__init__.py,sha256=t4ox50Pc4k7bDNprfg3_ujqj4dwidTTuzV9WRNH-EOs,190
2
+ modern_di_fastapi/main.py,sha256=lgW7RSbE6eV9lvHhyBh2a-IKN_cL6DJyQlN9cS4uLzA,2585
3
+ modern_di_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ modern_di_fastapi-2.0.0a1.dist-info/METADATA,sha256=YlW6gotlVI9TC-4zYIP8CQoQkQnqkXQ-AKqz5jWosKY,992
5
+ modern_di_fastapi-2.0.0a1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
6
+ modern_di_fastapi-2.0.0a1.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- modern_di_fastapi/__init__.py,sha256=t4ox50Pc4k7bDNprfg3_ujqj4dwidTTuzV9WRNH-EOs,190
2
- modern_di_fastapi/main.py,sha256=Kwq7h23JafSrAQsCjEAGBimw_V9upoT_t_UuP82By5c,1683
3
- modern_di_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- modern_di_fastapi-2.0.0a0.dist-info/METADATA,sha256=6X-CtNc2nL8isvPBQW4a5oJOCw2r15uroPl8E3A0KgY,991
5
- modern_di_fastapi-2.0.0a0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
6
- modern_di_fastapi-2.0.0a0.dist-info/RECORD,,