Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /GRID-INTELLIGENCE_GRID /src /application /mothership /routers /billing.py
| """ | |
| Billing API router. | |
| Endpoints for usage statistics and billing information. | |
| """ | |
| from __future__ import annotations | |
| import logging | |
| from fastapi import APIRouter, HTTPException, Query, status | |
| from ..dependencies import RequiredAuth, UoW | |
| from ..exceptions import ResourceNotFoundError | |
| from ..schemas.payment import InvoiceResponse | |
| from ..services.billing import BillingService | |
| logger = logging.getLogger(__name__) | |
| router = APIRouter(prefix="/billing", tags=["billing"]) | |
| async def get_usage_summary( | |
| auth: RequiredAuth, | |
| uow: UoW, | |
| period_days: int = Query(default=30, ge=1, le=365, description="Period in days"), | |
| ) -> dict: | |
| """ | |
| Get usage summary for the current user. | |
| Returns usage statistics, tier limits, and current consumption. | |
| """ | |
| user_id = auth.get("user_id", "unknown") | |
| billing_service = BillingService( | |
| subscription_repo=uow.subscriptions, | |
| invoice_repo=uow.invoices, | |
| usage_repo=uow.usage, | |
| ) | |
| summary = await billing_service.get_usage_summary(user_id, period_days) | |
| return summary | |
| async def list_invoices( | |
| auth: RequiredAuth, | |
| uow: UoW, | |
| ) -> list[InvoiceResponse]: | |
| """List all invoices for the current user.""" | |
| user_id = auth.get("user_id", "unknown") | |
| invoices = await uow.invoices.get_by_user(user_id) | |
| return [InvoiceResponse(**invoice.to_dict()) for invoice in invoices] | |
| async def get_invoice( | |
| invoice_id: str, | |
| auth: RequiredAuth, | |
| uow: UoW, | |
| ) -> InvoiceResponse: | |
| """Get invoice details.""" | |
| invoice = await uow.invoices.get(invoice_id) | |
| if not invoice: | |
| raise ResourceNotFoundError("invoice", invoice_id) | |
| user_id = auth.get("user_id", "unknown") | |
| if invoice.user_id != user_id: | |
| raise HTTPException( | |
| status_code=status.HTTP_403_FORBIDDEN, | |
| detail="Access denied", | |
| ) | |
| return InvoiceResponse(**invoice.to_dict()) | |
Xet Storage Details
- Size:
- 2.09 kB
- Xet hash:
- aaeea646284714eb4d30970f384ae40bf2ab762495bd86f732598afd5be65a8c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.