from uuid import UUID
from starlette.middleware.base import BaseHTTPMiddleware
from shared.tenant import HEADER_TENANT_ID, STATE_TENANT_ID
class TenantHeaderMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request, call_next):
        raw=request.headers.get(HEADER_TENANT_ID)
        try: value=UUID(raw) if raw else None
        except ValueError: value=None
        setattr(request.state,STATE_TENANT_ID,value)
        return await call_next(request)
