From 6576545ac4ccf8da46528966c1b8d4d6f7acae4a Mon Sep 17 00:00:00 2001 From: IgorChvyrov-sm Date: Mon, 14 Jul 2025 10:37:30 +0200 Subject: [PATCH 1/2] Refactor api_client: Fix typo, add ivelid token exception handling --- src/conductor/client/http/api_client.py | 26 +++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/conductor/client/http/api_client.py b/src/conductor/client/http/api_client.py index 1622e8054..3d38defa1 100644 --- a/src/conductor/client/http/api_client.py +++ b/src/conductor/client/http/api_client.py @@ -75,7 +75,7 @@ def __call_api( ) except AuthorizationException as ae: if ae.token_expired: - logger.error( + logger.warning( f'authentication token has expired, refreshing the token. request= {method} {resource_path}') # if the token has expired, lets refresh the token self.__force_refresh_auth_token() @@ -87,6 +87,19 @@ def __call_api( _return_http_data_only=_return_http_data_only, collection_formats=collection_formats, _preload_content=_preload_content, _request_timeout=_request_timeout ) + elif ae.invalid_token: + logger.warning( + f'authentication token is invalid, refreshing the token. request= {method} {resource_path}') + # if the token is invalid, lets refresh the token + self.__force_refresh_auth_token() + # and now retry the same request + return self.__call_api_no_retry( + resource_path=resource_path, method=method, path_params=path_params, + query_params=query_params, header_params=header_params, body=body, post_params=post_params, + files=files, response_type=response_type, auth_settings=auth_settings, + _return_http_data_only=_return_http_data_only, collection_formats=collection_formats, + _preload_content=_preload_content, _request_timeout=_request_timeout + ) raise ae def __call_api_no_retry( @@ -266,7 +279,7 @@ def __deserialize(self, data, klass): if data is None: return None - if type(klass) == str: + if isinstance(klass, str): if klass.startswith('list['): sub_kls = re.match(r'list\[(.*)\]', klass).group(1) return [self.__deserialize(sub_data, sub_kls) @@ -290,7 +303,7 @@ def __deserialize(self, data, klass): if klass in self.PRIMITIVE_TYPES: return self.__deserialize_primitive(data, klass) - elif klass == object: + elif klass is object: return self.__deserialize_object(data) elif klass == datetime.date: return self.__deserialize_date(data) @@ -567,7 +580,7 @@ def __deserialize_primitive(self, data, klass): :return: int, long, float, str, bool. """ try: - if klass == str and type(data) == bytes: + if klass is str and isinstance(data, bytes): return self.__deserialize_bytes_to_str(data) return klass(data) except UnicodeEncodeError: @@ -669,7 +682,7 @@ def __get_authentication_headers(self): if time_since_last_update > self.configuration.auth_token_ttl_msec: # time to refresh the token - logger.debug(f'refreshing authentication token') + logger.debug('refreshing authentication token') token = self.__get_new_token() self.configuration.update_token(token) @@ -699,9 +712,10 @@ def __force_refresh_auth_token(self) -> None: def __get_new_token(self) -> str: try: if self.configuration.authentication_settings.key_id is None or self.configuration.authentication_settings.key_secret is None: - logger.error('Authentication Key or Secret is set. Failed to get the auth token') + logger.error('Authentication Key or Secret is not set. Failed to get the auth token') return None + logger.debug('Requesting new authentication token from server') response = self.call_api( '/token', 'POST', header_params={ From a34eac48629c08f3ec49d8c4f571932fb515142f Mon Sep 17 00:00:00 2001 From: IgorChvyrov-sm Date: Mon, 14 Jul 2025 11:17:49 +0200 Subject: [PATCH 2/2] Refactor api_client AuthorizationException handling --- src/conductor/client/http/api_client.py | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/conductor/client/http/api_client.py b/src/conductor/client/http/api_client.py index 3d38defa1..5b6413752 100644 --- a/src/conductor/client/http/api_client.py +++ b/src/conductor/client/http/api_client.py @@ -74,10 +74,11 @@ def __call_api( _preload_content=_preload_content, _request_timeout=_request_timeout ) except AuthorizationException as ae: - if ae.token_expired: + if ae.token_expired or ae.invalid_token: + token_status = "expired" if ae.token_expired else "invalid" logger.warning( - f'authentication token has expired, refreshing the token. request= {method} {resource_path}') - # if the token has expired, lets refresh the token + f'authentication token is {token_status}, refreshing the token. request= {method} {resource_path}') + # if the token has expired or is invalid, lets refresh the token self.__force_refresh_auth_token() # and now retry the same request return self.__call_api_no_retry( @@ -87,20 +88,7 @@ def __call_api( _return_http_data_only=_return_http_data_only, collection_formats=collection_formats, _preload_content=_preload_content, _request_timeout=_request_timeout ) - elif ae.invalid_token: - logger.warning( - f'authentication token is invalid, refreshing the token. request= {method} {resource_path}') - # if the token is invalid, lets refresh the token - self.__force_refresh_auth_token() - # and now retry the same request - return self.__call_api_no_retry( - resource_path=resource_path, method=method, path_params=path_params, - query_params=query_params, header_params=header_params, body=body, post_params=post_params, - files=files, response_type=response_type, auth_settings=auth_settings, - _return_http_data_only=_return_http_data_only, collection_formats=collection_formats, - _preload_content=_preload_content, _request_timeout=_request_timeout - ) - raise ae + raise ae def __call_api_no_retry( self, resource_path, method, path_params=None,