Skip to content

Commit

Permalink
Added retrying support on rate limit exceeding. Closes #43
Browse files Browse the repository at this point in the history
  • Loading branch information
Stranger6667 committed Sep 19, 2016
1 parent 7c1e62c commit 2ef0510
Show file tree
Hide file tree
Showing 5 changed files with 396 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Expand Up @@ -8,6 +8,7 @@ Changelog

- Fixed credentials cleaning for non gzipped content. `#45`_
- Added ``fields`` parameter. `#34`_
- Added retrying support on rate limit exceeding. `#43`_

0.4.4 (2016-09-09)
------------------
Expand Down Expand Up @@ -103,6 +104,7 @@ Changelog


.. _#45: https://github.com/Stranger6667/pyoffers/issues/45
.. _#43: https://github.com/Stranger6667/pyoffers/issues/43
.. _#42: https://github.com/Stranger6667/pyoffers/issues/42
.. _#41: https://github.com/Stranger6667/pyoffers/issues/41
.. _#40: https://github.com/Stranger6667/pyoffers/issues/40
Expand Down
30 changes: 28 additions & 2 deletions pyoffers/api.py
Expand Up @@ -2,8 +2,9 @@
from collections import OrderedDict

import requests
import time

from .exceptions import HasOffersException
from .exceptions import HasOffersException, MaxRetriesExceeded
from .logging import get_logger
from .models import MODEL_MANAGERS
from .utils import prepare_query_params
Expand All @@ -17,16 +18,40 @@ def is_paginated(data):
return 'pageCount' in data


def retry(method):
"""
Allows to retry method execution few times.
"""

def inner(self, *args, **kwargs):
attempt_number = 1
while attempt_number < self.retries:
try:
return method(self, *args, **kwargs)
except HasOffersException as exc:
if not 'API usage exceeded rate limit' in str(exc):
raise exc
self.logger.debug('Retrying due: %s', exc)
time.sleep(self.retry_timeout)
attempt_number += 1
raise MaxRetriesExceeded

return inner


class HasOffersAPI:
"""
Client to communicate with HasOffers API.
"""

def __init__(self, endpoint=None, network_token=None, network_id=None, verify=True, verbosity=0):
def __init__(self, endpoint=None, network_token=None, network_id=None, verify=True, retries=3, retry_timeout=3,
verbosity=0):
self.endpoint = endpoint
self.network_token = network_token
self.network_id = network_id
self.verify = verify
self.retries = retries
self.retry_timeout = retry_timeout
self.logger = get_logger(verbosity)
self.setup_managers()

Expand All @@ -53,6 +78,7 @@ def session(self):
self._session = requests.Session()
return self._session

@retry
def _call(self, target, method, single_result=True, raw=False, **kwargs):
"""
Low-level call to HasOffers API.
Expand Down
4 changes: 4 additions & 0 deletions pyoffers/exceptions.py
Expand Up @@ -3,3 +3,7 @@

class HasOffersException(BaseException):
pass


class MaxRetriesExceeded(HasOffersException):
pass

0 comments on commit 2ef0510

Please sign in to comment.