/
dsboikov
/
aiBoardRoom
Обзор
Документация
Войти
/
dsboikov
/
aiBoardRoom
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/providers/validation.py
53 строки
2 KB
Денис
Grok added
03 авг 2026, 16:31
03 авг 2026, 16:31
255c2e0
Код
Авторство
О чём код?
"""Shared helpers for API key validation probes.""" from __future__ import annotations from dataclasses import dataclass from enum import Enum class KeyStatus(str, Enum): OK = "ok" MISSING = "missing" REJECTED = "rejected" # 401 / explicit auth failure ERROR = "error" # network / unexpected TIMEOUT = "timeout" @dataclass class KeyCheckResult: provider: str status: KeyStatus detail: str = "" @property def ok(self) -> bool: return self.status == KeyStatus.OK def label_ru(self) -> str: if self.status == KeyStatus.OK: return "ОК" if self.status == KeyStatus.MISSING: return "нет ключа" if self.status == KeyStatus.REJECTED: return f"ключ отклонён{f' ({self.detail})' if self.detail else ''}" if self.status == KeyStatus.TIMEOUT: return "таймаут" return f"ошибка API{f' ({self.detail})' if self.detail else ''}" def status_from_http(code: int, *, body_preview: str = "") -> KeyCheckResult | None: """Map HTTP code to auth outcome. Returns None when the caller should try another probe. 401 => rejected. 403 on some endpoints (e.g. xAI /models) is ambiguous. 2xx/400/404 => key was accepted by the gateway (request itself may be invalid). """ preview = (body_preview or "").replace("\n", " ")[:120] if code == 401: return KeyCheckResult("", KeyStatus.REJECTED, f"HTTP {code}") if code == 403: return None # ambiguous — caller may fall back if code < 500: return KeyCheckResult("", KeyStatus.OK, f"HTTP {code}") return KeyCheckResult("", KeyStatus.ERROR, f"HTTP {code} {preview}".strip())