From a17de787b3d9ccd2352c5d9e358ae0a63b46e410 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Hinse Date: Mon, 21 Oct 2024 15:30:38 -0400 Subject: [PATCH] Added urllib3 < 2.X compatibility for the Retry object Added matrix to test if it also works with urllib3 <2.x --- .github/workflows/tests.yml | 8 +++++++- flareio/api_client.py | 25 ++++++++++++++++--------- tests/test_api_client_creation.py | 9 +++++++++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 607ed6c..f6a8ace 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -12,12 +12,18 @@ jobs: '3.11', '3.12', ] - name: Python ${{ matrix.python-version }} + include: + - python-version: '3.9' + post-venv: 'venv/bin/pip install "urllib3<2" "requests<=2.30" "types-requests<=2.30"' + name: Python ${{ matrix.python-version }} ${{ matrix.post-venv }} steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} architecture: x64 + - run: make venv + - if: ${{ matrix.post-venv }} + run: ${{ matrix.post-venv }} - run: make test - run: make lint diff --git a/flareio/api_client.py b/flareio/api_client.py index 290e099..bf4620d 100644 --- a/flareio/api_client.py +++ b/flareio/api_client.py @@ -50,19 +50,26 @@ def _create_session() -> requests.Session: # Enable retries session.mount( "https://", - HTTPAdapter( - max_retries=Retry( - total=5, - backoff_factor=2, - status_forcelist=[429, 502, 503, 504], - allowed_methods={"GET", "POST"}, - backoff_max=15, - ) - ), + HTTPAdapter(max_retries=FlareApiClient._create_retry()), ) return session + @staticmethod + def _create_retry() -> Retry: + retry = Retry( + total=5, + backoff_factor=2, + status_forcelist=[429, 502, 503, 504], + allowed_methods={"GET", "POST"}, + ) + + # Support for urllib3 < 2.X + if hasattr(retry, "backoff_max"): + retry.backoff_max = 15 + + return retry + def generate_token(self) -> str: payload: t.Optional[dict] = None diff --git a/tests/test_api_client_creation.py b/tests/test_api_client_creation.py index 5b6ad2b..39f69b1 100644 --- a/tests/test_api_client_creation.py +++ b/tests/test_api_client_creation.py @@ -3,6 +3,8 @@ from .utils import get_test_client from datetime import datetime +from packaging.version import Version +from urllib3 import __version__ as urllib_version from flareio import FlareApiClient from flareio.exceptions import TokenError @@ -62,3 +64,10 @@ def test_generate_token_error() -> None: with pytest.raises(TokenError): client.generate_token() + + +def test_backoff_max() -> None: + if Version(urllib_version) >= Version("2.0.0"): + retry = FlareApiClient._create_retry() + assert hasattr(retry, "backoff_max") + assert retry.backoff_max == 15