30 lines
932 B
Python
30 lines
932 B
Python
"""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/credits,Bearer 认证,剩余 = 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")
|