Skip to content

arturboyun/dmr-dishka

Repository files navigation

Django Modern REST integration for Dishka

test GitHub Actions Workflow Status License Version Python Version

This package provides integration of Dishka dependency injection framework and Modern REST framework for Django with types and async support!

Features

  • Automatic REQUEST and SESSION scope management using middleware
  • Easy setup and integration with Django Modern REST framework
  • Support for both async and sync views
  • Support for Django views

Installation

pip install dmr-dishka
uv add dmr-dishka

How to use async

  1. Import
from dishka import Provider, provide, Scope

class YourProvider(Provider):
    @provide(scope=Scope.REQUEST)
    def create_x(self, request: Request) -> X:
         ...
  1. Create provider.
class YourProvider(Provider):
    @provide(scope=Scope.REQUEST)
    def create_x(self) -> X:
         ...
  1. Mark those of your handlers parameters which are to be injected with FromDishka[] and decorate them using @inject
from dmr_dishka.integration import inject

class ExampleBlueprint(Controller[MsgspecSerializer]):
    @inject
    async def get(self, x: FromDishka[X])
        ...
  1. Make container
container = make_async_container(YourProvider())
  1. Setup dishka integration in your asgi.py
# asgi.py
from dishka import make_async_container
from django_example.app.ioc import AppProvider
from dmr_dishka.integration import setup_dishka

container = make_async_container(AppProvider())
setup_dishka(container)

How to use sync

  1. Steps 1 and 2 is identical to async
  2. In step 3 you need to decorate your handlers with @inject_sync and their parameters should be marked with FromDishka[] as well
from dmr_dishka.integration import inject_sync

class ExampleBlueprint(Controller[MsgspecSerializer]):
    @inject_sync
    def get(self, x: FromDishka[X])
        ...
  1. Step 4 is identical to async
  2. In step 5 need to setup dishka in your wsgi.py instead of asgi.py
# wsgi.py
from dishka import make_container
from django_example.app.ioc import AppProvider
from dmr_dishka.integration import setup_dishka

container = make_container(AppProvider())
setup_dishka(container)

| Check src/django_example for more examples of usage and vanila django view support.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages