I have a class CustomSession
. It behaves the same way as requests.Session
, except calling the request
method of this new class first makes a preliminary request prelim_request
first using some pre-defined PRELIM_PARAMS
, and then secondly makes a request request
with user-provided parameters.
class CustomSession(requests.Session): def __init__(self): super().__init__() self.prelim_params = PRELIM_PARAMS def prelim_request(self): response = super().request(**self.prelim_params) def request(self, method, url, **kwargs): self.prelim_request() return super().request(method, url, **kwargs)
I want to implement some retry logic so that CustomSession.request
is re-tried if the status_code
of its output is either [502, 503, 504]
. If I was just using the parent class requests.Session
to make requests this would be done as follows:
retries = Retry(total=7, backoff_factor=1, status_forcelist=[502, 503, 504], allowed_methods=None)session.mount('http://', HTTPAdapter(max_retries=retries))session.mount('https://', HTTPAdapter(max_retries=retries))
But this only re-tries individual requests, i.e. if CustomSession.request
outputs a response with status_code
as 502
then only the second of the two requests within the method super().request(method, url, **kwargs)
is retried, and NOT the entire CustomSession.request
method itself (the two requests banded together).
I can probably resort to using try-except logic here but I would like to still use the Retry
adapter if possible. How can I still make use of Retry
here?