22import os
33from typing import Any
44
5- import httpx
5+ from httpx import AsyncClient , HTTPStatusError , ProxyError , ReadTimeout , Response
66
77from .accounts_pool import Account , AccountsPool
88from .logger import logger
1313
1414
1515class Ctx :
16- def __init__ (self , acc : Account , clt : httpx . AsyncClient ):
16+ def __init__ (self , acc : Account , clt : AsyncClient ):
1717 self .acc = acc
1818 self .clt = clt
1919 self .req_count = 0
@@ -27,7 +27,7 @@ class AbortReqError(Exception):
2727 pass
2828
2929
30- def req_id (rep : httpx . Response ):
30+ def req_id (rep : Response ):
3131 lr = str (rep .headers .get ("x-rate-limit-remaining" , - 1 ))
3232 ll = str (rep .headers .get ("x-rate-limit-limit" , - 1 ))
3333 sz = max (len (lr ), len (ll ))
@@ -37,7 +37,7 @@ def req_id(rep: httpx.Response):
3737 return f"{ lr } /{ ll } - { username } "
3838
3939
40- def dump_rep (rep : httpx . Response ):
40+ def dump_rep (rep : Response ):
4141 count = getattr (dump_rep , "__count" , - 1 ) + 1
4242 setattr (dump_rep , "__count" , count )
4343
@@ -108,7 +108,7 @@ async def _get_ctx(self):
108108 self .ctx = Ctx (acc , clt )
109109 return self .ctx
110110
111- async def _check_rep (self , rep : httpx . Response ) -> None :
111+ async def _check_rep (self , rep : Response ) -> None :
112112 """
113113 This function can raise Exception and request will be retried or aborted
114114 Or if None is returned, response will passed to api parser as is
@@ -186,18 +186,18 @@ async def _check_rep(self, rep: httpx.Response) -> None:
186186
187187 try :
188188 rep .raise_for_status ()
189- except httpx . HTTPStatusError :
189+ except HTTPStatusError :
190190 logger .error (f"Unhandled API response code: { log_msg } " )
191191 await self ._close_ctx (utc .ts () + 60 * 15 ) # 15 minutes
192192 raise HandledError ()
193193
194194 async def get (self , url : str , params : ReqParams = None ):
195195 return await self .req ("GET" , url , params = params )
196196
197- async def req (self , method : str , url : str , params : ReqParams = None ) -> httpx . Response | None :
197+ async def req (self , method : str , url : str , params : ReqParams = None ) -> Response | None :
198198 retry_count = 0
199199 while True :
200- ctx = await self ._get_ctx ()
200+ ctx = await self ._get_ctx () # not need to close client, class implements __aexit__
201201 if ctx is None :
202202 return None
203203
@@ -215,7 +215,7 @@ async def req(self, method: str, url: str, params: ReqParams = None) -> httpx.Re
215215 except HandledError :
216216 # retry with new account
217217 continue
218- except (httpx . ReadTimeout , httpx . ProxyError ):
218+ except (ReadTimeout , ProxyError ):
219219 # http transport failed, just retry with same account
220220 continue
221221 except Exception as e :
0 commit comments