def _send_request_with_retries()

in cvm-attestation/src/ImdsClient.py [0:0]


  def _send_request_with_retries(self, method, url, headers=None, data=None,
                                 max_retries=5, initial_delay=1, delay_strategy='constant',
                                 expected_status=200, exception_class=Exception):
    retries = 0
    delay = initial_delay

    while retries < max_retries:
      try:
        self.log.info(f"Sending {method.upper()} request to {url}")
        response = requests.request(method, url, headers=headers, data=data)

        if response.status_code == expected_status:
          return response

        self.log.warning(f"Unexpected status code {response.status_code}: {response.text}")
        retries += 1
        if retries >= max_retries:
          raise exception_class(f"Error {response.status_code}: {response.text}")
      except requests.exceptions.RequestException as e:
        self.log.error("Request exception occurred", exc_info=True)
        retries += 1
        if retries >= max_retries:
          raise exception_class(f"HTTP request failed with error {e}") from e

      self.log.info(f"Retrying in {delay} seconds... Attempt {retries}")
      time.sleep(delay)
      delay = delay * 2 if delay_strategy == 'exponential' else delay

    raise exception_class("Request failed after all retries")