|
16 | 16 | from contextlib import asynccontextmanager |
17 | 17 | import functools |
18 | 18 | import time |
19 | | -import typing |
20 | 19 | from typing import Mapping, Optional, TYPE_CHECKING, Union |
21 | 20 |
|
22 | 21 | from google.auth import _exponential_backoff, exceptions |
|
27 | 26 | import google.auth.transport._mtls_helper |
28 | 27 |
|
29 | 28 | if TYPE_CHECKING: # pragma: NO COVER |
| 29 | + import aiohttp |
30 | 30 | from aiohttp import ClientTimeout # type: ignore |
31 | 31 |
|
32 | 32 | else: |
33 | 33 | try: |
| 34 | + import aiohttp |
34 | 35 | from aiohttp import ClientTimeout |
35 | 36 | except (ImportError, AttributeError): |
36 | 37 | ClientTimeout = None |
37 | 38 |
|
38 | 39 | try: |
39 | | - import aiohttp |
40 | 40 | from google.auth.aio.transport.aiohttp import Request as AiohttpRequest |
41 | 41 |
|
42 | 42 | AIOHTTP_INSTALLED = True |
@@ -154,7 +154,7 @@ async def configure_mtls_channel(self, client_cert_callback=None): |
154 | 154 | default SSL credentials), the underlying transport will be reconfigured |
155 | 155 | to use mTLS. |
156 | 156 | Note: This function does nothing if the `aiohttp` library is not |
157 | | - installed. |
| 157 | + installed. Plus, will close any ongoing API requests. |
158 | 158 |
|
159 | 159 | Args: |
160 | 160 | client_cert_callback (Optional[Callable[[], (bytes, bytes)]]): |
@@ -260,13 +260,13 @@ async def request( |
260 | 260 | self._auth_request, method, url, headers |
261 | 261 | ) |
262 | 262 | ) |
263 | | - if AIOHTTP_INSTALLED and hasattr(timeout, "total"): |
264 | | - # If it's a ClientTimeout, extract the total float |
265 | | - actual_timeout = typing.cast(float, timeout.total) |
266 | | - elif timeout is None: |
267 | | - actual_timeout = 0.0 |
| 263 | + actual_timeout: float = 0.0 |
| 264 | + if AIOHTTP_INSTALLED and isinstance(timeout, aiohttp.ClientTimeout): |
| 265 | + actual_timeout = timeout.total if timeout.total is not None else 0.0 |
| 266 | + elif isinstance(timeout, (int, float)): |
| 267 | + actual_timeout = float(timeout) |
268 | 268 | else: |
269 | | - actual_timeout = typing.cast(float, timeout) |
| 269 | + actual_timeout = 0.0 |
270 | 270 | # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch` |
271 | 271 | # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372 |
272 | 272 | async for _ in retries: # pragma: no branch |
|
0 commit comments