1616from contextlib import asynccontextmanager
1717import functools
1818import time
19- from typing import Mapping , Optional
19+ import typing
20+ from typing import Any , Mapping , Optional , Union
2021
2122from google .auth import _exponential_backoff , exceptions
2223from google .auth .aio import transport
3031except ImportError : # pragma: NO COVER
3132 AIOHTTP_INSTALLED = False
3233
33-
34+ if typing .TYPE_CHECKING :
35+ from aiohttp import ClientTimeout
36+ else :
37+ ClientTimeout : typing .Type = Any
38+ try :
39+ from aiohttp import ClientTimeout
40+ except ImportError :
41+ ClientTimeout = None
42+
43+
3444@asynccontextmanager
3545async def timeout_guard (timeout ):
3646 """
@@ -137,7 +147,8 @@ async def request(
137147 data : Optional [bytes ] = None ,
138148 headers : Optional [Mapping [str , str ]] = None ,
139149 max_allowed_time : float = transport ._DEFAULT_TIMEOUT_SECONDS ,
140- timeout : float = transport ._DEFAULT_TIMEOUT_SECONDS ,
150+ timeout : Union [float , ClientTimeout ] = transport ._DEFAULT_TIMEOUT_SECONDS ,
151+ total_attempts : Optional [int ] = transport .DEFAULT_MAX_RETRY_ATTEMPTS ,
141152 ** kwargs ,
142153 ) -> transport .Response :
143154 """
@@ -146,14 +157,16 @@ async def request(
146157 url (str): The URI to be requested.
147158 data (Optional[bytes]): The payload or body in HTTP request.
148159 headers (Optional[Mapping[str, str]]): Request headers.
149- timeout (float):
160+ timeout (float, aiohttp.ClientTimeout ):
150161 The amount of time in seconds to wait for the server response
151162 with each individual request.
152163 max_allowed_time (float):
153164 If the method runs longer than this, a ``Timeout`` exception is
154165 automatically raised. Unlike the ``timeout`` parameter, this
155166 value applies to the total method execution time, even if
156167 multiple requests are made under the hood.
168+ total_attempts (int):
169+ The total number of retry attempts.
157170
158171 Mind that it is not guaranteed that the timeout error is raised
159172 at ``max_allowed_time``. It might take longer, for example, if
@@ -172,7 +185,7 @@ async def request(
172185 """
173186
174187 retries = _exponential_backoff .AsyncExponentialBackoff (
175- total_attempts = transport . DEFAULT_MAX_RETRY_ATTEMPTS
188+ total_attempts = total_attempts ,
176189 )
177190 async with timeout_guard (max_allowed_time ) as with_timeout :
178191 await with_timeout (
@@ -198,7 +211,7 @@ async def get(
198211 data : Optional [bytes ] = None ,
199212 headers : Optional [Mapping [str , str ]] = None ,
200213 max_allowed_time : float = transport ._DEFAULT_TIMEOUT_SECONDS ,
201- timeout : float = transport ._DEFAULT_TIMEOUT_SECONDS ,
214+ timeout : Union [ float , ClientTimeout ] = transport ._DEFAULT_TIMEOUT_SECONDS ,
202215 ** kwargs ,
203216 ) -> transport .Response :
204217 return await self .request (
@@ -212,7 +225,7 @@ async def post(
212225 data : Optional [bytes ] = None ,
213226 headers : Optional [Mapping [str , str ]] = None ,
214227 max_allowed_time : float = transport ._DEFAULT_TIMEOUT_SECONDS ,
215- timeout : float = transport ._DEFAULT_TIMEOUT_SECONDS ,
228+ timeout : Union [ float , ClientTimeout ] = transport ._DEFAULT_TIMEOUT_SECONDS ,
216229 ** kwargs ,
217230 ) -> transport .Response :
218231 return await self .request (
@@ -226,7 +239,7 @@ async def put(
226239 data : Optional [bytes ] = None ,
227240 headers : Optional [Mapping [str , str ]] = None ,
228241 max_allowed_time : float = transport ._DEFAULT_TIMEOUT_SECONDS ,
229- timeout : float = transport ._DEFAULT_TIMEOUT_SECONDS ,
242+ timeout : Union [ float , ClientTimeout ] = transport ._DEFAULT_TIMEOUT_SECONDS ,
230243 ** kwargs ,
231244 ) -> transport .Response :
232245 return await self .request (
@@ -240,7 +253,7 @@ async def patch(
240253 data : Optional [bytes ] = None ,
241254 headers : Optional [Mapping [str , str ]] = None ,
242255 max_allowed_time : float = transport ._DEFAULT_TIMEOUT_SECONDS ,
243- timeout : float = transport ._DEFAULT_TIMEOUT_SECONDS ,
256+ timeout : Union [ float , ClientTimeout ] = transport ._DEFAULT_TIMEOUT_SECONDS ,
244257 ** kwargs ,
245258 ) -> transport .Response :
246259 return await self .request (
0 commit comments