Skip to content

Commit 3e75427

Browse files
authored
Merge pull request #13 from Flared/mahinse/support_python3_9
Added urllib3 < 2.X compatibility for the Retry object
2 parents f33d84d + a17de78 commit 3e75427

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

.github/workflows/tests.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ jobs:
1212
'3.11',
1313
'3.12',
1414
]
15-
name: Python ${{ matrix.python-version }}
15+
include:
16+
- python-version: '3.9'
17+
post-venv: 'venv/bin/pip install "urllib3<2" "requests<=2.30" "types-requests<=2.30"'
18+
name: Python ${{ matrix.python-version }} ${{ matrix.post-venv }}
1619
steps:
1720
- uses: actions/checkout@v4
1821
- uses: actions/setup-python@v5
1922
with:
2023
python-version: ${{ matrix.python-version }}
2124
architecture: x64
25+
- run: make venv
26+
- if: ${{ matrix.post-venv }}
27+
run: ${{ matrix.post-venv }}
2228
- run: make test
2329
- run: make lint

flareio/api_client.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,26 @@ def _create_session() -> requests.Session:
5050
# Enable retries
5151
session.mount(
5252
"https://",
53-
HTTPAdapter(
54-
max_retries=Retry(
55-
total=5,
56-
backoff_factor=2,
57-
status_forcelist=[429, 502, 503, 504],
58-
allowed_methods={"GET", "POST"},
59-
backoff_max=15,
60-
)
61-
),
53+
HTTPAdapter(max_retries=FlareApiClient._create_retry()),
6254
)
6355

6456
return session
6557

58+
@staticmethod
59+
def _create_retry() -> Retry:
60+
retry = Retry(
61+
total=5,
62+
backoff_factor=2,
63+
status_forcelist=[429, 502, 503, 504],
64+
allowed_methods={"GET", "POST"},
65+
)
66+
67+
# Support for urllib3 < 2.X
68+
if hasattr(retry, "backoff_max"):
69+
retry.backoff_max = 15
70+
71+
return retry
72+
6673
def generate_token(self) -> str:
6774
payload: t.Optional[dict] = None
6875

tests/test_api_client_creation.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
from .utils import get_test_client
55
from datetime import datetime
6+
from packaging.version import Version
7+
from urllib3 import __version__ as urllib_version
68

79
from flareio import FlareApiClient
810
from flareio.exceptions import TokenError
@@ -62,3 +64,10 @@ def test_generate_token_error() -> None:
6264

6365
with pytest.raises(TokenError):
6466
client.generate_token()
67+
68+
69+
def test_backoff_max() -> None:
70+
if Version(urllib_version) >= Version("2.0.0"):
71+
retry = FlareApiClient._create_retry()
72+
assert hasattr(retry, "backoff_max")
73+
assert retry.backoff_max == 15

0 commit comments

Comments
 (0)