Starlette integration for dishka
Project description
Starlette integration for Dishka
This package provides integration of Dishka dependency injection framework and Starlette, a lightweight ASGI framework.
Installation
pip install starlette-dishka
Features
- automatic REQUEST and SESSION scope management using middleware
- passing
Requestobject as a context data to providers for both Websockets and HTTP requests - automatic injection of dependencies into handler function.
How to use
- Import
from starlette_dishka import (
FromDishka,
StarletteProvider,
inject,
setup_dishka,
)
from dishka import make_async_container, Provider, provide, Scope
- Create provider. You can use
starlette.requests.Requestas a factory parameter to access on REQUEST-scope, andstarlette.websockets.WebSocketon SESSION-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[]and decorate them using@inject
@inject
async def endpoint(
request: Request,
*,
gateway: FromDishka[Gateway],
) -> ResponseModel:
...
- (optional) Use
StarletteProvider()when creating container if you are going to usestarlette.Requestorstarlette.WebSocketin providers
container = make_async_container(YourProvider(), StarletteProvider())
- Setup
dishkaintegration.
setup_dishka(container=container, app=app)
Websockets
In starlette your view function is called once per connection and then you retrieve messages in loop.
So, inject decorator can be only used to retrieve SESSION-scoped objects.
To achieve REQUEST-scope you can enter in manually:
@inject
async def get_with_request(
websocket: WebSocket,
a: FromDishka[A], # object with Scope.SESSION
container: FromDishka[AsyncContainer], # container for Scope.SESSION
) -> None:
await websocket.accept()
while True:
data = await websocket.receive_text()
# enter the nested scope, which is Scope.REQUEST
async with container() as request_container:
b = await request_container.get(B) # object with Scope.REQUEST
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file starlette_dishka-1.0.1.tar.gz.
File metadata
- Download URL: starlette_dishka-1.0.1.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a079c119395f01bade38a04dc426ac2a07b7aa109bda5e033e5084db5ed756b7
|
|
| MD5 |
315ef660f0de4249bb6ca19635da71ca
|
|
| BLAKE2b-256 |
0ad40e6588e50c084458507ecf7973096f8f88e8b1c7770ad919aa40005c87f5
|
File details
Details for the file starlette_dishka-1.0.1-py3-none-any.whl.
File metadata
- Download URL: starlette_dishka-1.0.1-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbbb69dd6b90a11ac9589120c58e0e48cf9721df43749de641b36a922d09a6bd
|
|
| MD5 |
4f11a7d6fc149da000d17ca006ff828f
|
|
| BLAKE2b-256 |
f04225d7a5d9225f4b6c50d537ca996135411436881479a89b4a6b46f756c8f8
|