Skip to main content

Starlette integration for dishka

Project description

Starlette integration for Dishka

PyPI version Supported versions Downloads License GitHub Actions Workflow Status Telegram

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.2.tar.gz (16.7 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.2-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file starlette_dishka-1.0.2.tar.gz.

File metadata

  • Download URL: starlette_dishka-1.0.2.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for starlette_dishka-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f01054d9ca675e7778283b66bffaa5284a7e6e8cb3d895d103aba3f117dd715a
MD5 57e4d3afe14294d3482ea46c4ef85798
BLAKE2b-256 91324da097f2443ceafae09d418fcc62e7e9f0e637b8c37c90ac36f68b17e2a6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: starlette_dishka-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for starlette_dishka-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e554ebc99c7a494c1084196871ccdb3f313f8430347d589576d688b5645fbbfe
MD5 a1c2d4c5bee5d609f0f10bf089bf5c40
BLAKE2b-256 c945aca50b953a81968a7e68aa1b764d0c86fe061ccf8b7779b108aca3d72f35

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