|
16 | 16 | from contextlib import asynccontextmanager |
17 | 17 | import functools |
18 | 18 | import time |
19 | | -from typing import Mapping, Optional |
| 19 | +from typing import Mapping, Optional, TYPE_CHECKING, Union |
20 | 20 |
|
21 | 21 | from google.auth import _exponential_backoff, exceptions |
22 | 22 | from google.auth.aio import transport |
23 | 23 | from google.auth.aio.credentials import Credentials |
24 | 24 | from google.auth.exceptions import TimeoutError |
25 | 25 |
|
| 26 | +if TYPE_CHECKING: # pragma: NO COVER |
| 27 | + from aiohttp import ClientTimeout # type: ignore |
| 28 | + |
| 29 | +else: |
| 30 | + try: |
| 31 | + from aiohttp import ClientTimeout |
| 32 | + except (ImportError, AttributeError): |
| 33 | + ClientTimeout = None |
| 34 | + |
26 | 35 | try: |
27 | 36 | from google.auth.aio.transport.aiohttp import Request as AiohttpRequest |
28 | 37 |
|
@@ -137,7 +146,8 @@ async def request( |
137 | 146 | data: Optional[bytes] = None, |
138 | 147 | headers: Optional[Mapping[str, str]] = None, |
139 | 148 | max_allowed_time: float = transport._DEFAULT_TIMEOUT_SECONDS, |
140 | | - timeout: float = transport._DEFAULT_TIMEOUT_SECONDS, |
| 149 | + timeout: Union[float, ClientTimeout] = transport._DEFAULT_TIMEOUT_SECONDS, |
| 150 | + total_attempts: Optional[int] = transport.DEFAULT_MAX_RETRY_ATTEMPTS, |
141 | 151 | **kwargs, |
142 | 152 | ) -> transport.Response: |
143 | 153 | """ |
@@ -146,14 +156,16 @@ async def request( |
146 | 156 | url (str): The URI to be requested. |
147 | 157 | data (Optional[bytes]): The payload or body in HTTP request. |
148 | 158 | headers (Optional[Mapping[str, str]]): Request headers. |
149 | | - timeout (float): |
| 159 | + timeout (float, aiohttp.ClientTimeout): |
150 | 160 | The amount of time in seconds to wait for the server response |
151 | 161 | with each individual request. |
152 | 162 | max_allowed_time (float): |
153 | 163 | If the method runs longer than this, a ``Timeout`` exception is |
154 | 164 | automatically raised. Unlike the ``timeout`` parameter, this |
155 | 165 | value applies to the total method execution time, even if |
156 | 166 | multiple requests are made under the hood. |
| 167 | + total_attempts (int): |
| 168 | + The total number of retry attempts. |
157 | 169 |
|
158 | 170 | Mind that it is not guaranteed that the timeout error is raised |
159 | 171 | at ``max_allowed_time``. It might take longer, for example, if |
@@ -172,7 +184,7 @@ async def request( |
172 | 184 | """ |
173 | 185 |
|
174 | 186 | retries = _exponential_backoff.AsyncExponentialBackoff( |
175 | | - total_attempts=transport.DEFAULT_MAX_RETRY_ATTEMPTS |
| 187 | + total_attempts=total_attempts, |
176 | 188 | ) |
177 | 189 | async with timeout_guard(max_allowed_time) as with_timeout: |
178 | 190 | await with_timeout( |
@@ -198,11 +210,50 @@ async def get( |
198 | 210 | data: Optional[bytes] = None, |
199 | 211 | headers: Optional[Mapping[str, str]] = None, |
200 | 212 | max_allowed_time: float = transport._DEFAULT_TIMEOUT_SECONDS, |
201 | | - timeout: float = transport._DEFAULT_TIMEOUT_SECONDS, |
| 213 | + timeout: Union[float, ClientTimeout] = transport._DEFAULT_TIMEOUT_SECONDS, |
| 214 | + total_attempts: Optional[int] = transport.DEFAULT_MAX_RETRY_ATTEMPTS, |
202 | 215 | **kwargs, |
203 | 216 | ) -> transport.Response: |
| 217 | + """ |
| 218 | + Args: |
| 219 | + url (str): The URI to be requested. |
| 220 | + data (Optional[bytes]): The payload or body in HTTP request. |
| 221 | + headers (Optional[Mapping[str, str]]): Request headers. |
| 222 | + max_allowed_time (float): |
| 223 | + If the method runs longer than this, a ``Timeout`` exception is |
| 224 | + automatically raised. Unlike the ``timeout`` parameter, this |
| 225 | + value applies to the total method execution time, even if |
| 226 | + multiple requests are made under the hood. |
| 227 | + timeout (float, aiohttp.ClientTimeout): |
| 228 | + The amount of time in seconds to wait for the server response |
| 229 | + with each individual request. |
| 230 | + total_attempts (int): |
| 231 | + The total number of retry attempts. |
| 232 | +
|
| 233 | + Mind that it is not guaranteed that the timeout error is raised |
| 234 | + at ``max_allowed_time``. It might take longer, for example, if |
| 235 | + an underlying request takes a lot of time, but the request |
| 236 | + itself does not timeout, e.g. if a large file is being |
| 237 | + transmitted. The timeout error will be raised after such |
| 238 | + request completes. |
| 239 | +
|
| 240 | + Returns: |
| 241 | + google.auth.aio.transport.Response: The HTTP response. |
| 242 | +
|
| 243 | + Raises: |
| 244 | + google.auth.exceptions.TimeoutError: If the method does not complete within |
| 245 | + the configured `max_allowed_time` or the request exceeds the configured |
| 246 | + `timeout`. |
| 247 | + """ |
204 | 248 | return await self.request( |
205 | | - "GET", url, data, headers, max_allowed_time, timeout, **kwargs |
| 249 | + "GET", |
| 250 | + url, |
| 251 | + data, |
| 252 | + headers, |
| 253 | + max_allowed_time, |
| 254 | + timeout, |
| 255 | + total_attempts, |
| 256 | + **kwargs, |
206 | 257 | ) |
207 | 258 |
|
208 | 259 | @functools.wraps(request) |
@@ -212,11 +263,50 @@ async def post( |
212 | 263 | data: Optional[bytes] = None, |
213 | 264 | headers: Optional[Mapping[str, str]] = None, |
214 | 265 | max_allowed_time: float = transport._DEFAULT_TIMEOUT_SECONDS, |
215 | | - timeout: float = transport._DEFAULT_TIMEOUT_SECONDS, |
| 266 | + timeout: Union[float, ClientTimeout] = transport._DEFAULT_TIMEOUT_SECONDS, |
| 267 | + total_attempts: Optional[int] = transport.DEFAULT_MAX_RETRY_ATTEMPTS, |
216 | 268 | **kwargs, |
217 | 269 | ) -> transport.Response: |
| 270 | + """ |
| 271 | + Args: |
| 272 | + url (str): The URI to be requested. |
| 273 | + data (Optional[bytes]): The payload or body in HTTP request. |
| 274 | + headers (Optional[Mapping[str, str]]): Request headers. |
| 275 | + max_allowed_time (float): |
| 276 | + If the method runs longer than this, a ``Timeout`` exception is |
| 277 | + automatically raised. Unlike the ``timeout`` parameter, this |
| 278 | + value applies to the total method execution time, even if |
| 279 | + multiple requests are made under the hood. |
| 280 | + timeout (float, aiohttp.ClientTimeout): |
| 281 | + The amount of time in seconds to wait for the server response |
| 282 | + with each individual request. |
| 283 | + total_attempts (int): |
| 284 | + The total number of retry attempts. |
| 285 | +
|
| 286 | + Mind that it is not guaranteed that the timeout error is raised |
| 287 | + at ``max_allowed_time``. It might take longer, for example, if |
| 288 | + an underlying request takes a lot of time, but the request |
| 289 | + itself does not timeout, e.g. if a large file is being |
| 290 | + transmitted. The timeout error will be raised after such |
| 291 | + request completes. |
| 292 | +
|
| 293 | + Returns: |
| 294 | + google.auth.aio.transport.Response: The HTTP response. |
| 295 | +
|
| 296 | + Raises: |
| 297 | + google.auth.exceptions.TimeoutError: If the method does not complete within |
| 298 | + the configured `max_allowed_time` or the request exceeds the configured |
| 299 | + `timeout`. |
| 300 | + """ |
218 | 301 | return await self.request( |
219 | | - "POST", url, data, headers, max_allowed_time, timeout, **kwargs |
| 302 | + "POST", |
| 303 | + url, |
| 304 | + data, |
| 305 | + headers, |
| 306 | + max_allowed_time, |
| 307 | + timeout, |
| 308 | + total_attempts, |
| 309 | + **kwargs, |
220 | 310 | ) |
221 | 311 |
|
222 | 312 | @functools.wraps(request) |
@@ -226,11 +316,50 @@ async def put( |
226 | 316 | data: Optional[bytes] = None, |
227 | 317 | headers: Optional[Mapping[str, str]] = None, |
228 | 318 | max_allowed_time: float = transport._DEFAULT_TIMEOUT_SECONDS, |
229 | | - timeout: float = transport._DEFAULT_TIMEOUT_SECONDS, |
| 319 | + timeout: Union[float, ClientTimeout] = transport._DEFAULT_TIMEOUT_SECONDS, |
| 320 | + total_attempts: Optional[int] = transport.DEFAULT_MAX_RETRY_ATTEMPTS, |
230 | 321 | **kwargs, |
231 | 322 | ) -> transport.Response: |
| 323 | + """ |
| 324 | + Args: |
| 325 | + url (str): The URI to be requested. |
| 326 | + data (Optional[bytes]): The payload or body in HTTP request. |
| 327 | + headers (Optional[Mapping[str, str]]): Request headers. |
| 328 | + max_allowed_time (float): |
| 329 | + If the method runs longer than this, a ``Timeout`` exception is |
| 330 | + automatically raised. Unlike the ``timeout`` parameter, this |
| 331 | + value applies to the total method execution time, even if |
| 332 | + multiple requests are made under the hood. |
| 333 | + timeout (float, aiohttp.ClientTimeout): |
| 334 | + The amount of time in seconds to wait for the server response |
| 335 | + with each individual request. |
| 336 | + total_attempts (int): |
| 337 | + The total number of retry attempts. |
| 338 | +
|
| 339 | + Mind that it is not guaranteed that the timeout error is raised |
| 340 | + at ``max_allowed_time``. It might take longer, for example, if |
| 341 | + an underlying request takes a lot of time, but the request |
| 342 | + itself does not timeout, e.g. if a large file is being |
| 343 | + transmitted. The timeout error will be raised after such |
| 344 | + request completes. |
| 345 | +
|
| 346 | + Returns: |
| 347 | + google.auth.aio.transport.Response: The HTTP response. |
| 348 | +
|
| 349 | + Raises: |
| 350 | + google.auth.exceptions.TimeoutError: If the method does not complete within |
| 351 | + the configured `max_allowed_time` or the request exceeds the configured |
| 352 | + `timeout`. |
| 353 | + """ |
232 | 354 | return await self.request( |
233 | | - "PUT", url, data, headers, max_allowed_time, timeout, **kwargs |
| 355 | + "PUT", |
| 356 | + url, |
| 357 | + data, |
| 358 | + headers, |
| 359 | + max_allowed_time, |
| 360 | + timeout, |
| 361 | + total_attempts, |
| 362 | + **kwargs, |
234 | 363 | ) |
235 | 364 |
|
236 | 365 | @functools.wraps(request) |
@@ -240,11 +369,50 @@ async def patch( |
240 | 369 | data: Optional[bytes] = None, |
241 | 370 | headers: Optional[Mapping[str, str]] = None, |
242 | 371 | max_allowed_time: float = transport._DEFAULT_TIMEOUT_SECONDS, |
243 | | - timeout: float = transport._DEFAULT_TIMEOUT_SECONDS, |
| 372 | + timeout: Union[float, ClientTimeout] = transport._DEFAULT_TIMEOUT_SECONDS, |
| 373 | + total_attempts: Optional[int] = transport.DEFAULT_MAX_RETRY_ATTEMPTS, |
244 | 374 | **kwargs, |
245 | 375 | ) -> transport.Response: |
| 376 | + """ |
| 377 | + Args: |
| 378 | + url (str): The URI to be requested. |
| 379 | + data (Optional[bytes]): The payload or body in HTTP request. |
| 380 | + headers (Optional[Mapping[str, str]]): Request headers. |
| 381 | + max_allowed_time (float): |
| 382 | + If the method runs longer than this, a ``Timeout`` exception is |
| 383 | + automatically raised. Unlike the ``timeout`` parameter, this |
| 384 | + value applies to the total method execution time, even if |
| 385 | + multiple requests are made under the hood. |
| 386 | + timeout (float, aiohttp.ClientTimeout): |
| 387 | + The amount of time in seconds to wait for the server response |
| 388 | + with each individual request. |
| 389 | + total_attempts (int): |
| 390 | + The total number of retry attempts. |
| 391 | +
|
| 392 | + Mind that it is not guaranteed that the timeout error is raised |
| 393 | + at ``max_allowed_time``. It might take longer, for example, if |
| 394 | + an underlying request takes a lot of time, but the request |
| 395 | + itself does not timeout, e.g. if a large file is being |
| 396 | + transmitted. The timeout error will be raised after such |
| 397 | + request completes. |
| 398 | +
|
| 399 | + Returns: |
| 400 | + google.auth.aio.transport.Response: The HTTP response. |
| 401 | +
|
| 402 | + Raises: |
| 403 | + google.auth.exceptions.TimeoutError: If the method does not complete within |
| 404 | + the configured `max_allowed_time` or the request exceeds the configured |
| 405 | + `timeout`. |
| 406 | + """ |
246 | 407 | return await self.request( |
247 | | - "PATCH", url, data, headers, max_allowed_time, timeout, **kwargs |
| 408 | + "PATCH", |
| 409 | + url, |
| 410 | + data, |
| 411 | + headers, |
| 412 | + max_allowed_time, |
| 413 | + timeout, |
| 414 | + total_attempts, |
| 415 | + **kwargs, |
248 | 416 | ) |
249 | 417 |
|
250 | 418 | @functools.wraps(request) |
@@ -254,11 +422,50 @@ async def delete( |
254 | 422 | data: Optional[bytes] = None, |
255 | 423 | headers: Optional[Mapping[str, str]] = None, |
256 | 424 | max_allowed_time: float = transport._DEFAULT_TIMEOUT_SECONDS, |
257 | | - timeout: float = transport._DEFAULT_TIMEOUT_SECONDS, |
| 425 | + timeout: Union[float, ClientTimeout] = transport._DEFAULT_TIMEOUT_SECONDS, |
| 426 | + total_attempts: Optional[int] = transport.DEFAULT_MAX_RETRY_ATTEMPTS, |
258 | 427 | **kwargs, |
259 | 428 | ) -> transport.Response: |
| 429 | + """ |
| 430 | + Args: |
| 431 | + url (str): The URI to be requested. |
| 432 | + data (Optional[bytes]): The payload or body in HTTP request. |
| 433 | + headers (Optional[Mapping[str, str]]): Request headers. |
| 434 | + max_allowed_time (float): |
| 435 | + If the method runs longer than this, a ``Timeout`` exception is |
| 436 | + automatically raised. Unlike the ``timeout`` parameter, this |
| 437 | + value applies to the total method execution time, even if |
| 438 | + multiple requests are made under the hood. |
| 439 | + timeout (float, aiohttp.ClientTimeout): |
| 440 | + The amount of time in seconds to wait for the server response |
| 441 | + with each individual request. |
| 442 | + total_attempts (int): |
| 443 | + The total number of retry attempts. |
| 444 | +
|
| 445 | + Mind that it is not guaranteed that the timeout error is raised |
| 446 | + at ``max_allowed_time``. It might take longer, for example, if |
| 447 | + an underlying request takes a lot of time, but the request |
| 448 | + itself does not timeout, e.g. if a large file is being |
| 449 | + transmitted. The timeout error will be raised after such |
| 450 | + request completes. |
| 451 | +
|
| 452 | + Returns: |
| 453 | + google.auth.aio.transport.Response: The HTTP response. |
| 454 | +
|
| 455 | + Raises: |
| 456 | + google.auth.exceptions.TimeoutError: If the method does not complete within |
| 457 | + the configured `max_allowed_time` or the request exceeds the configured |
| 458 | + `timeout`. |
| 459 | + """ |
260 | 460 | return await self.request( |
261 | | - "DELETE", url, data, headers, max_allowed_time, timeout, **kwargs |
| 461 | + "DELETE", |
| 462 | + url, |
| 463 | + data, |
| 464 | + headers, |
| 465 | + max_allowed_time, |
| 466 | + timeout, |
| 467 | + total_attempts, |
| 468 | + **kwargs, |
262 | 469 | ) |
263 | 470 |
|
264 | 471 | async def close(self) -> None: |
|