pyshipstation icon indicating copy to clipboard operation
pyshipstation copied to clipboard

Retry on 5xx server errors

Open NathanSmeltzer opened this issue 2 years ago • 1 comments

After receiving a 504 GATEWAY_TIMEOUT from ShipStation's API, a retry with a backoff would improve stability. Here a function that would solve this issue from this retries in requests guide:

def get_session(
    retries: int = 5,
    backoff_factor: float = 1,
    status_forcelist: list = [502, 503, 504],
) -> requests.Session:
    """Returns a requests session that retries on specific failures.
    also: https://stackoverflow.com/a/35636367/2469390
    https://www.coglib.com/~icordasc/blog/2014/12/retries-in-requests.html
    """
    session = 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

Then I think ShipStation.get would change to:

    def get(self, endpoint="", payload=None):
        url = "{}{}".format(self.url, endpoint)
       session = get_session()
        r = session.get(url, auth=(self.key, self.secret), params=payload)
        if self.debug:
            pprint.PrettyPrinter(indent=4).pprint(r.json())

        return r

And ShipStation.post to:

    def post(self, endpoint="", data=None):
        url = "{}{}".format(self.url, endpoint)
        headers = {"content-type": "application/json"}
       session = get_session()
        r = session.post(url, auth=(self.key, self.secret),
                          data=data, headers=headers)
        if self.debug:
            pprint.PrettyPrinter(indent=4).pprint(r.json())

       return r

And similar for put()

NathanSmeltzer avatar Feb 10 '23 14:02 NathanSmeltzer

PR submitted #39

NathanSmeltzer avatar Feb 10 '23 15:02 NathanSmeltzer