`responses` with `requests` retry handling

1.1k Views Asked by At

I'm attempting to implement/test request retrying within my module - below is a scratch which has the base functionality of what I'm trying to accomplish.

import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import responses

retry_strategy = Retry(
    total=3,
    status_forcelist=[429, 500, 502, 503, 504],
    method_whitelist=["HEAD", "GET", "OPTIONS"]
)
adapter = HTTPAdapter(max_retries=retry_strategy)
http = requests.Session()
http.mount("https://", adapter)
http.mount("http://", adapter)

with responses.RequestsMock() as rsps:
    rsps.add(responses.GET,
             "https://en.wikipedia.org/w/api.php",
             status=502)

    response = http.get("https://en.wikipedia.org/w/api.php")
    print(len(rsps.calls))
print(response)

The base setup is from this article, although is used elsewhere on stackoverflow.

I'd expect len(rsps.calls) to be 3, since this from my understanding is the amount of retries we would attempt before giving up. However, the output is instead;

1
<Response [502]>

Is this due to the setup with responses, or is my initial configuration incorrect?

Any help would be greatly appreciated - thanks!

1

There are 1 best solutions below

0
On

Actually you are not looking for the number of calls, but the number of performed retries. if that is the case, I assume that the answer will be response.raw.retries.history

I've found it here