Add X-User-ID header extraction to FastAPI service.
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 <noreply@anthropic.com>
This commit is contained in:
parent
b9f6770f2a
commit
399e760fa5
1 changed files with 12 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue