测试 Sub2API 连接(GET /api/v1/admin/accounts/data 探活) Returns: (成功标志, 消息)
(api_url: str, api_key: str)
| 186 | |
| 187 | |
| 188 | def test_sub2api_connection(api_url: str, api_key: str) -> Tuple[bool, str]: |
| 189 | """ |
| 190 | 测试 Sub2API 连接(GET /api/v1/admin/accounts/data 探活) |
| 191 | |
| 192 | Returns: |
| 193 | (成功标志, 消息) |
| 194 | """ |
| 195 | if not api_url: |
| 196 | return False, "API URL 不能为空" |
| 197 | if not api_key: |
| 198 | return False, "API Key 不能为空" |
| 199 | |
| 200 | url = api_url.rstrip("/") + "/api/v1/admin/accounts/data" |
| 201 | headers = {"x-api-key": api_key} |
| 202 | |
| 203 | try: |
| 204 | response = cffi_requests.get( |
| 205 | url, |
| 206 | headers=headers, |
| 207 | proxies=None, |
| 208 | timeout=10, |
| 209 | impersonate="chrome110", |
| 210 | ) |
| 211 | |
| 212 | if response.status_code in (200, 201, 204, 405): |
| 213 | return True, "Sub2API 连接测试成功" |
| 214 | if response.status_code == 401: |
| 215 | return False, "连接成功,但 API Key 无效" |
| 216 | if response.status_code == 403: |
| 217 | return False, "连接成功,但权限不足" |
| 218 | |
| 219 | return False, f"服务器返回异常状态码: {response.status_code}" |
| 220 | |
| 221 | except cffi_requests.exceptions.ConnectionError as e: |
| 222 | return False, f"无法连接到服务器: {str(e)}" |
| 223 | except cffi_requests.exceptions.Timeout: |
| 224 | return False, "连接超时,请检查网络配置" |
| 225 | except Exception as e: |
| 226 | return False, f"连接测试失败: {str(e)}" |
no test coverage detected