11# Copyright (c) Microsoft Corporation.
22# Licensed under the MIT license.
33
4+ """
5+ HTTP client with automatic retry logic and timeout handling.
6+
7+ This module provides :class:`HttpClient`, a wrapper around the requests library
8+ that adds configurable retry behavior for transient network errors and
9+ intelligent timeout management based on HTTP method types.
10+ """
11+
412from __future__ import annotations
513
614import time
1018
1119
1220class HttpClient :
21+ """
22+ HTTP client with configurable retry logic and timeout handling.
23+
24+ Provides automatic retry behavior for transient failures and default timeout
25+ management for different HTTP methods.
26+
27+ :param retries: Maximum number of retry attempts for transient errors. Default is 5.
28+ :type retries: int or None
29+ :param backoff: Base delay in seconds between retry attempts. Default is 0.5.
30+ :type backoff: float or None
31+ :param timeout: Default request timeout in seconds. If None, uses per-method defaults.
32+ :type timeout: float or None
33+ """
34+
1335 def __init__ (
1436 self ,
1537 * ,
@@ -22,8 +44,23 @@ def __init__(
2244 self .default_timeout : Optional [float ] = timeout
2345
2446 def request (self , method : str , url : str , ** kwargs : Any ) -> requests .Response :
25- # Apply per-method default timeouts if not provided
26- # Apply default timeout if not provided; fall back to per-method defaults
47+ """
48+ Execute an HTTP request with automatic retry logic and timeout management.
49+
50+ Applies default timeouts based on HTTP method (120s for POST/DELETE, 10s for others)
51+ and retries on network errors with exponential backoff.
52+
53+ :param method: HTTP method (GET, POST, PUT, DELETE, etc.).
54+ :type method: str
55+ :param url: Target URL for the request.
56+ :type url: str
57+ :param kwargs: Additional arguments passed to ``requests.request()``, including headers, data, etc.
58+ :return: HTTP response object.
59+ :rtype: requests.Response
60+ :raises requests.exceptions.RequestException: If all retry attempts fail.
61+ """
62+ # If no timeout is provided, use the user-specified default timeout if set;
63+ # otherwise, apply per-method defaults (120s for POST/DELETE, 10s for others).
2764 if "timeout" not in kwargs :
2865 if self .default_timeout is not None :
2966 kwargs ["timeout" ] = self .default_timeout
0 commit comments