Skip to content

Commit

Permalink
Merge pull request #79 from prakashpp/develop
Browse files Browse the repository at this point in the history
Provision to retry on rate limit
  • Loading branch information
prakashpp authored Aug 10, 2020
2 parents 4cd0ca9 + 1e13051 commit 2957db0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
23 changes: 20 additions & 3 deletions fulfil_client/client.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import base64
import hashlib
import hmac
import json
import logging
from collections import defaultdict
from contextlib import contextmanager
from datetime import datetime
from functools import wraps

import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from more_itertools import chunked
from .serialization import dumps, loads
from .exceptions import (
UserError, ClientError, ServerError, AuthenticationError
UserError, ClientError, ServerError, AuthenticationError, RateLimitError
)
from .signals import response_received
from .exceptions import Error # noqa
Expand Down Expand Up @@ -46,6 +47,8 @@ def wrapper(*args, **kwargs):
# logged out. Either way raise this error so the app
# can decide how to handle logouts.
raise AuthenticationError(loads(rv.text), rv.status_code)
elif rv.status_code == 429:
raise RateLimitError(loads(rv.text), rv.status_code)
elif 402 <= rv.status_code and rv.status_code < 500:
# 4XX range errors always have a JSON response
# with a code, message and description.
Expand Down Expand Up @@ -109,7 +112,8 @@ class Client(object):

def __init__(self, subdomain,
api_key=None, context=None, auth=None,
user_agent="Python Client", base_url="fulfil.io"):
user_agent="Python Client", base_url="fulfil.io",
retry_on_rate_limit=False):
self.subdomain = subdomain

if self.subdomain == 'localhost':
Expand All @@ -125,6 +129,19 @@ def __init__(self, subdomain,
else:
self.set_auth(auth)

if retry_on_rate_limit:
retries = 5
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=0.5,
status_forcelist=(429, ),
)
adapter = HTTPAdapter(max_retries=retry)
self.session.mount('http://', adapter)
self.session.mount('https://', adapter)

self.session.headers.update({
'User-Agent': user_agent,
})
Expand Down
4 changes: 4 additions & 0 deletions fulfil_client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ def __init__(self, message, code, description=None):

def __getnewargs__(self):
return (self.message, self.code, self.description)


class RateLimitError(ClientError):
pass

0 comments on commit 2957db0

Please sign in to comment.