Skip to content

Commit

Permalink
Merge branch 'feature/retry' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
borislavd88 committed Oct 28, 2020
2 parents 4960e31 + 94badfc commit 0654fe6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
16 changes: 14 additions & 2 deletions sevenbridges/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,20 @@ def _request(self, verb, url, headers=None, params=None, data=None,
verb, url, params=params, stream=stream, allow_redirects=True,
)
if self.error_handlers:
for error_handler in self.error_handlers:
response = error_handler(self, response)
while True:
for error_handler in self.error_handlers:
handled_response = error_handler(self, response)
# if error handler 'is_repeatable', and error handling
# occurred, iterate again
if hasattr(error_handler, 'is_repeatable'):
if response != handled_response:
response = handled_response
break
else:
response = handled_response
else:
break

headers = response.headers
self._limit = headers.get('X-RateLimit-Limit', self._limit)
self._remaining = headers.get('X-RateLimit-Remaining', self._remaining)
Expand Down
12 changes: 12 additions & 0 deletions sevenbridges/http/error_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
logger = logging.getLogger(__name__)


def repeatable_handler(f):
"""
Marks 'repeatable' error handlers. Error handler is repeatable if propagate
input response in case of no error handling occurred.
"""
f.is_repeatable = True
return f


@repeatable_handler
@retry_on_excs(excs=(HTTPError, UrlLibHTTPError))
def rate_limit_sleeper(api, response):
"""
Expand All @@ -31,6 +41,7 @@ def rate_limit_sleeper(api, response):
return response


@repeatable_handler
@retry_on_excs(excs=(HTTPError, UrlLibHTTPError, JSONDecodeError))
def maintenance_sleeper(api, response, sleep=300):
"""
Expand Down Expand Up @@ -58,6 +69,7 @@ def maintenance_sleeper(api, response, sleep=300):
return response


@repeatable_handler
@retry_on_excs(excs=(HTTPError, UrlLibHTTPError))
def general_error_sleeper(api, response, sleep=300):
"""
Expand Down

0 comments on commit 0654fe6

Please sign in to comment.