"""API dependencies.""" from typing import AsyncGenerator from uuid import UUID from fastapi import Depends, Header from sqlalchemy.ext.asyncio import AsyncSession from src.core.database import AsyncSessionLocal from src.repositories.scenario import scenario_repository, ScenarioStatus from src.core.exceptions import NotFoundException, ScenarioNotRunningException async def get_db() -> AsyncGenerator[AsyncSession, None]: """Dependency that provides a database session.""" async with AsyncSessionLocal() as session: try: yield session finally: await session.close() async def get_running_scenario( scenario_id: str = Header(..., alias="X-Scenario-ID"), db: AsyncSession = Depends(get_db), ): """Dependency that validates scenario exists and is running.""" try: scenario_uuid = UUID(scenario_id) except ValueError: raise NotFoundException("Scenario") scenario = await scenario_repository.get(db, scenario_uuid) if not scenario: raise NotFoundException("Scenario") if scenario.status != ScenarioStatus.RUNNING.value: raise ScenarioNotRunningException() return scenario