diff --git a/easyverein/api.py b/easyverein/api.py index 266f7af..5e1a423 100644 --- a/easyverein/api.py +++ b/easyverein/api.py @@ -19,6 +19,7 @@ def __init__( api_version="v1.7", base_url: str = "https://hexa.easyverein.com/api/", logger: logging.Logger = None, + auto_retry=False, ): """ Constructor setting API key and logger. Test @@ -31,7 +32,9 @@ def __init__( else: self.logger = logging.getLogger("easyverein") - self.c = EasyvereinClient(api_key, api_version, base_url, self.logger, self) + self.c = EasyvereinClient( + api_key, api_version, base_url, self.logger, self, auto_retry + ) # Add methods diff --git a/easyverein/core/client.py b/easyverein/core/client.py index 86b6869..371e9c5 100644 --- a/easyverein/core/client.py +++ b/easyverein/core/client.py @@ -5,6 +5,7 @@ import logging from pathlib import Path +from time import sleep from typing import TYPE_CHECKING, Any, TypeVar import requests @@ -35,6 +36,7 @@ def __init__( base_url, logger: logging.Logger, instance: EasyvereinAPI, + auto_retry=False, ): """ Constructor setting API key and logger @@ -44,6 +46,7 @@ def __init__( self.api_version = api_version self.logger = logger self.api_instance = instance + self.auto_retry = auto_retry def _get_header(self): """ @@ -118,10 +121,21 @@ def _do_request( # noqa: PLR0913 "Request returned status code 429, too many requests. Wait %d seconds", retry_after, ) - raise EasyvereinAPITooManyRetriesException( - f"Too many requests, please wait {retry_after} seconds and try again.", - retry_after=retry_after, - ) + if self.auto_retry: + self.logger.warning("Retrying after %d seconds sleep.", retry_after) + sleep(retry_after) + if files: + for v in files.values(): + v.seek( + 0 + ) # reset file seek, as it has been moved by the previous call + + return self._do_request(method, url, binary, data, headers, files) + else: + raise EasyvereinAPITooManyRetriesException( + f"Too many requests, please wait {retry_after} seconds and try again.", + retry_after=retry_after, + ) if res.status_code == 404: self.logger.warning("Request returned status code 404, resource not found")