diff --git a/tts_api_library/client.py b/tts_api_library/client.py index 4e1a1cd..fb380c6 100644 --- a/tts_api_library/client.py +++ b/tts_api_library/client.py @@ -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}") \ No newline at end of file