feat: support upstream token refresh
This commit is contained in:
@@ -3,14 +3,17 @@ from __future__ import annotations
|
||||
import json
|
||||
import logging
|
||||
from decimal import Decimal
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.database import SessionLocal
|
||||
from app.models.snapshot import UpstreamRateSnapshot
|
||||
from app.models.upstream import Upstream
|
||||
from app.models.upstream_key import UpstreamGeneratedKey
|
||||
from app.models.website import Website, WebsiteGroupBinding, WebsiteSyncLog
|
||||
from app.services.auth_config import normalize_auth_config
|
||||
from app.services.website_client import Sub2ApiWebsiteClient, WebsiteError, _extract_id, calculate_target_rate
|
||||
from app.services.upstream_client import UpstreamClient
|
||||
from app.services import webhook_service
|
||||
@@ -23,6 +26,25 @@ PRIORITY_BASE = 1
|
||||
PRIORITY_STEP = 10
|
||||
|
||||
|
||||
def _persist_upstream_auth_config(upstream: Upstream, updated_config: dict[str, Any]) -> None:
|
||||
config_json = json.dumps(
|
||||
normalize_auth_config(upstream.auth_type, updated_config),
|
||||
ensure_ascii=False,
|
||||
)
|
||||
updated_at = datetime.now(timezone.utc)
|
||||
db = SessionLocal()
|
||||
try:
|
||||
row = db.query(Upstream).filter(Upstream.id == upstream.id).first()
|
||||
if row:
|
||||
row.auth_config_json = config_json
|
||||
row.updated_at = updated_at
|
||||
db.commit()
|
||||
upstream.auth_config_json = row.auth_config_json
|
||||
upstream.updated_at = updated_at
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def priority_for_rate_rank(rank: int) -> int:
|
||||
"""Convert a zero-based sorted rate rank to an account priority."""
|
||||
return PRIORITY_BASE + rank * PRIORITY_STEP
|
||||
@@ -672,6 +694,7 @@ def reconcile_upstream_keys_full(db: Session, upstream_id: int) -> bool:
|
||||
auth_type=upstream.auth_type,
|
||||
auth_config=auth_config,
|
||||
timeout=float(upstream.timeout_seconds),
|
||||
on_auth_config_update=lambda updated: _persist_upstream_auth_config(upstream, updated),
|
||||
) as client:
|
||||
client.login()
|
||||
# 获取远端 Key 列表(支持自定义 managed_prefix)
|
||||
|
||||
Reference in New Issue
Block a user