Ping ``/v1/models`` to check if an API key is valid. Returns ``(ok, detail)`` — *detail* is a human-readable status string.
(base_url: str, api_key: str)
| 186 | |
| 187 | |
| 188 | def verify_key(base_url: str, api_key: str) -> tuple[bool, str]: |
| 189 | """Ping ``/v1/models`` to check if an API key is valid. |
| 190 | |
| 191 | Returns ``(ok, detail)`` — *detail* is a human-readable status string. |
| 192 | """ |
| 193 | import httpx |
| 194 | |
| 195 | url = f"{base_url.rstrip('/')}/models" |
| 196 | try: |
| 197 | resp = httpx.get( |
| 198 | url, |
| 199 | headers={ |
| 200 | "authorization": f"Bearer {api_key}", |
| 201 | "user-agent": "uncommon-route/provider-check", |
| 202 | }, |
| 203 | timeout=httpx.Timeout(8.0, connect=4.0), |
| 204 | ) |
| 205 | if resp.status_code == 200: |
| 206 | data = resp.json() |
| 207 | count = len(data.get("data", [])) |
| 208 | return True, f"{count} models available" |
| 209 | if resp.status_code == 401: |
| 210 | return False, "invalid or expired API key (HTTP 401)" |
| 211 | if resp.status_code == 403: |
| 212 | return False, "access denied (HTTP 403)" |
| 213 | return False, f"unexpected HTTP {resp.status_code}" |
| 214 | except httpx.ConnectError: |
| 215 | return False, f"cannot connect to {url}" |
| 216 | except httpx.TimeoutException: |
| 217 | return False, f"timeout connecting to {url}" |
| 218 | except Exception as exc: # noqa: BLE001 |
| 219 | return False, str(exc) |
| 220 | |
| 221 | |
| 222 | def cmd_provider(args: list[str]) -> None: |
no test coverage detected