Spaces:
Running
Running
Fix API key lookup endpoint - use proper Pydantic model for request body
Browse files- Created LookupKeyRequest(BaseModel) with token field
- Updated lookup_key_by_token endpoint to accept the model
- Added better error handling with explicit HTTPException re-raising
- Better exception message extraction
This fixes the [object Object] error by ensuring proper JSON parsing
- admin_router.py +14 -4
admin_router.py
CHANGED
|
@@ -23,6 +23,9 @@ class CreateKeyRequest(BaseModel):
|
|
| 23 |
name: str
|
| 24 |
limit_tokens: Optional[int] = 1000000
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
# --- Endpoints ---
|
| 27 |
|
| 28 |
@router.get("/keys", response_model=List[APIKey])
|
|
@@ -93,17 +96,17 @@ async def reset_usage(key_id: str):
|
|
| 93 |
raise HTTPException(status_code=500, detail=str(e))
|
| 94 |
|
| 95 |
@router.post("/keys/lookup")
|
| 96 |
-
async def lookup_key_by_token(
|
| 97 |
"""Lookup API key usage by token (for public dashboard)."""
|
| 98 |
supabase = get_supabase()
|
| 99 |
if not supabase:
|
| 100 |
raise HTTPException(status_code=503, detail="Database unavailable")
|
| 101 |
|
| 102 |
-
if not token or not token.startswith("sk-"):
|
| 103 |
raise HTTPException(status_code=400, detail="Invalid token format")
|
| 104 |
|
| 105 |
try:
|
| 106 |
-
res = supabase.table("api_keys").select("*").eq("token", token).execute()
|
| 107 |
|
| 108 |
if not res.data or len(res.data) == 0:
|
| 109 |
raise HTTPException(status_code=404, detail="Key not found")
|
|
@@ -119,5 +122,12 @@ async def lookup_key_by_token(token: str):
|
|
| 119 |
"created_at": key.get("created_at"),
|
| 120 |
"is_active": key.get("is_active", True)
|
| 121 |
}
|
|
|
|
|
|
|
| 122 |
except Exception as e:
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
name: str
|
| 24 |
limit_tokens: Optional[int] = 1000000
|
| 25 |
|
| 26 |
+
class LookupKeyRequest(BaseModel):
|
| 27 |
+
token: str
|
| 28 |
+
|
| 29 |
# --- Endpoints ---
|
| 30 |
|
| 31 |
@router.get("/keys", response_model=List[APIKey])
|
|
|
|
| 96 |
raise HTTPException(status_code=500, detail=str(e))
|
| 97 |
|
| 98 |
@router.post("/keys/lookup")
|
| 99 |
+
async def lookup_key_by_token(req: LookupKeyRequest):
|
| 100 |
"""Lookup API key usage by token (for public dashboard)."""
|
| 101 |
supabase = get_supabase()
|
| 102 |
if not supabase:
|
| 103 |
raise HTTPException(status_code=503, detail="Database unavailable")
|
| 104 |
|
| 105 |
+
if not req.token or not req.token.startswith("sk-"):
|
| 106 |
raise HTTPException(status_code=400, detail="Invalid token format")
|
| 107 |
|
| 108 |
try:
|
| 109 |
+
res = supabase.table("api_keys").select("*").eq("token", req.token).execute()
|
| 110 |
|
| 111 |
if not res.data or len(res.data) == 0:
|
| 112 |
raise HTTPException(status_code=404, detail="Key not found")
|
|
|
|
| 122 |
"created_at": key.get("created_at"),
|
| 123 |
"is_active": key.get("is_active", True)
|
| 124 |
}
|
| 125 |
+
except HTTPException:
|
| 126 |
+
raise
|
| 127 |
except Exception as e:
|
| 128 |
+
error_msg = str(e)
|
| 129 |
+
if hasattr(e, 'message'):
|
| 130 |
+
error_msg = str(e.message)
|
| 131 |
+
elif hasattr(e, 'args') and len(e.args) > 0:
|
| 132 |
+
error_msg = str(e.args[0])
|
| 133 |
+
raise HTTPException(status_code=500, detail=error_msg)
|