Skip to content

Commit 185b381

Browse files
committed
feat(python): enhance retry configuration in REST client
Updated the retry parameter in the Configuration class to support different types based on the library used (urllib3 or asyncio). Adjusted the RESTClientObject to handle the new retry configuration, allowing for more flexible retry options. This change improves the handling of retries in API requests, ensuring compatibility with various retry strategies.
1 parent 8b6df51 commit 185b381

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,20 @@ class RESTClientObject:
6868
self.proxy = configuration.proxy
6969
self.proxy_headers = configuration.proxy_headers
7070

71-
self.retries = configuration.retries
71+
retries = configuration.retries
72+
if retries is None:
73+
self._effective_retry_options = None
74+
elif isinstance(retries, aiohttp_retry.RetryOptionsBase):
75+
self._effective_retry_options = retries
76+
elif isinstance(retries, int):
77+
self._effective_retry_options = aiohttp_retry.ExponentialRetry(
78+
attempts=retries,
79+
factor=2.0,
80+
start_timeout=0.1,
81+
max_timeout=120.0
82+
)
83+
else:
84+
self._effective_retry_options = None
7285

7386
self.pool_manager: Optional[aiohttp.ClientSession] = None
7487
self.retry_client: Optional[aiohttp_retry.RetryClient] = None
@@ -191,16 +204,11 @@ class RESTClientObject:
191204
)
192205
pool_manager = self.pool_manager
193206

194-
if self.retries is not None and method in ALLOW_RETRY_METHODS:
207+
if self._effective_retry_options is not None and method in ALLOW_RETRY_METHODS:
195208
if self.retry_client is None:
196209
self.retry_client = aiohttp_retry.RetryClient(
197210
client_session=self.pool_manager,
198-
retry_options=aiohttp_retry.ExponentialRetry(
199-
attempts=self.retries,
200-
factor=2.0,
201-
start_timeout=0.1,
202-
max_timeout=120.0
203-
)
211+
retry_options=self._effective_retry_options
204212
)
205213
pool_manager = self.retry_client
206214

modules/openapi-generator/src/main/resources/python/configuration.mustache

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ class Configuration:
188188
values before.
189189
:param ssl_ca_cert: str - the path to a file of concatenated CA certificates
190190
in PEM format.
191-
:param retries: Number of retries for API requests.
191+
:param retries: Retry config; type depends on library:
192+
* urllib3: int | urllib3.util.retry.Retry
193+
* asyncio: int | aiohttp_retry.RetryOptionsBase
192194
:param ca_cert_data: verify the peer using concatenated CA certificate data
193195
in PEM (str) or DER (bytes) format.
194196
:param cert_file: the path to a client certificate file, for mTLS.
@@ -298,7 +300,7 @@ conf = {{{packageName}}}.Configuration(
298300
server_operation_variables: Optional[Dict[int, ServerVariablesT]]=None,
299301
ignore_operation_servers: bool=False,
300302
ssl_ca_cert: Optional[str]=None,
301-
retries: Optional[int] = None,
303+
retries: Optional[Union[int, Any]] = None,
302304
ca_cert_data: Optional[Union[str, bytes]] = None,
303305
cert_file: Optional[str]=None,
304306
key_file: Optional[str]=None,
@@ -432,7 +434,7 @@ conf = {{{packageName}}}.Configuration(
432434
"""Safe chars for path_param
433435
"""
434436
self.retries = retries
435-
"""Adding retries to override urllib3 default value 3
437+
"""Retry configuration
436438
"""
437439
# Enable client side validation
438440
self.client_side_validation = True

0 commit comments

Comments
 (0)