Spaces:
Sleeping
Sleeping
Download api/usage.py from BOWOWEEEEE/pill-advisor-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/spaces/BOWOWEEEEE/pill-advisor-backend/resolve/main/api/usage.py
- Command line
-
hf download hf://spaces/BOWOWEEEEE/pill-advisor-backend/api/usage.py
-
curl -L -o usage.py https://huggingface.co/spaces/BOWOWEEEEE/pill-advisor-backend/resolve/main/api/usage.py
1.52 kB
| 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}") | |
| 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} | |