MCPcopy Create free account
hub / github.com/CommonstackAI/UncommonRoute / handle_spend

Function handle_spend

uncommon_route/proxy.py:3361–3387  ·  view source on GitHub ↗

GET /v1/spend — current spend status. POST /v1/spend — set limits.

(request: Request)

Source from the content-addressed store, hash-verified

3359 pass
3360
3361 async def handle_spend(request: Request) -> JSONResponse:
3362 """GET /v1/spend — current spend status. POST /v1/spend — set limits."""
3363 if request.method == "GET":
3364 s = _spend.status()
3365 return JSONResponse({
3366 "limits": {k: v for k, v in vars(s.limits).items() if v is not None},
3367 "spent": s.spent,
3368 "remaining": {k: v for k, v in s.remaining.items() if v is not None},
3369 "calls": s.calls,
3370 })
3371 denied = _admin_auth_failure(request)
3372 if denied is not None:
3373 return denied
3374 body = await request.json()
3375 action = body.get("action", "set")
3376 window = body.get("window")
3377 amount = body.get("amount")
3378 if action == "set" and window and amount is not None:
3379 _spend.set_limit(window, float(amount))
3380 return JSONResponse({"ok": True, "window": window, "amount": amount})
3381 if action == "clear" and window:
3382 _spend.clear_limit(window)
3383 return JSONResponse({"ok": True, "window": window, "cleared": True})
3384 if action == "reset_session":
3385 _spend.reset_session()
3386 return JSONResponse({"ok": True, "session_reset": True})
3387 return JSONResponse({"error": "Invalid action"}, status_code=400)
3388
3389 async def handle_stats(request: Request) -> JSONResponse:
3390 """GET /v1/stats — route analytics. POST /v1/stats — reset."""

Callers

nothing calls this directly

Calls 6

_admin_auth_failureFunction · 0.85
set_limitMethod · 0.80
clear_limitMethod · 0.80
reset_sessionMethod · 0.80
statusMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected