From 7892d0297423455888483e9717f76b417270a1d2 Mon Sep 17 00:00:00 2001 From: codemonk Date: Wed, 14 Feb 2024 13:27:15 +0100 Subject: [PATCH 1/5] simple retry on EasyvereinAPITooManyRetriesException --- easyverein/core/client.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/easyverein/core/client.py b/easyverein/core/client.py index 86b6869..4f143e0 100644 --- a/easyverein/core/client.py +++ b/easyverein/core/client.py @@ -6,6 +6,7 @@ import logging from pathlib import Path from typing import TYPE_CHECKING, Any, TypeVar +from time import sleep import requests from pydantic import BaseModel @@ -77,7 +78,7 @@ def get_url(self, path: str, url_params: dict = None) -> str: return url def _do_request( # noqa: PLR0913 - self, method, url, binary=False, data=None, headers=None, files=None + self, method, url, binary=False, data=None, headers=None, files=None, retry=True ) -> tuple[int, dict[str, Any] | requests.Response | None]: """ Helper method that performs an actual call against the API, @@ -118,10 +119,18 @@ 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 retry: + self.logger.warning("Sleeping %d seconds", retry_after) + sleep(retry_after) + if files: + for k,v in files: + v.seek(0) + return self._do_request(method, url, binary, data, headers, files, retry=False) + 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") From ef19a91ecd1e773763536bb52b8c798f7533f464 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 14 Feb 2024 12:29:51 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- easyverein/core/client.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/easyverein/core/client.py b/easyverein/core/client.py index 4f143e0..8e019da 100644 --- a/easyverein/core/client.py +++ b/easyverein/core/client.py @@ -5,8 +5,8 @@ import logging from pathlib import Path -from typing import TYPE_CHECKING, Any, TypeVar from time import sleep +from typing import TYPE_CHECKING, Any, TypeVar import requests from pydantic import BaseModel @@ -123,9 +123,11 @@ def _do_request( # noqa: PLR0913 self.logger.warning("Sleeping %d seconds", retry_after) sleep(retry_after) if files: - for k,v in files: + for k, v in files: v.seek(0) - return self._do_request(method, url, binary, data, headers, files, retry=False) + return self._do_request( + method, url, binary, data, headers, files, retry=False + ) else: raise EasyvereinAPITooManyRetriesException( f"Too many requests, please wait {retry_after} seconds and try again.", From 00f7209d5141d2660d16cf74c3cdfc071ebf1b8d Mon Sep 17 00:00:00 2001 From: codemonk Date: Wed, 14 Feb 2024 16:17:41 +0100 Subject: [PATCH 3/5] add auto_retry feature flag --- easyverein/api.py | 3 ++- easyverein/core/client.py | 16 ++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/easyverein/api.py b/easyverein/api.py index 266f7af..3172b4d 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,7 @@ 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 8e019da..1aacca9 100644 --- a/easyverein/core/client.py +++ b/easyverein/core/client.py @@ -36,6 +36,7 @@ def __init__( base_url, logger: logging.Logger, instance: EasyvereinAPI, + auto_retry=False, ): """ Constructor setting API key and logger @@ -45,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): """ @@ -78,7 +80,7 @@ def get_url(self, path: str, url_params: dict = None) -> str: return url def _do_request( # noqa: PLR0913 - self, method, url, binary=False, data=None, headers=None, files=None, retry=True + self, method, url, binary=False, data=None, headers=None, files=None ) -> tuple[int, dict[str, Any] | requests.Response | None]: """ Helper method that performs an actual call against the API, @@ -119,15 +121,13 @@ def _do_request( # noqa: PLR0913 "Request returned status code 429, too many requests. Wait %d seconds", retry_after, ) - if retry: - self.logger.warning("Sleeping %d seconds", retry_after) + if self.auto_retry: + self.logger.warning("Retrying after %d seconds sleep.", retry_after) sleep(retry_after) if files: - for k, v in files: - v.seek(0) - return self._do_request( - method, url, binary, data, headers, files, retry=False - ) + for k,v in files: + 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.", From d7b9bebae8fa667166b5004fb75dfaabb4d7c01c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:17:53 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- easyverein/api.py | 4 +++- easyverein/core/client.py | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/easyverein/api.py b/easyverein/api.py index 3172b4d..5e1a423 100644 --- a/easyverein/api.py +++ b/easyverein/api.py @@ -32,7 +32,9 @@ def __init__( else: self.logger = logging.getLogger("easyverein") - self.c = EasyvereinClient(api_key, api_version, base_url, self.logger, self, auto_retry) + 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 1aacca9..a275008 100644 --- a/easyverein/core/client.py +++ b/easyverein/core/client.py @@ -125,8 +125,10 @@ def _do_request( # noqa: PLR0913 self.logger.warning("Retrying after %d seconds sleep.", retry_after) sleep(retry_after) if files: - for k,v in files: - v.seek(0) # reset file seek, as it has been moved by the previous call + for k, v in files: + 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( From 575f1486b3c36a39fc7fe0f29c75ca0d434fd524 Mon Sep 17 00:00:00 2001 From: codemonk Date: Sat, 17 Feb 2024 15:50:52 +0100 Subject: [PATCH 5/5] fix file seek reset on auto_retry --- easyverein/core/client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/easyverein/core/client.py b/easyverein/core/client.py index a275008..371e9c5 100644 --- a/easyverein/core/client.py +++ b/easyverein/core/client.py @@ -125,10 +125,11 @@ def _do_request( # noqa: PLR0913 self.logger.warning("Retrying after %d seconds sleep.", retry_after) sleep(retry_after) if files: - for k, v in 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(