"""FastAPI dependency injection providers.""" from typing import AsyncGenerator from fastapi import Request from sqlalchemy.ext.asyncio import AsyncSession from src.infrastructure.db import AsyncSessionLocal from src.infrastructure.redis import RedisCache, get_redis async def get_db_session() -> AsyncGenerator[AsyncSession, None]: """Yield a database session for FastAPI dependency injection.""" session = AsyncSessionLocal() try: yield session await session.commit() except Exception: await session.rollback() raise finally: await session.close() async def get_cache() -> AsyncGenerator[RedisCache, None]: """Yield a Redis cache instance.""" redis = await get_redis() yield RedisCache(redis) def get_client_ip(request: Request) -> str: """Extract client IP from request, handling proxies.""" forwarded = request.headers.get("x-forwarded-for") if forwarded: return forwarded.split(",")[0].strip() return request.client.host if request.client else "unknown"