24 行
912 B
Python
24 行
912 B
Python
"""用量统计 API:/api/usage/summary(个人)+ /api/usage/monthly(月度配额概览)。"""
|
||
from __future__ import annotations
|
||
|
||
from fastapi import APIRouter, Depends, Header, HTTPException
|
||
from sqlalchemy.orm import Session
|
||
|
||
from app.core.db import get_session
|
||
from app.services.auth_service import AuthService
|
||
from app.services.usage_service import UsageService
|
||
|
||
router = APIRouter(prefix="/usage", tags=["usage"])
|
||
|
||
|
||
def _bearer(authorization: str | None) -> str:
|
||
if not authorization or not authorization.lower().startswith("bearer "):
|
||
raise HTTPException(401, "未登录")
|
||
return authorization[7:].strip()
|
||
|
||
|
||
@router.get("/summary")
|
||
async def summary(days: int = 7, authorization: str | None = Header(None), session: Session = Depends(get_session)):
|
||
user = AuthService(session).me(_bearer(authorization))
|
||
return UsageService(session).summary(user["id"], days=min(days, 30))
|