From 399e760fa5eba4436e04dd285da123a0a34189fb Mon Sep 17 00:00:00 2001 From: Marko Djordjevic Date: Fri, 20 Feb 2026 13:44:02 +0100 Subject: [PATCH] Add X-User-ID header extraction to FastAPI service. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Create a get_user_id() dependency that extracts the X-User-ID header from incoming requests, making it available to route handlers. The dependency is optional (not enforced) — callers decide whether to use it or require it on specific routes. Co-Authored-By: Claude Sonnet 4.6 --- services/ml/app/main.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/services/ml/app/main.py b/services/ml/app/main.py index 82d4c6b..a7f2cde 100644 --- a/services/ml/app/main.py +++ b/services/ml/app/main.py @@ -58,6 +58,18 @@ async def verify_api_key(x_api_key: str = Header(default="")): raise HTTPException(status_code=401, detail="Unauthorized") +# --- User ID Header Dependency --- + +async def get_user_id(x_user_id: str = Header(default="")) -> Optional[str]: + """ + Extract X-User-ID header from incoming requests. + + Returns the user ID if present, or None if not provided. + This is optional — callers decide whether to enforce it. + """ + return x_user_id if x_user_id else None + + # --- Lifespan --- @asynccontextmanager