title: Retry class method if requests fail with code
body: <p>I have a Python class that sends payloads to AWS with <code>boto3</code> and <code>requests</code> library. However, sometimes the http requests fail with various codes, so I wanted to write a wrapper function inside the class that will retry to send the payload 5 times if it gets certain codes, and raise an exception if it completely fails. Here is the class method(assume method calls work as expected):</p>
  
  <pre><code>import requests
  from boto3 import Session
  
  def update_status(self, status):
      payload = status
  
      auth = self.sign_request()
      response = requests.patch(self.url, auth=auth, data=payload)
  
      status_code = response.status_code
      response_text = response.text
  
      if not response.ok:
          logging.error("Failed updating status of request: " + str(
              {'host': self.host, 'region': self.region,
               'service': self.service, 'url': self.url, 'status': str(status)}))
  
          raise IOError('Update training status failed with status code: ' + str(status_code) + '\n' + response_text)
  
      logging.info("Updated status")
  </code></pre>
  
  <p>Sometimes this api call will fail with status <code>504</code>. I would like to write a wrapper retry method around this class method that will by default retry 5 times with a wait of <code>retry^2</code> between every try, and exit the loop if it's a success with code <code>200</code>.</p>
  
  <p>I found this code which seems to be along the lines of what I would use, I'm just not sure how to wrap my current method inside this and call it:</p>
  
  <pre><code>import requests
  from requests.adapters import HTTPAdapter
  from requests.packages.urllib3.util.retry import Retry
  
  
  def requests_retry_session(
      retries=5,
      backoff_factor=0.3,
      status_forcelist=(500, 502, 504),
      session=None,
  ):
      session = session or requests.Session()
      retry = Retry(
          total=retries,
          read=retries,
          connect=retries,
          backoff_factor=backoff_factor,
          status_forcelist=status_forcelist,
      )
      adapter = HTTPAdapter(max_retries=retry)
      session.mount('http://', adapter)
      session.mount('https://', adapter)
      return session
  </code></pre>
  
  <p>Issue with the above code is it is using <code>requests.session</code> and returning it while my class is already using <code>boto3.Session</code>. Any help would be appreciated!</p>