yangtb24 commited on
Commit
86543ba
·
verified ·
1 Parent(s): b1e9de0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -51,16 +51,23 @@ def get_credit_summary(api_key):
51
  "Authorization": f"Bearer {api_key}",
52
  "Content-Type": "application/json"
53
  }
54
- try:
55
- response = requests.get(API_ENDPOINT, headers=headers)
56
- response.raise_for_status()
57
- data = response.json().get("data", {})
58
- total_balance = data.get("totalBalance", 0)
59
- logging.info(f"获取额度,API Key:{api_key},当前额度: {total_balance}")
60
- return {"total_balance": float(total_balance)}
61
- except requests.exceptions.RequestException as e:
62
- logging.error(f"获取额度信息失败,API Key:{api_key},错误信息:{e}")
63
- return None
 
 
 
 
 
 
 
64
 
65
  FREE_MODEL_TEST_KEY = (
66
  "sk-bmjbjzleaqfgtqfzmcnsbagxrlohriadnxqrzfocbizaxukw"
 
51
  "Authorization": f"Bearer {api_key}",
52
  "Content-Type": "application/json"
53
  }
54
+ max_retries = 5
55
+ retry_delay = 5
56
+ for attempt in range(max_retries):
57
+ try:
58
+ response = requests.get(API_ENDPOINT, headers=headers, timeout=5)
59
+ response.raise_for_status()
60
+ data = response.json().get("data", {})
61
+ total_balance = data.get("totalBalance", 0)
62
+ logging.info(f"API Key:{api_key},当前额度: {total_balance}")
63
+ return {"total_balance": float(total_balance)}
64
+ except requests.exceptions.RequestException as e:
65
+ logging.error(f"获取额度信息失败,API Key:{api_key},尝试次数:{attempt+1}/{max_retries},错误信息:{e}")
66
+ if attempt < max_retries - 1:
67
+ time.sleep(retry_delay)
68
+ else:
69
+ logging.error(f"获取额度信息失败,API Key:{api_key},所有重试次数均已失败")
70
+ return None
71
 
72
  FREE_MODEL_TEST_KEY = (
73
  "sk-bmjbjzleaqfgtqfzmcnsbagxrlohriadnxqrzfocbizaxukw"