1313# limitations under the License.
1414
1515import asyncio
16+ import collections .abc
1617from contextlib import asynccontextmanager
1718import functools
1819import http .client as http_client
@@ -321,11 +322,17 @@ async def request(
321322
322323 if response .status_code not in transport .DEFAULT_RETRYABLE_STATUS_CODES :
323324 break
324-
325+
325326 if response .status_code == http_client .UNAUTHORIZED :
326327 _auth_retry_count = kwargs .pop ("_auth_retry_count" , 0 )
327328 if _auth_retry_count < 2 :
328- is_streaming = data is not None and isinstance (data , (collections .abc .Iterator , collections .abc .AsyncIterable )) or hasattr (data , "read" )
329+ is_streaming = (
330+ data is not None
331+ and isinstance (
332+ data , (collections .abc .Iterator , collections .abc .AsyncIterable )
333+ )
334+ or hasattr (data , "read" )
335+ )
329336 if getattr (self , "is_mtls" , False ) and any (
330337 prefix in url for prefix in MTLS_URL_PREFIXES
331338 ):
@@ -335,7 +342,7 @@ async def request(
335342
336343 # Wait in line to acquire the lock
337344 async with self ._mtls_rotation_lock :
338- # Check Did another coroutine already reconfigure mTLS
345+ # Check Did another coroutine already reconfigure mTLS
339346 if self ._cached_cert != stale_cert :
340347 # Yes! Another request already updated the channel
341348 pass
@@ -350,19 +357,30 @@ async def request(
350357 google .auth .transport ._mtls_helper .check_parameters_for_unauthorized_response ,
351358 self ._cached_cert ,
352359 )
360+ except Exception as e :
361+ _LOGGER .warning (
362+ "Failed to check client certificate parameters: %s. Proceeding with original response." ,
363+ e ,
364+ )
365+ else :
353366 if cached_fingerprint != current_cert_fingerprint :
354367 try :
355368 _LOGGER .info (
356369 "Client certificate has changed, reconfiguring mTLS "
357370 "channel."
358371 )
359- if self ._mtls_init_task and self ._mtls_init_task .done ():
372+ if (
373+ self ._mtls_init_task
374+ and self ._mtls_init_task .done ()
375+ ):
360376 self ._mtls_init_task = None
361377 await self .configure_mtls_channel (
362378 lambda : (call_cert_bytes , call_key_bytes )
363379 )
364380 except Exception as e :
365- _LOGGER .error ("Failed to reconfigure mTLS channel: %s" , e )
381+ _LOGGER .error (
382+ "Failed to reconfigure mTLS channel: %s" , e
383+ )
366384 raise exceptions .MutualTLSChannelError (
367385 "Failed to reconfigure mTLS channel"
368386 ) from e
@@ -371,11 +389,6 @@ async def request(
371389 "Skipping reconfiguration of mTLS channel because the client"
372390 " certificate has not changed."
373391 )
374- except Exception as e :
375- _LOGGER .warning (
376- "Failed to check client certificate parameters: %s. Proceeding with original response." ,
377- e ,
378- )
379392 if is_streaming :
380393 return response
381394 if hasattr (response , "close" ):
@@ -393,7 +406,7 @@ async def request(
393406 max_allowed_time = max_allowed_time ,
394407 timeout = timeout ,
395408 total_attempts = total_attempts ,
396- ** kwargs
409+ ** kwargs ,
397410 )
398411 return response
399412
0 commit comments