[docs]defbuild_requests_session(raise_for_status:bool=True,retry:bool|int|Retry=True,auth:TokenAuth|None=None,)->Session:""" Create a requests session. :param raise_for_status: If True, a hook to invoke raise_for_status be installed :param retry: If true, it will use default Retry configuration. if an integer, it will use default Retry configuration with given integer as total retry count. if Retry instance, it will use this instance. :param auth: Optional TokenAuth instance to be used to provide the Authorization header to the session :return: configured requests Session """session=Session()ifraise_for_status:session.hooks={"response":[lambdar,*_,**__:r.raise_for_status()]}ifretry:ifisinstance(retry,bool):retry=Retry()elifisinstance(retry,int):retry=Retry(retry)elifnotisinstance(retry,Retry):raiseValueError("retry should be a bool, int or Retry instance.")adapter=HTTPAdapter(max_retries=retry)session.mount("http://",adapter)session.mount("https://",adapter)ifauth:logger.debug("setting up default session authentication")session.auth=authreturnsession
_R=TypeVar("_R")
[docs]defsuppress_http_error_for_codes(*codes:int,)->Callable[[Callable[...,_R]],Callable[...,_R|None]]:""" For the codes given, return a decorator that will suppress HTTPErrors that are raised from responses that came with one of those status codes. The function will return False instead of raising the HTTPError """def_suppress_http_error_for_codes(func:Callable[...,_R],)->Callable[...,_R|None]:@wraps(func)def_wrapper(*a:Any,**kw:Any)->_R|None:try:returnfunc(*a,**kw)exceptHTTPErroraserr:iferr.responseanderr.response.status_codeincodes:logger.warning("%s received response %s: %s",func.__qualname__,err.response.status_code,str(err),)returnNonereturn_wrapperreturn_suppress_http_error_for_codes