1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
from inspect import Parameter, Signature
from typing import Any, Dict, Tuple
from litestar import Litestar, get
from litestar.di import Provide
from litestar.plugins import DIPlugin
class MyBaseType:
def __init__(self, param):
self.param = param
class MyDIPlugin(DIPlugin):
def has_typed_init(self, type_: Any) -> bool:
return issubclass(type_, MyBaseType)
def get_typed_init(self, type_: Any) -> Tuple[Signature, Dict[str, Any]]:
signature = Signature([Parameter(name="param", kind=Parameter.POSITIONAL_OR_KEYWORD)])
annotations = {"param": str}
return signature, annotations
@get("/", dependencies={"injected": Provide(MyBaseType, sync_to_thread=False)})
async def handler(injected: MyBaseType) -> str:
return injected.param
app = Litestar(route_handlers=[handler], plugins=[MyDIPlugin()])
# run: /?param=hello
|