feat: add processing of api error when code=200

Этот коммит содержится в:
Vlad Pronsky 2023-07-10 23:34:07 +03:00
родитель 42357d0233
Коммит 7866e44ade
3 изменённых файлов: 25 добавлений и 3 удалений

Просмотреть файл

@ -2,9 +2,8 @@ from httpx import Response
from .accounts_pool import AccountsPool from .accounts_pool import AccountsPool
from .constants import GQL_FEATURES, GQL_URL from .constants import GQL_FEATURES, GQL_URL
from .logger import logger
from .models import Tweet, User from .models import Tweet, User
from .queue_client import QueueClient, req_id from .queue_client import QueueClient
from .utils import encode_params, find_obj, get_by_path, to_old_obj, to_old_rep from .utils import encode_params, find_obj, get_by_path, to_old_obj, to_old_rep
SEARCH_FEATURES = { SEARCH_FEATURES = {

Просмотреть файл

@ -76,6 +76,10 @@ async def main(args):
await pool.load_from_file(args.file_path, args.line_format) await pool.load_from_file(args.file_path, args.line_format)
return return
if args.command == "del_accounts":
await pool.delete_accounts(args.usernames)
return
if args.command == "login_accounts": if args.command == "login_accounts":
print(await pool.login_all()) print(await pool.login_all())
return return

Просмотреть файл

@ -25,6 +25,16 @@ class Ctx:
self.req_count = 0 self.req_count = 0
class ApiError(Exception):
def __init__(self, rep: httpx.Response, res: dict):
self.rep = rep
self.res = res
self.errors = [x["message"] for x in res["errors"]]
def __str__(self):
return f"ApiError ({self.rep.status_code}) {' ~ '.join(self.errors)}"
class QueueClient: class QueueClient:
def __init__(self, pool: AccountsPool, queue: str, debug=False): def __init__(self, pool: AccountsPool, queue: str, debug=False):
self.pool = pool self.pool = pool
@ -98,7 +108,12 @@ class QueueClient:
rep = await ctx.clt.request(method, url, params=params) rep = await ctx.clt.request(method, url, params=params)
setattr(rep, "__username", ctx.acc.username) setattr(rep, "__username", ctx.acc.username)
self._push_history(rep) self._push_history(rep)
rep.raise_for_status() rep.raise_for_status()
res = rep.json()
if "errors" in res:
raise ApiError(rep, res)
ctx.req_count += 1 # count only successful ctx.req_count += 1 # count only successful
retry_count = 0 retry_count = 0
return rep return rep
@ -125,11 +140,15 @@ class QueueClient:
else: else:
known_code = False known_code = False
logger.debug(f"HTTP Error {rep.status_code} {e.request.url}\n{rep.text}") logger.warning(f"HTTP Error {rep.status_code} {e.request.url}\n{rep.text}")
await self._close_ctx(reset_ts) await self._close_ctx(reset_ts)
if not known_code: if not known_code:
raise e raise e
except ApiError as e:
reset_ts = utc_ts() + 60 * 60 * 4 # 4 hours
await self._close_ctx(reset_ts)
logger.warning(e)
except Exception as e: except Exception as e:
logger.warning(f"Unknown error, retrying. Err ({type(e)}): {str(e)}") logger.warning(f"Unknown error, retrying. Err ({type(e)}): {str(e)}")
retry_count += 1 retry_count += 1