[UP] move functions
This commit is contained in:
parent
fa1942f55a
commit
ef9bb6bf57
1 changed files with 35 additions and 36 deletions
|
|
@ -27,6 +27,40 @@ class TTSApiError(RuntimeError):
|
||||||
pass
|
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:
|
class TTSClient:
|
||||||
|
|
@ -161,39 +195,4 @@ class TTSAioClient:
|
||||||
status = r.status
|
status = r.status
|
||||||
text = await r.text()
|
text = await r.text()
|
||||||
detail = _extract_error_detail_from_text(text)
|
detail = _extract_error_detail_from_text(text)
|
||||||
raise TTSApiError(f"{status}: {detail}")
|
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"
|
|
||||||
Loading…
Add table
Reference in a new issue