Skip to content

Commit 2b6b3e5

Browse files
committed
PR #770 openeo.utils.http finetuning (#441, #764)
1 parent d262382 commit 2b6b3e5

File tree

6 files changed

+26
-29
lines changed

6 files changed

+26
-29
lines changed

openeo/extra/job_management/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
import shapely.errors
3030
import shapely.geometry.base
3131
import shapely.wkt
32-
from requests.adapters import HTTPAdapter, Retry
32+
from requests.adapters import HTTPAdapter
33+
from urllib3.util import Retry
3334

3435
from openeo import BatchJob, Connection
3536
from openeo.internal.processes.parse import (

openeo/rest/_connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Iterable, Optional, Union
66

77
import requests
8-
import requests.adapters
8+
import urllib3.util
99
from requests import Response
1010
from requests.auth import AuthBase
1111

@@ -33,7 +33,7 @@ def __init__(
3333
session: Optional[requests.Session] = None,
3434
default_timeout: Optional[int] = None,
3535
slow_response_threshold: Optional[float] = None,
36-
retry: Union[requests.adapters.Retry, dict, bool, None] = None,
36+
retry: Union[urllib3.util.Retry, dict, bool, None] = None,
3737
):
3838
self._root_url = root_url
3939
self._auth = None

openeo/rest/connection.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
)
2929

3030
import requests
31-
import requests.adapters
3231
import shapely.geometry.base
32+
import urllib3.util
3333
from requests.auth import AuthBase, HTTPBasicAuth
3434

3535
import openeo
@@ -117,7 +117,7 @@ class Connection(RestApiConnection):
117117
Usage of this parameter is deprecated, use the specific authentication methods instead.
118118
:param retry: general request retry settings, can be specified as:
119119
120-
- :py:class:`requests.adapters.Retry` object
120+
- :py:class:`urllib3.util.Retry` object
121121
or a dictionary with corresponding keyword arguments
122122
(e.g. ``total``, ``backoff_factor``, ``status_forcelist``, ...)
123123
- ``None`` (default) to use default openEO-oriented retry settings
@@ -141,7 +141,7 @@ def __init__(
141141
refresh_token_store: Optional[RefreshTokenStore] = None,
142142
oidc_auth_renewer: Optional[OidcAuthenticator] = None,
143143
auth: Optional[AuthBase] = None,
144-
retry: Union[requests.adapters.Retry, dict, bool, None] = None,
144+
retry: Union[urllib3.util.Retry, dict, bool, None] = None,
145145
):
146146
if "://" not in url:
147147
url = "https://" + url
@@ -1898,7 +1898,7 @@ def connect(
18981898
session: Optional[requests.Session] = None,
18991899
default_timeout: Optional[int] = None,
19001900
auto_validate: bool = True,
1901-
retry: Union[requests.adapters.Retry, dict, bool, None] = None,
1901+
retry: Union[urllib3.util.Retry, dict, bool, None] = None,
19021902
) -> Connection:
19031903
"""
19041904
This method is the entry point to OpenEO.
@@ -1920,7 +1920,7 @@ def connect(
19201920
:param auto_validate: toggle to automatically validate process graphs before execution
19211921
:param retry: general request retry settings, can be specified as:
19221922
1923-
- :py:class:`requests.adapters.Retry` object
1923+
- :py:class:`urllib3.util.Retry` object
19241924
or a dictionary with corresponding keyword arguments
19251925
(e.g. ``total``, ``backoff_factor``, ``status_forcelist``, ...)
19261926
- ``None`` (default) to use default openEO-oriented retry settings

openeo/utils/http.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import requests
88
import requests.adapters
9+
from urllib3.util import Retry
910

1011
DEFAULT_RETRIES_TOTAL = 5
1112

@@ -28,29 +29,29 @@
2829
)
2930

3031

31-
def retry_adapter(
32+
def retry_configuration(
3233
*,
3334
total: int = DEFAULT_RETRIES_TOTAL,
3435
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
3536
status_forcelist: Collection[int] = DEFAULT_RETRY_FORCELIST,
3637
**kwargs,
37-
) -> requests.adapters.Retry:
38+
) -> Retry:
3839
"""
39-
Factory for creating a `requests.adapters.Retry` configuration object with
40+
Factory for creating a :py:class:`urllib3.util.retry.Retry` configuration object with
4041
openEO-oriented retry settings.
4142
4243
:param total: Total number of retries to allow
4344
:param backoff_factor: scaling factor for sleeps between retries
4445
:param status_forcelist: A set of integer HTTP status codes that we should force a retry on.
45-
:param kwargs: additional kwargs to pass to `requests.adapters.Retry`
46+
:param kwargs: additional kwargs to pass to :py:class:`urllib3.util.retry.Retry`
4647
:return:
4748
4849
Inspiration and references:
4950
- https://requests.readthedocs.io/en/latest/api/#requests.adapters.HTTPAdapter
5051
- https://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#urllib3.util.Retry
5152
- https://findwork.dev/blog/advanced-usage-python-requests-timeouts-retries-hooks/#retry-on-failure
5253
"""
53-
retry = requests.adapters.Retry(
54+
retry = Retry(
5455
total=total,
5556
backoff_factor=backoff_factor,
5657
status_forcelist=status_forcelist,
@@ -59,32 +60,28 @@ def retry_adapter(
5960
return retry
6061

6162

62-
def _to_retry(
63-
retry: Union[requests.adapters.Retry, dict, None],
64-
) -> requests.adapters.Retry:
63+
def _to_retry(retry: Union[Retry, dict, None]) -> Retry:
6564
"""
66-
Convert a retry specification to a `requests.adapters.Retry` object.
65+
Convert a retry specification to a :py:class:`urllib3.util.retry.Retry` object.
6766
"""
68-
if isinstance(retry, requests.adapters.Retry):
69-
return retry
67+
if isinstance(retry, Retry):
68+
pass
7069
elif isinstance(retry, dict):
71-
adapter = retry_adapter(**retry)
70+
retry = retry_configuration(**retry)
7271
elif retry in {None, True}:
73-
adapter = retry_adapter()
72+
retry = retry_configuration()
7473
else:
7574
raise ValueError(f"Invalid retry setting: {retry!r}")
76-
return adapter
75+
return retry
7776

7877

79-
def session_with_retries(
80-
retry: Union[requests.adapters.Retry, dict, None] = None,
81-
) -> requests.Session:
78+
def session_with_retries(retry: Union[Retry, dict, None] = None) -> requests.Session:
8279
"""
8380
Factory for a requests session with openEO-oriented retry settings.
8481
8582
:param retry: The retry configuration, can be specified as:
86-
- :py:class:`requests.adapters.Retry`
87-
- a dictionary with :py:class:`requests.adapters.Retry` arguments,
83+
- :py:class:`urllib3.util.retry.Retry`
84+
- a dictionary with :py:class:`urllib3.util.retry.Retry` arguments,
8885
e.g. ``total``, ``backoff_factor``, ``status_forcelist``, ...
8986
- ``None`` for default openEO-oriented retry settings
9087
"""

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
test_suite="tests",
7373
install_requires=[
7474
"requests>=2.26.0",
75+
"urllib3>=2.0.0",
7576
"shapely>=1.6.4",
7677
"numpy>=1.17.0",
7778
"xarray>=0.12.3,<2025.01.2", # TODO #721 xarray non-nanosecond support

tests/utils/test_http.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import contextlib
2-
import logging
31
from typing import Iterator
42
from unittest import mock
53

0 commit comments

Comments
 (0)