Sanic¶
Though it is not required, you can use dishka-sanic integration. It features:
automatic REQUEST scope management using middleware
passing
Requestobject as a context data to providers for HTTP requestsautomatic injection of dependencies into handler function.
How to use¶
Import
from dishka.integrations.sanic import (
FromDishka,
SanicProvider,
inject,
setup_dishka,
)
from dishka import make_async_container, Provider, provide, Scope
Create provider. You can use
sanic.Requestas a factory parameter to access on REQUEST-scope
class YourProvider(Provider):
@provide(scope=Scope.REQUEST)
def create_x(self, request: Request) -> X:
...
Mark those of your handlers parameters which are to be injected with
FromDishka[]
@app.get('/')
async def endpoint(
request: str, gateway: FromDishka[Gateway],
) -> Response:
...
3a. (optional) decorate them using @inject if you are not using auto-injection
@app.get('/')
@inject
async def endpoint(
gateway: FromDishka[Gateway],
) -> ResponseModel:
...
(optional) Use
SanicProvider()when creating container if you are going to usesanic.Requestin providers
container = make_async_container(YourProvider(), SanicProvider())
Setup
dishkaintegration.auto_inject=Trueis required unless you explicitly use@injectdecorator
setup_dishka(container=container, app=app, auto_inject=True)
Or pass your own inject decorator
setup_dishka(container=container, app=app, auto_inject=my_inject)
Websockets¶
Not supported yet