|
| 1 | +import backoff |
| 2 | +import requests |
| 3 | +from requests.exceptions import HTTPError |
| 4 | +import logging |
| 5 | + |
| 6 | + |
| 7 | +# Function to log retry attempts |
| 8 | +def log_retry(details): |
| 9 | + logging.error(f"Request failed, retrying... Attempt #{details['tries']}") |
| 10 | + |
| 11 | + |
| 12 | +class BaseHttpClient: |
| 13 | + # Initialize HttpClient with a backoff_max value |
| 14 | + def __init__(self, backoff_max=30): |
| 15 | + self.backoff_max = backoff_max |
| 16 | + |
| 17 | + # GET request method with exponential backoff |
| 18 | + @backoff.on_exception( |
| 19 | + backoff.expo, |
| 20 | + HTTPError, |
| 21 | + max_time=30, |
| 22 | + on_backoff=log_retry |
| 23 | + ) |
| 24 | + def get(self, url, *args, **kwargs): |
| 25 | + """ |
| 26 | + Sends a GET request to the specified URL with optional extra arguments. |
| 27 | +
|
| 28 | + This method is a thin wrapper around `requests.get()`. Any additional arguments |
| 29 | + are passed directly to `requests.get()`. For more information on the available |
| 30 | + arguments, refer to the `requests.get()` documentation: |
| 31 | + https://docs.python-requests.org/en/latest/api/#requests.get |
| 32 | +
|
| 33 | + Args: |
| 34 | + url (str): The URL to send the GET request to. |
| 35 | + *args: Optional positional arguments passed to `requests.get()`. |
| 36 | + **kwargs: Optional keyword arguments passed to `requests.get()`. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + Response: The Response object from `requests.get()`. |
| 40 | + Raises: |
| 41 | + HTTPError: If the request fails for a network-related reason. |
| 42 | + """ |
| 43 | + return self._handle_request('GET', url, *args, **kwargs) |
| 44 | + |
| 45 | + # POST request method with exponential backoff |
| 46 | + @backoff.on_exception( |
| 47 | + backoff.expo, |
| 48 | + HTTPError, |
| 49 | + max_time=30, |
| 50 | + on_backoff=log_retry, |
| 51 | + ) |
| 52 | + def post(self, url, *args, **kwargs): |
| 53 | + """ |
| 54 | + Sends a POST request to the specified URL with optional extra arguments. |
| 55 | +
|
| 56 | + This method is a thin wrapper around `requests.post()`. Any additional arguments |
| 57 | + are passed directly to `requests.post()`. For more information on the available |
| 58 | + arguments, refer to the `requests.post()` documentation: |
| 59 | + https://docs.python-requests.org/en/latest/api/#requests.post |
| 60 | +
|
| 61 | + Args: |
| 62 | + url (str): The URL to send the POST request to. |
| 63 | + *args: Optional positional arguments passed to `requests.post()`. |
| 64 | + **kwargs: Optional keyword arguments passed to `requests.post()`. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + Response: The Response object from `requests.post()`. |
| 68 | +
|
| 69 | + Raises: |
| 70 | + HTTPError: If the request fails for a network-related reason. |
| 71 | + """ |
| 72 | + return self._handle_request('POST', url, *args, **kwargs) |
| 73 | + |
| 74 | + # Method to handle requests for GET and POST |
| 75 | + def _handle_request(self, method, url, *args, **kwargs): |
| 76 | + logging.info(f"Sending {method} request to {url}") |
| 77 | + try: |
| 78 | + response = requests.request(method, url, *args, **kwargs) |
| 79 | + response.raise_for_status() |
| 80 | + return response |
| 81 | + |
| 82 | + except HTTPError as http_err: |
| 83 | + logging.error(f"HTTP error occurred: {http_err} when sending a {method} to {url} with headers {kwargs.get('headers')}") |
| 84 | + raise http_err |
| 85 | + except Exception as err: |
| 86 | + logging.error(f"Other error occurred: {err} when sending a {method} to {url} with headers {kwargs.get('headers')}") |
| 87 | + raise err |
0 commit comments