From 185b381f996b2220c1c8718aeafb49a85d0ad27a Mon Sep 17 00:00:00 2001 From: timonrieger Date: Sat, 31 Jan 2026 20:44:35 +0100 Subject: [PATCH] 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. --- .../resources/python/asyncio/rest.mustache | 24 ++++++++++++------- .../resources/python/configuration.mustache | 8 ++++--- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache b/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache index 9d9fc9682fb6..474413e955e6 100644 --- a/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache @@ -68,7 +68,20 @@ class RESTClientObject: self.proxy = configuration.proxy self.proxy_headers = configuration.proxy_headers - self.retries = configuration.retries + retries = configuration.retries + if retries is None: + self._effective_retry_options = None + elif isinstance(retries, aiohttp_retry.RetryOptionsBase): + self._effective_retry_options = retries + elif isinstance(retries, int): + self._effective_retry_options = aiohttp_retry.ExponentialRetry( + attempts=retries, + factor=2.0, + start_timeout=0.1, + max_timeout=120.0 + ) + else: + self._effective_retry_options = None self.pool_manager: Optional[aiohttp.ClientSession] = None self.retry_client: Optional[aiohttp_retry.RetryClient] = None @@ -191,16 +204,11 @@ class RESTClientObject: ) pool_manager = self.pool_manager - if self.retries is not None and method in ALLOW_RETRY_METHODS: + if self._effective_retry_options is not None and method in ALLOW_RETRY_METHODS: if self.retry_client is None: self.retry_client = aiohttp_retry.RetryClient( client_session=self.pool_manager, - retry_options=aiohttp_retry.ExponentialRetry( - attempts=self.retries, - factor=2.0, - start_timeout=0.1, - max_timeout=120.0 - ) + retry_options=self._effective_retry_options ) pool_manager = self.retry_client diff --git a/modules/openapi-generator/src/main/resources/python/configuration.mustache b/modules/openapi-generator/src/main/resources/python/configuration.mustache index 039b2a85519f..f7602ed100a8 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -188,7 +188,9 @@ class Configuration: values before. :param ssl_ca_cert: str - the path to a file of concatenated CA certificates in PEM format. - :param retries: Number of retries for API requests. + :param retries: Retry config; type depends on library: + * urllib3: int | urllib3.util.retry.Retry + * asyncio: int | aiohttp_retry.RetryOptionsBase :param ca_cert_data: verify the peer using concatenated CA certificate data in PEM (str) or DER (bytes) format. :param cert_file: the path to a client certificate file, for mTLS. @@ -298,7 +300,7 @@ conf = {{{packageName}}}.Configuration( server_operation_variables: Optional[Dict[int, ServerVariablesT]]=None, ignore_operation_servers: bool=False, ssl_ca_cert: Optional[str]=None, - retries: Optional[int] = None, + retries: Optional[Union[int, Any]] = None, ca_cert_data: Optional[Union[str, bytes]] = None, cert_file: Optional[str]=None, key_file: Optional[str]=None, @@ -432,7 +434,7 @@ conf = {{{packageName}}}.Configuration( """Safe chars for path_param """ self.retries = retries - """Adding retries to override urllib3 default value 3 + """Retry configuration """ # Enable client side validation self.client_side_validation = True