Skip to content

Commit

Permalink
implement retries
Browse files Browse the repository at this point in the history
  • Loading branch information
aviau committed Aug 19, 2024
1 parent dc48619 commit 00d2b73
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
22 changes: 21 additions & 1 deletion flareio/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from datetime import datetime
from datetime import timedelta
from flareio.exceptions import TokenError
from requests.adapters import HTTPAdapter
from urllib.parse import urljoin
from urllib.parse import urlparse
from urllib3.util import Retry

import typing as t

Expand All @@ -24,7 +26,25 @@ def __init__(

self._api_token: t.Optional[str] = None
self._api_token_exp: t.Optional[datetime] = None
self._session = session or requests.Session()
self._session = session or self._create_session()

@staticmethod
def _create_session() -> requests.Session:
session = requests.Session()
retries = Retry(
total=5,
backoff_factor=1,
status_forcelist=[502, 503, 504, 429],
allowed_methods={"GET", "POST"},
backoff_max=10,
)
session.mount(
"https://",
HTTPAdapter(
max_retries=retries,
),
)
return session

def generate_token(self) -> str:
payload: t.Optional[dict] = None
Expand Down
20 changes: 20 additions & 0 deletions tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,23 @@ def test_scroll() -> None:
with pytest.raises(StopIteration):
resp = next(response_iterator)
assert len(mocker.request_history) == 0


def test_retry() -> None:
client = _get_test_client()
with requests_mock.Mocker() as mocker:
mocker.register_uri(
"GET",
"https://api.flare.io/hello/test",
[
{
"status_code": 429,
},
{
"status_code": 200,
},
],
)
client.get("/hello/test")
assert len(mocker.request_history) == 2
assert mocker.last_request.url == "https://api.flare.io/hello/test"

0 comments on commit 00d2b73

Please sign in to comment.