Files
ai-balance-monitor/app/providers/openrouter.py
T

30 lines
932 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""OpenRouter 余额。
接口:GET https://openrouter.ai/api/v1/credits
响应:{"data": {"total_credits": 100.5, "total_usage": 25.75}}
余额取剩余可用:total_credits - total_usage
"""
from __future__ import annotations
from app.expr import evaluate_balance
from app.providers.base import BalanceProvider
class OpenRouterProvider(BalanceProvider):
id = "openrouter"
name = "OpenRouter"
currency = "USD"
icon = "OpenRouter"
description = "OpenRouter 余额(GET /api/v1/creditsBearer 认证,剩余 = total_credits - total_usage"
def build_request(self, api_key: str) -> tuple[str, dict, str | None]:
return (
"https://openrouter.ai/api/v1/credits",
{"Authorization": f"Bearer {api_key}"},
None,
)
def extract_balance(self, data: object) -> float:
return evaluate_balance(data, "data.total_credits - data.total_usage")