Files
bakery-ia/services/procurement/app/core/dependencies.py

45 lines
1.1 KiB
Python
Raw Normal View History

2025-10-30 21:08:07 +01:00
"""
FastAPI Dependencies for Procurement Service
"""
from fastapi import Header, HTTPException, status
from uuid import UUID
from typing import Optional
from sqlalchemy.ext.asyncio import AsyncSession
from .database import get_db
async def get_current_tenant_id(
x_tenant_id: Optional[str] = Header(None, alias="X-Tenant-ID")
) -> UUID:
"""
Extract and validate tenant ID from request header.
Args:
x_tenant_id: Tenant ID from X-Tenant-ID header
Returns:
UUID: Validated tenant ID
Raises:
HTTPException: If tenant ID is missing or invalid
"""
if not x_tenant_id:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="X-Tenant-ID header is required"
)
try:
return UUID(x_tenant_id)
except (ValueError, AttributeError):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invalid tenant ID format: {x_tenant_id}"
)
# Re-export get_db for convenience
__all__ = ["get_db", "get_current_tenant_id"]