да
This commit is contained in:
1 parent
5d76e154e0
commit
763ca6062d
3 files changed
+80
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import aiohttp
|
||||
import asyncio
|
||||
import json
|
||||
import re
|
||||
import gconfig
|
||||
|
||||
try:
|
||||
@@ -10,9 +11,30 @@ except:
|
||||
finally:
|
||||
from config import *
|
||||
|
||||
|
||||
def parse_error_body(status: int, body: str) -> str:
|
||||
try:
|
||||
data = json.loads(body)
|
||||
if isinstance(data, dict):
|
||||
err = data.get("error")
|
||||
if isinstance(err, dict):
|
||||
return f"{status} {err.get('message') or err.get('status') or err}"
|
||||
if err:
|
||||
return f"{status} {err}"
|
||||
return f"{status} {data.get('message') or body[:200]}"
|
||||
return f"{status} {body[:200]}"
|
||||
except Exception:
|
||||
pass
|
||||
clean = re.sub(r"<[^>]+>", " ", body).strip()
|
||||
return f"{status} {clean[:200]}"
|
||||
|
||||
|
||||
async def make_request(url, headers, payload):
|
||||
async with aiohttp.ClientSession(headers=headers) as session:
|
||||
async with session.post(url, json=payload) as response:
|
||||
if response.status != 200:
|
||||
error_body = await response.text()
|
||||
raise Exception(parse_error_body(response.status, error_body))
|
||||
if payload.get("stream") == True:
|
||||
async for line in response.content:
|
||||
line = line.decode("utf-8").strip()
|
||||
|
||||
Reference in new issue
Block a user