Skip to main content

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 Request object as a context data to providers for both Websockets and HTTP requests
  • automatic injection of dependencies into handler function.

How to use

  1. Import
from starlette_dishka import (
    FromDishka,
    StarletteProvider,
    inject,
    setup_dishka,
)
from dishka import make_async_container, Provider, provide, Scope
  1. Create provider. You can use starlette.requests.Request as a factory parameter to access on REQUEST-scope, and starlette.websockets.WebSocket on SESSION-scope
class YourProvider(Provider):
    @provide(scope=Scope.REQUEST)
    def create_x(self, request: Request) -> X:
         ...
  1. 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:
    ...
  1. (optional) Use StarletteProvider() when creating container if you are going to use starlette.Request or starlette.WebSocket in providers
container = make_async_container(YourProvider(), StarletteProvider())
  1. Setup dishka integration.
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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette_dishka-1.0.1.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

starlette_dishka-1.0.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

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

Hashes for starlette_dishka-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a079c119395f01bade38a04dc426ac2a07b7aa109bda5e033e5084db5ed756b7
MD5 315ef660f0de4249bb6ca19635da71ca
BLAKE2b-256 0ad40e6588e50c084458507ecf7973096f8f88e8b1c7770ad919aa40005c87f5

See more details on using hashes here.

File details

Details for the file starlette_dishka-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for starlette_dishka-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cbbb69dd6b90a11ac9589120c58e0e48cf9721df43749de641b36a922d09a6bd
MD5 4f11a7d6fc149da000d17ca006ff828f
BLAKE2b-256 f04225d7a5d9225f4b6c50d537ca996135411436881479a89b4a6b46f756c8f8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page