From 83c7356bfde2709340e283f541f918fee85164c7 Mon Sep 17 00:00:00 2001 From: Andrew Srg Date: Sun, 4 Feb 2024 14:54:21 +0200 Subject: [PATCH 1/7] create asgi middleware --- examples/litestar_app.py | 19 ++++--------------- src/dishka/integrations/litestar.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/examples/litestar_app.py b/examples/litestar_app.py index 53883a93..321e5935 100644 --- a/examples/litestar_app.py +++ b/examples/litestar_app.py @@ -7,13 +7,7 @@ from dishka import Provider, Scope, provide from dishka.integrations.base import Depends -from dishka.integrations.litestar import ( - inject, - setup_dishka, - startup_dishka, - make_dishka_container, - shutdown_dishka -) +from dishka.integrations.litestar import inject, DishkaApp # app core @@ -25,7 +19,7 @@ def get(self) -> str: class FakeDbGateway(DbGateway): def get(self) -> str: - return "Hello" + return "Hello123" class Interactor: @@ -62,13 +56,8 @@ def create_app(): level=logging.WARNING, format='%(asctime)s %(process)-7s %(module)-20s %(message)s', ) - app = Litestar( - route_handlers=[MainController], - on_startup=[startup_dishka], - on_shutdown=[shutdown_dishka], - before_request=make_dishka_container - ) - return setup_dishka(app, [InteractorProvider(), AdaptersProvider()]) + app = Litestar(route_handlers=[MainController]) + return DishkaApp([InteractorProvider(), AdaptersProvider()], app) if __name__ == "__main__": diff --git a/src/dishka/integrations/litestar.py b/src/dishka/integrations/litestar.py index 4afade76..f6bf7ee0 100644 --- a/src/dishka/integrations/litestar.py +++ b/src/dishka/integrations/litestar.py @@ -49,3 +49,15 @@ async def shutdown_dishka(app: Litestar): def setup_dishka(app: Litestar, providers: Sequence[Provider]) -> Litestar: app.state.dishka_container_wrapper = make_async_container(*providers) return app + + +class DishkaApp: + def __init__(self, providers: Sequence[Provider], app: Litestar): + self.app = app + self.app.state.dishka_container_wrapper = make_async_container(*providers) + self.app.on_startup.append(startup_dishka) + self.app.on_shutdown.append(shutdown_dishka) + self.app.before_request = make_dishka_container + + async def __call__(self, scope, receive, send): + await self.app(scope, receive, send) From f3cffa4c96d8989334cbdefcb68f3ba5d76623ea Mon Sep 17 00:00:00 2001 From: Andrew Srg Date: Sun, 4 Feb 2024 14:55:04 +0200 Subject: [PATCH 2/7] create litestar after response handler --- src/dishka/integrations/litestar.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/dishka/integrations/litestar.py b/src/dishka/integrations/litestar.py index f6bf7ee0..025386b7 100644 --- a/src/dishka/integrations/litestar.py +++ b/src/dishka/integrations/litestar.py @@ -37,6 +37,10 @@ async def make_dishka_container(request: Request): request.state.dishka_container = request_container +async def close_dishka_container(request: Request): + await request.app.state.dishka_container().__aexit__(None, None, None) + + async def startup_dishka(app: Litestar): container = await app.state.dishka_container_wrapper.__aenter__() app.state.dishka_container = container @@ -58,6 +62,7 @@ def __init__(self, providers: Sequence[Provider], app: Litestar): self.app.on_startup.append(startup_dishka) self.app.on_shutdown.append(shutdown_dishka) self.app.before_request = make_dishka_container + self.app.after_response = close_dishka_container async def __call__(self, scope, receive, send): await self.app(scope, receive, send) From 5882dab9ca92d55ce40a1f6f0821a6499fd260bb Mon Sep 17 00:00:00 2001 From: Andrew Srg Date: Sun, 4 Feb 2024 17:38:42 +0200 Subject: [PATCH 3/7] refactor litestar asgi middleware --- src/dishka/integrations/litestar.py | 46 +++++++++++++---------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/src/dishka/integrations/litestar.py b/src/dishka/integrations/litestar.py index 025386b7..abe93880 100644 --- a/src/dishka/integrations/litestar.py +++ b/src/dishka/integrations/litestar.py @@ -2,8 +2,11 @@ from typing import Optional, Sequence, get_type_hints from litestar import Litestar, Request +from litestar.types import ASGIApp, Receive, Scope, Send from dishka import Provider, make_async_container +from dishka.async_container import AsyncContainer, AsyncContextWrapper +from dishka.integrations.asgi import BaseDishkaApp from dishka.integrations.base import wrap_injection @@ -32,22 +35,16 @@ def inject(func): ) -async def make_dishka_container(request: Request): - request_container = await request.app.state.dishka_container().__aenter__() - request.state.dishka_container = request_container +def make_add_request_container_middleware(app: ASGIApp): + async def middleware(scope: Scope, receive: Receive, send: Send) -> None: + request = Request(scope) + async with request.app.state.dishka_container( + {Request: request}, + ) as request_container: + request.state.dishka_container = request_container + await app(scope, receive, send) - -async def close_dishka_container(request: Request): - await request.app.state.dishka_container().__aexit__(None, None, None) - - -async def startup_dishka(app: Litestar): - container = await app.state.dishka_container_wrapper.__aenter__() - app.state.dishka_container = container - - -async def shutdown_dishka(app: Litestar): - await app.state.dishka_container_wrapper.__aexit__(None, None, None) + return middleware def setup_dishka(app: Litestar, providers: Sequence[Provider]) -> Litestar: @@ -55,14 +52,13 @@ def setup_dishka(app: Litestar, providers: Sequence[Provider]) -> Litestar: return app -class DishkaApp: - def __init__(self, providers: Sequence[Provider], app: Litestar): - self.app = app - self.app.state.dishka_container_wrapper = make_async_container(*providers) - self.app.on_startup.append(startup_dishka) - self.app.on_shutdown.append(shutdown_dishka) - self.app.before_request = make_dishka_container - self.app.after_response = close_dishka_container +class DishkaApp(BaseDishkaApp): + def _init_request_middleware( + self, app, container_wrapper: AsyncContextWrapper, + ): + app.asgi_handler = make_add_request_container_middleware( + app.asgi_handler, + ) - async def __call__(self, scope, receive, send): - await self.app(scope, receive, send) + def _app_startup(self, app, container: AsyncContainer): + app.state.dishka_container = container From 08ba2164bbfeb775cc84a23a846cd7137ae962bd Mon Sep 17 00:00:00 2001 From: Andrew Srg Date: Sun, 4 Feb 2024 17:44:00 +0200 Subject: [PATCH 4/7] refactor: remove unnecessary functions --- src/dishka/integrations/litestar.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/dishka/integrations/litestar.py b/src/dishka/integrations/litestar.py index abe93880..b8f86eb7 100644 --- a/src/dishka/integrations/litestar.py +++ b/src/dishka/integrations/litestar.py @@ -47,11 +47,6 @@ async def middleware(scope: Scope, receive: Receive, send: Send) -> None: return middleware -def setup_dishka(app: Litestar, providers: Sequence[Provider]) -> Litestar: - app.state.dishka_container_wrapper = make_async_container(*providers) - return app - - class DishkaApp(BaseDishkaApp): def _init_request_middleware( self, app, container_wrapper: AsyncContextWrapper, From c2c9dd28845c886156d6f53f85fcf874e2df741f Mon Sep 17 00:00:00 2001 From: Andrew Srg Date: Sun, 4 Feb 2024 17:45:01 +0200 Subject: [PATCH 5/7] fix code formatting --- src/dishka/integrations/litestar.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/dishka/integrations/litestar.py b/src/dishka/integrations/litestar.py index b8f86eb7..68e6b779 100644 --- a/src/dishka/integrations/litestar.py +++ b/src/dishka/integrations/litestar.py @@ -1,10 +1,9 @@ from inspect import Parameter -from typing import Optional, Sequence, get_type_hints +from typing import Optional, get_type_hints -from litestar import Litestar, Request +from litestar import Request from litestar.types import ASGIApp, Receive, Scope, Send -from dishka import Provider, make_async_container from dishka.async_container import AsyncContainer, AsyncContextWrapper from dishka.integrations.asgi import BaseDishkaApp from dishka.integrations.base import wrap_injection From 5e6cea36c14618ea2920ecec6de0e3c9f0564516 Mon Sep 17 00:00:00 2001 From: Andrew Srg Date: Mon, 5 Feb 2024 14:22:13 +0200 Subject: [PATCH 6/7] add event type checking --- src/dishka/integrations/litestar.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/dishka/integrations/litestar.py b/src/dishka/integrations/litestar.py index 68e6b779..32e640fe 100644 --- a/src/dishka/integrations/litestar.py +++ b/src/dishka/integrations/litestar.py @@ -2,6 +2,7 @@ from typing import Optional, get_type_hints from litestar import Request +from litestar.enums import ScopeType from litestar.types import ASGIApp, Receive, Scope, Send from dishka.async_container import AsyncContainer, AsyncContextWrapper @@ -36,6 +37,9 @@ def inject(func): def make_add_request_container_middleware(app: ASGIApp): async def middleware(scope: Scope, receive: Receive, send: Send) -> None: + if scope.get("type") != ScopeType.HTTP: + await app(scope, receive, send) + request = Request(scope) async with request.app.state.dishka_container( {Request: request}, From 1317a8d2aedb2cf592fc53a2f45f11543aea8565 Mon Sep 17 00:00:00 2001 From: Andrew Srg Date: Tue, 6 Feb 2024 20:32:36 +0200 Subject: [PATCH 7/7] refactor: add return to litestar middleware --- src/dishka/integrations/litestar.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/dishka/integrations/litestar.py b/src/dishka/integrations/litestar.py index 32e640fe..aa9d0b69 100644 --- a/src/dishka/integrations/litestar.py +++ b/src/dishka/integrations/litestar.py @@ -39,6 +39,7 @@ def make_add_request_container_middleware(app: ASGIApp): async def middleware(scope: Scope, receive: Receive, send: Send) -> None: if scope.get("type") != ScopeType.HTTP: await app(scope, receive, send) + return request = Request(scope) async with request.app.state.dishka_container(