Skip to content

Commit

Permalink
Merge pull request #7 from cod3monk/feature/retry
Browse files Browse the repository at this point in the history
Retry on EasyvereinAPITooManyRetriesException
  • Loading branch information
waza-ari authored Apr 27, 2024
2 parents 1abb222 + 575f148 commit c79284a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
5 changes: 4 additions & 1 deletion easyverein/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
22 changes: 18 additions & 4 deletions easyverein/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import logging
from pathlib import Path
from time import sleep
from typing import TYPE_CHECKING, Any, TypeVar

import requests
Expand Down Expand Up @@ -35,6 +36,7 @@ def __init__(
base_url,
logger: logging.Logger,
instance: EasyvereinAPI,
auto_retry=False,
):
"""
Constructor setting API key and logger
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit c79284a

Please sign in to comment.