From 2c326ed6b43abc9802c8d3ce894d8966aee48ed1 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 | 7 +++++-- Makefile | 4 ++++ flareio/api_client.py | 23 ++++++++++++++--------- pyproject.toml | 4 ++++ 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 607ed6c..892716c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -12,12 +12,15 @@ jobs: '3.11', '3.12', ] - name: Python ${{ matrix.python-version }} + include: + - python-version: '3.9' + pip-group: 'urllib3_1' + name: Python ${{ matrix.python-version }} ${{ matrix.pip-group }} steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} architecture: x64 - - run: make test + - run: make test PIP_GROUP='${{ matrix.pip-group }}' - run: make lint diff --git a/Makefile b/Makefile index ca7a773..742b005 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,11 @@ venv: pyproject.toml poetry.lock venv-tools rm -rf venv python3 -m venv venv +ifneq (${PIP_GROUP},) + VIRTUAL_ENV=venv venv-tools/bin/poetry install --with ${PIP_GROUP} +else VIRTUAL_ENV=venv venv-tools/bin/poetry install +endif venv-tools: requirements.tools.txt rm -rf venv-tools diff --git a/flareio/api_client.py b/flareio/api_client.py index 290e099..fd2f28a 100644 --- a/flareio/api_client.py +++ b/flareio/api_client.py @@ -47,18 +47,23 @@ 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 + + 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/pyproject.toml b/pyproject.toml index 6c8b62d..5b132fc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,6 +39,10 @@ build-backend = "poetry.core.masonry.api" [tool.poetry.group.dev.dependencies] pytest = "8.3.2" +[tool.poetry.group.urllib3_1.dependencies] +urllib3 = "<2" +requests = "<=2.30" + [tool.ruff.lint] extend-select = [ "I",