Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add DishkaRouter for integrations/litestar.py #331

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ __pycache__/
.mypy_cache/
.idea/
.vscode/
.venv/
.venv/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to PR
anyway, we do not use lock here as we develop library

uv.lock
14 changes: 14 additions & 0 deletions docs/integrations/litestar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ How to use
LitestarProvider,
inject,
setup_dishka,
DishkaRouter,
)
from dishka import make_async_container, Provider, provide, Scope

Expand All @@ -46,6 +47,19 @@ How to use
) -> Response:
...

3a. *(optional)* Set route class to each of your fastapi routers to enable automatic injection (it works only for HTTP, not for websockets)

.. code-block:: python

@router.get('/')
async def endpoint(
request: str, gateway: FromDishka[Gateway],
) -> Response:
...

r = DishkaRouter('', route_handlers=[endpoint])
app = Litestar(route_handlers=[r])


4. *(optional)* Use ``LitestarProvider()`` when creating container if you are going to use ``litestar.Request`` in providers.

Expand Down
8 changes: 7 additions & 1 deletion src/dishka/integrations/litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from inspect import Parameter
from typing import ParamSpec, TypeVar, get_type_hints

from litestar import Litestar, Request, WebSocket
from litestar import Litestar, Request, WebSocket, Router
from litestar.enums import ScopeType
from litestar.types import ASGIApp, Receive, Scope, Send

Expand Down Expand Up @@ -93,3 +93,9 @@ def setup_dishka(container: AsyncContainer, app: Litestar) -> None:
app.asgi_handler,
)
app.state.dishka_container = container


class DishkaRouter(Router):
def register(self, value):
value._fn = inject(value._fn)
return super().register(value)
8 changes: 6 additions & 2 deletions tests/integrations/litestar/test_litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
FromDishka,
inject,
setup_dishka,
DishkaRouter
)
from ..common import (
APP_DEP_VALUE,
Expand All @@ -29,8 +30,11 @@ async def dishka_app(
provider,
request_class: type[Request] = Request,
) -> TestClient:
app = litestar.Litestar(request_class=request_class)
app.register(get("/")(inject(view)))
dishka_router = DishkaRouter('', route_handlers=[])
dishka_router.register(get("/")(view))

app = litestar.Litestar(route_handlers=[dishka_router], request_class=request_class)
# app.register(get("/")(inject(view)))
app.register(websocket_listener("/ws")(websocket_handler))
container = make_async_container(provider)
setup_dishka(container, app)
Expand Down
Loading