Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry on EasyvereinAPITooManyRetriesException #7

Merged
merged 5 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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