Spaces:
Sleeping
Sleeping
File size: 1,517 Bytes
56945de 1e9b4a1 56945de 1e9b4a1 8456fc7 1e9b4a1 56945de 1e9b4a1 56945de 1e9b4a1 56945de 8456fc7 56945de 1e9b4a1 8456fc7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import os
from typing import Any
import supabase as sb
from fastapi import APIRouter, Header, HTTPException
from middleware.rate_limit import get_usage_daily
router = APIRouter()
SUPABASE_URL = os.environ["SUPABASE_URL"]
SUPABASE_SERVICE_ROLE_KEY = os.environ["SUPABASE_SERVICE_ROLE_KEY"]
def get_supabase() -> sb.Client:
return sb.create_client(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY)
def extract_user_id(token: str, db: sb.Client) -> str:
try:
user_response = db.auth.get_user(token)
return str(user_response.user.id)
except Exception as e:
raise HTTPException(status_code=401, detail=f"μ ν¨νμ§ μμ ν ν°μ
λλ€: {e}")
@router.get("/usage")
async def usage(authorization: str = Header(...)) -> dict[str, Any]:
"""
μ€λ **μ€μΊ** νμλ₯Ό λ°ννλ€.
μ§κ³λ `usage_daily.scans` νλλ§ λ³Έλ€(`daily_usage`λ νκΈ°). λΆμ μλ μ¬κΈ° μμ§ μλλ€.
νλκ° μμ΄μ‘μΌλ―λ‘ `usage_limit`Β·`remaining`μ μλ΅μμ λΊλ€ β λ¨μ νμλΌλ κ°λ
μ체κ°
μλ€. μ«μλ ν΅κ³Β·λ¨μ© μΆμ λͺ©μ μΌλ‘λ§ μΌλ€.
"""
if not authorization.startswith("Bearer "):
raise HTTPException(status_code=401, detail="Authorization ν€λκ° μ¬λ°λ₯΄μ§ μμ΅λλ€.")
token = authorization.removeprefix("Bearer ").strip()
db = get_supabase()
user_id = extract_user_id(token, db)
usage_today = get_usage_daily(user_id, db)["scans"]
return {"usage_today": usage_today}
|