2929
3030import requests
3131import shapely .geometry .base
32+ import urllib3 .util
3233from requests .auth import AuthBase , HTTPBasicAuth
3334
3435import openeo
8586 load_json_resource ,
8687 rfc3339 ,
8788)
89+ from openeo .utils .http import (
90+ HTTP_201_CREATED ,
91+ HTTP_401_UNAUTHORIZED ,
92+ HTTP_403_FORBIDDEN ,
93+ )
8894from openeo .utils .version import ComparableVersion
8995
9096__all__ = ["Connection" , "connect" ]
@@ -114,6 +120,16 @@ class Connection(RestApiConnection):
114120 optional :class:`OidcAuthenticator` object to use for renewing OIDC tokens.
115121 :param auth: Optional ``requests.auth.AuthBase`` object to use for requests.
116122 Usage of this parameter is deprecated, use the specific authentication methods instead.
123+ :param retry: general request retry settings, can be specified as:
124+
125+ - :py:class:`urllib3.util.Retry` object
126+ or a dictionary with corresponding keyword arguments
127+ (e.g. ``total``, ``backoff_factor``, ``status_forcelist``, ...)
128+ - ``None`` (default) to use default openEO-oriented retry settings
129+ - ``False`` to disable retrying requests
130+
131+ .. versionchanged:: 0.41.0
132+ Added ``retry`` argument.
117133 """
118134
119135 _MINIMUM_API_VERSION = ComparableVersion ("1.0.0" )
@@ -130,6 +146,7 @@ def __init__(
130146 refresh_token_store : Optional [RefreshTokenStore ] = None ,
131147 oidc_auth_renewer : Optional [OidcAuthenticator ] = None ,
132148 auth : Optional [AuthBase ] = None ,
149+ retry : Union [urllib3 .util .Retry , dict , bool , None ] = None ,
133150 ):
134151 if "://" not in url :
135152 url = "https://" + url
@@ -139,6 +156,7 @@ def __init__(
139156 root_url = self .version_discovery (url , session = session , timeout = default_timeout ),
140157 auth = auth , session = session , default_timeout = default_timeout ,
141158 slow_response_threshold = slow_response_threshold ,
159+ retry = retry ,
142160 )
143161
144162 # Initial API version check.
@@ -663,7 +681,10 @@ def _request():
663681 # Initial request attempt
664682 return _request ()
665683 except OpenEoApiError as api_exc :
666- if api_exc .http_status_code in {401 , 403 } and api_exc .code == "TokenInvalid" :
684+ if (
685+ api_exc .http_status_code in {HTTP_401_UNAUTHORIZED , HTTP_403_FORBIDDEN }
686+ and api_exc .code == "TokenInvalid"
687+ ):
667688 # Auth token expired: can we refresh?
668689 if isinstance (self .auth , OidcBearerAuth ) and self ._oidc_auth_renewer :
669690 msg = f"OIDC access token expired ({ api_exc .http_status_code } { api_exc .code } )."
@@ -1750,7 +1771,7 @@ def create_job(
17501771 )
17511772
17521773 self ._preflight_validation (pg_with_metadata = pg_with_metadata , validate = validate )
1753- response = self .post ("/jobs" , json = pg_with_metadata , expected_status = 201 )
1774+ response = self .post ("/jobs" , json = pg_with_metadata , expected_status = HTTP_201_CREATED )
17541775
17551776 job_id = None
17561777 if "openeo-identifier" in response .headers :
@@ -1885,6 +1906,7 @@ def connect(
18851906 session : Optional [requests .Session ] = None ,
18861907 default_timeout : Optional [int ] = None ,
18871908 auto_validate : bool = True ,
1909+ retry : Union [urllib3 .util .Retry , dict , bool , None ] = None ,
18881910) -> Connection :
18891911 """
18901912 This method is the entry point to OpenEO.
@@ -1904,9 +1926,19 @@ def connect(
19041926 :param auth_options: Options/arguments specific to the authentication type
19051927 :param default_timeout: default timeout (in seconds) for requests
19061928 :param auto_validate: toggle to automatically validate process graphs before execution
1929+ :param retry: general request retry settings, can be specified as:
19071930
1908- .. versionadded:: 0.24.0
1909- added ``auto_validate`` argument
1931+ - :py:class:`urllib3.util.Retry` object
1932+ or a dictionary with corresponding keyword arguments
1933+ (e.g. ``total``, ``backoff_factor``, ``status_forcelist``, ...)
1934+ - ``None`` (default) to use default openEO-oriented retry settings
1935+ - ``False`` to disable retrying requests
1936+
1937+ .. versionchanged:: 0.24.0
1938+ Added ``auto_validate`` argument
1939+
1940+ .. versionchanged:: 0.41.0
1941+ Added ``retry`` argument.
19101942 """
19111943
19121944 def _config_log (message ):
@@ -1931,7 +1963,13 @@ def _config_log(message):
19311963
19321964 if not url :
19331965 raise OpenEoClientException ("No openEO back-end URL given or known to connect to." )
1934- connection = Connection (url , session = session , default_timeout = default_timeout , auto_validate = auto_validate )
1966+ connection = Connection (
1967+ url ,
1968+ session = session ,
1969+ default_timeout = default_timeout ,
1970+ auto_validate = auto_validate ,
1971+ retry = retry ,
1972+ )
19351973
19361974 auth_type = auth_type .lower () if isinstance (auth_type , str ) else auth_type
19371975 if auth_type in {None , False , 'null' , 'none' }:
0 commit comments