From cb64bc47330a2db35935bf1f6142b39df6160cb3 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 | 21 ++++++++++++--------- poetry.toml | 3 +++ 3 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 poetry.toml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 607ed6c..ff674c6 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"' + 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..263d994 100644 --- a/flareio/api_client.py +++ b/flareio/api_client.py @@ -47,18 +47,21 @@ def _create_session() -> requests.Session: ), ) + 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 + # 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=retry), ) return session diff --git a/poetry.toml b/poetry.toml new file mode 100644 index 0000000..7c2fca5 --- /dev/null +++ b/poetry.toml @@ -0,0 +1,3 @@ +[virtualenvs] +path = ".venv" +in-project = true \ No newline at end of file