diff --git a/api-client/libretime_api_client/_client.py b/api-client/libretime_api_client/_client.py index f1eaac2f3..a2ae4975b 100644 --- a/api-client/libretime_api_client/_client.py +++ b/api-client/libretime_api_client/_client.py @@ -26,20 +26,26 @@ class TimeoutHTTPAdapter(HTTPAdapter): return super().send(request, *args, **kwargs) +def default_retry(max_retries: int = 5): + return Retry( + total=max_retries, + backoff_factor=2, + status_forcelist=[413, 429, 500, 502, 503, 504], + ) + + class Session(BaseSession): base_url: Optional[str] - def __init__(self, base_url: Optional[str] = None): + def __init__( + self, + base_url: Optional[str] = None, + retry: Optional[Retry] = None, + ): super().__init__() self.base_url = base_url - retry_strategy = Retry( - total=5, - backoff_factor=2, - status_forcelist=[413, 429, 500, 502, 503, 504], - ) - - adapter = TimeoutHTTPAdapter(max_retries=retry_strategy) + adapter = TimeoutHTTPAdapter(max_retries=retry) self.mount("http://", adapter) self.mount("https://", adapter) @@ -61,9 +67,16 @@ class AbstractApiClient: session: Session base_url: str - def __init__(self, base_url: str): + def __init__( + self, + base_url: str, + retry: Optional[Retry] = None, + ): self.base_url = base_url - self.session = Session(base_url=base_url) + self.session = Session( + base_url=base_url, + retry=retry, + ) def _request( self, diff --git a/api-client/libretime_api_client/v2.py b/api-client/libretime_api_client/v2.py index 07d65f923..1c6982f56 100644 --- a/api-client/libretime_api_client/v2.py +++ b/api-client/libretime_api_client/v2.py @@ -1,11 +1,14 @@ -from ._client import AbstractApiClient, Response +from ._client import AbstractApiClient, Response, default_retry class ApiClient(AbstractApiClient): VERSION = "2.0" def __init__(self, base_url: str, api_key: str): - super().__init__(base_url=base_url) + super().__init__( + base_url=base_url, + retry=default_retry(), + ) self.session.headers.update({"Authorization": f"Api-Key {api_key}"}) def get_info(self, **kwargs) -> Response: