Skip to content

Commit

Permalink
sanic doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Tishka17 committed Oct 20, 2024
1 parent 33eb3ec commit bdfa136
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 21 deletions.
21 changes: 2 additions & 19 deletions docs/integrations/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Built-in frameworks integrations includes:
fastapi
flask
litestar
sanic
adding_new

Web frameworks
Expand All @@ -23,7 +24,7 @@ Web frameworks
* :ref:`Fastapi`
* :ref:`Flask`
* :ref:`Litestar`
* Sanic
* :ref:`Sanic`
* Starlette

Telegram bots
Expand Down Expand Up @@ -153,24 +154,6 @@ With some frameworks we provide an option to inject dependencies in handlers wit
await message.ack()
return message.body
* For **Sanic** you need to provide ``auto_inject=True`` when calling ``setup_dishka``. It is important here to call it after registering all views and blueprints. E.g:

.. code-block:: python
from sanic import Sanic, Request, HTTPResponse
from dishka.integrations.sanic import FromDishka, setup_dishka
app = Sanic(__name__)
@app.get("/")
async def index(
request: Request,
interactor: FromDishka[Interactor],
) -> HTTPResponse:
return HTTPResponse(interactor())
setup_dishka(container=container, app=app, auto_inject=True)
* For **Click** you need to provide ``auto_inject=True`` when calling ``setup_dishka``. E.g:

.. code-block:: python
Expand Down
77 changes: 77 additions & 0 deletions docs/integrations/sanic.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
.. _sanic:

Sanic
===========================================

Though it is not required, you can use dishka-sanic integration. It features:

* automatic REQUEST and SESSION scope management using middleware
* passing ``Request`` object as a context data to providers for **HTTP** requests
* automatic injection of dependencies into handler function


How to use
****************

1. Import

.. code-block:: python
from dishka.integrations.sanic import (
FromDishka,
SanicProvider,
inject,
setup_dishka,
)
from dishka import make_async_container, Provider, provide, Scope
2. Create provider. You can use ``sanic.Request`` as a factory parameter to access on REQUEST-scope

.. code-block:: python
class YourProvider(Provider):
@provide(scope=Scope.REQUEST)
def create_x(self, request: Request) -> X:
...
3. Mark those of your handlers parameters which are to be injected with ``FromDishka[]``

.. code-block:: python
@app.get('/')
async def endpoint(
request: str, gateway: FromDishka[Gateway],
) -> Response:
...
3a. *(optional)* decorate them using ``@inject`` if you are not using auto-injection

.. code-block:: python
@app.get('/')
@inject
async def endpoint(
gateway: FromDishka[Gateway],
) -> ResponseModel:
...
4. *(optional)* Use ``SanicProvider()`` when creating container if you are going to use ``sanic.Request`` in providers.

.. code-block:: python
container = make_async_container(YourProvider(), SanicProvider())
6. Setup dishka integration. ``autoinject=True`` is required unless you explicitly use ``@inject`` decorator

.. code-block:: python
setup_dishka(container=container, app=app, auto_inject=True)
Websockets
**********************

Not supported yet
8 changes: 6 additions & 2 deletions examples/integrations/sanic_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from sanic import Blueprint, HTTPResponse, Request, Sanic

from dishka import Provider, Scope, make_async_container, provide
from dishka.integrations.sanic import FromDishka, inject, setup_dishka
from dishka.integrations.sanic import (
FromDishka, inject, setup_dishka, SanicProvider,
)


class DbGateway(Protocol):
Expand Down Expand Up @@ -58,7 +60,9 @@ async def auto(
app = Sanic(__name__)

app.blueprint(bp)
container = make_async_container(AdaptersProvider(), InteractorsProvider())
container = make_async_container(
AdaptersProvider(), InteractorsProvider(), SanicProvider(),
)

setup_dishka(container, app, auto_inject=True)

Expand Down

0 comments on commit bdfa136

Please sign in to comment.