Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Retry on 5xx server errors #38

@NathanSmeltzer

Description

@NathanSmeltzer

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions