测试 Codex2API 连接。
(api_url: str, admin_key: str)
| 140 | |
| 141 | |
| 142 | def test_codex2api_connection(api_url: str, admin_key: str) -> Tuple[bool, str]: |
| 143 | """测试 Codex2API 连接。""" |
| 144 | if not api_url: |
| 145 | return False, "API URL 不能为空" |
| 146 | if not admin_key: |
| 147 | return False, "Admin Key 不能为空" |
| 148 | |
| 149 | url = normalize_codex2api_url(api_url) + "/api/admin/health" |
| 150 | headers = {"X-Admin-Key": admin_key} |
| 151 | |
| 152 | try: |
| 153 | response = cffi_requests.get( |
| 154 | url, |
| 155 | headers=headers, |
| 156 | proxies=None, |
| 157 | timeout=10, |
| 158 | impersonate="chrome110", |
| 159 | ) |
| 160 | if response.status_code in (200, 204): |
| 161 | return True, "Codex2API 连接测试成功" |
| 162 | if response.status_code == 401: |
| 163 | return False, "连接成功,但 Admin Key 无效" |
| 164 | if response.status_code == 403: |
| 165 | return False, "连接成功,但权限不足" |
| 166 | return False, f"服务器返回异常状态码: {response.status_code}" |
| 167 | except cffi_requests.exceptions.ConnectionError as e: |
| 168 | return False, f"无法连接到服务器: {str(e)}" |
| 169 | except cffi_requests.exceptions.Timeout: |
| 170 | return False, "连接超时,请检查网络配置" |
| 171 | except Exception as e: |
| 172 | return False, f"连接测试失败: {str(e)}" |
no test coverage detected