mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-25 21:36:52 +08:00
Co-authored-by: openhands <openhands@all-hands.dev> Co-authored-by: sp.wack <83104063+amanape@users.noreply.github.com> Co-authored-by: Engel Nyst <enyst@users.noreply.github.com>
35 lines
1.2 KiB
Python
35 lines
1.2 KiB
Python
import contextlib
|
|
from abc import ABC, abstractmethod
|
|
from typing import AsyncGenerator, Generic, TypeAlias, TypeVar
|
|
|
|
from fastapi import Request
|
|
from starlette.datastructures import State
|
|
|
|
T = TypeVar('T')
|
|
InjectorState: TypeAlias = State
|
|
|
|
|
|
class Injector(Generic[T], ABC):
|
|
"""Object designed to facilitate dependency injection"""
|
|
|
|
@abstractmethod
|
|
async def inject(
|
|
self, state: InjectorState, request: Request | None = None
|
|
) -> AsyncGenerator[T, None]:
|
|
"""Inject an object. The state object may be used to store variables for
|
|
reuse by other injectors, as injection operations may be nested."""
|
|
yield None # type: ignore
|
|
|
|
@contextlib.asynccontextmanager
|
|
async def context(
|
|
self, state: InjectorState, request: Request | None = None
|
|
) -> AsyncGenerator[T, None]:
|
|
"""Context function suitable for use in async with clauses"""
|
|
async for result in self.inject(state, request):
|
|
yield result
|
|
|
|
async def depends(self, request: Request) -> AsyncGenerator[T, None]:
|
|
"""Depends function suitable for use with FastAPI dependency injection."""
|
|
async for result in self.inject(request.state, request):
|
|
yield result
|