[UP] move functions

This commit is contained in:
Evgeny (Krymmy) Momotov 2025-09-11 14:22:09 +03:00
parent fa1942f55a
commit ef9bb6bf57

View file

@ -27,6 +27,40 @@ class TTSApiError(RuntimeError):
pass
# --------- утилиты ---------
def _safe_json_sync(r: requests.Response) -> Any:
try:
return r.json()
except Exception:
# если пришёл не JSON, но статус 2xx — вернём сырой текст
return {"text": r.text}
def _extract_error_detail_sync(r: requests.Response) -> str:
try:
j = r.json()
if isinstance(j, dict) and "detail" in j:
return str(j["detail"])
except Exception:
pass
return r.text or "unknown error"
async def _safe_json_async(r: aiohttp.ClientResponse) -> Any:
try:
return await r.json()
except Exception:
txt = await r.text()
return {"text": txt}
def _extract_error_detail_from_text(text: str) -> str:
try:
j = _json.loads(text)
if isinstance(j, dict) and "detail" in j:
return str(j["detail"])
except Exception:
pass
return text or "unknown error"
# --------- синхронный клиент ---------
class TTSClient:
@ -161,39 +195,4 @@ class TTSAioClient:
status = r.status
text = await r.text()
detail = _extract_error_detail_from_text(text)
raise TTSApiError(f"{status}: {detail}")
# --------- утилиты ---------
def _safe_json_sync(r: requests.Response) -> Any:
try:
return r.json()
except Exception:
# если пришёл не JSON, но статус 2xx — вернём сырой текст
return {"text": r.text}
def _extract_error_detail_sync(r: requests.Response) -> str:
try:
j = r.json()
if isinstance(j, dict) and "detail" in j:
return str(j["detail"])
except Exception:
pass
return r.text or "unknown error"
async def _safe_json_async(r: aiohttp.ClientResponse) -> Any:
try:
return await r.json()
except Exception:
txt = await r.text()
return {"text": txt}
def _extract_error_detail_from_text(text: str) -> str:
try:
j = _json.loads(text)
if isinstance(j, dict) and "detail" in j:
return str(j["detail"])
except Exception:
pass
return text or "unknown error"
raise TTSApiError(f"{status}: {detail}")