Litestar

By now you should have a container with providers. Now all you have to do is define request handlers according to the following rules:

  1. use @inject decorator before http-method Litestar decorator;

  2. added for each injected parameter to the request handler a typing of the form Union[<your type>, Any] for Python versions below 3.10 or <your type> | Any, as well as the Provide marker from the injection package indicating the provider

Example

from typing import Any, Union

from litestar import Litestar, get
from injection import Provide, inject, DeclarativeContainer, providers


class Redis:
    def __init__(self, *, url: str, port: int):
        self.uri = url + ":" + str(port)
        self.url = url
        self.port = port

    def get(self, key):
        return key


class Container(DeclarativeContainer):
    redis = providers.Singleton(
        Redis,
        port=9873,
        url="redis://...",
    )
    num = providers.Object(9402)


@get(
    "/some_resource",
    status_code=200,
)
@inject
async def litestar_endpoint(
    redis: Union[Redis, Any] = Provide(Container.redis),
    num: Union[int, Any] = Provide(Container.num),
) -> dict:
    value = redis.get(800)
    return {"detail": value, "num2": num}


@get(
    "/num_endpoint",
    status_code=200,
)
@inject
async def litestar_endpoint_object_provider(
    num: Union[int, Any] = Provide(Container.num),
) -> dict:
    return {"detail": num}


_handlers = [
    litestar_endpoint,
    litestar_endpoint_object_provider,
]

app = Litestar(route_handlers=_handlers)