Skip to content

Commit

Permalink
Retry request option added, closes MichalJanik/mergado/issues/#7962
Browse files Browse the repository at this point in the history
-bump version to 0.1.5
  • Loading branch information
alby-m committed Mar 24, 2021
1 parent a23df05 commit bd024d1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
5 changes: 3 additions & 2 deletions mergadoapiclient/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-


__version__ = '0.1.4'
__authors__ = ['Lukáš Ševčík', 'Peter Smatana', 'Pavel Dedík', 'Matěj Hamala']
__version__ = '0.1.5'
__authors__ = ['Lukáš Ševčík', 'Peter Smatana', 'Pavel Dedík', 'Matěj Hamala',
'Alžbeta Mazurkovičová']
27 changes: 23 additions & 4 deletions mergadoapiclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import importlib
import time

try:
from urlparse import urljoin
Expand All @@ -25,11 +26,23 @@ def build(credentials, api_version='0.3'):
NotImplementedError('Grant type not implemented.')


def retry_request(make_request, retry_status_list, tries):
for i in range(tries):
response = make_request()
if response.status_code not in retry_status_list:
break
wait = 2 ** i
time.sleep(wait)
response.raise_for_status()
return response


class BaseClient(object):

def __init__(self, client_id, client_secret,
grant_type=None, storage_class=config.TOKEN_STORAGE_CLASS,
token_uri=config.TOKEN_URI, api_uri=config.MERGADO_API_URI):
token_uri=config.TOKEN_URI, api_uri=config.MERGADO_API_URI,
retry_status_list=(500, 502, 503, 504), tries=3):

self.client_id = client_id
self.client_secret = client_secret
Expand All @@ -39,6 +52,8 @@ def __init__(self, client_id, client_secret,
self.storage = self.TokenStorage()
self.token_uri = token_uri
self.api_uri = api_uri
self.retry_status_list = retry_status_list
self.tries = tries

@property
def _token_headers(self):
Expand Down Expand Up @@ -70,9 +85,13 @@ def get_url(self, path):
return urljoin(self.api_uri, path)

def request(self, method, path, **options):
response = http.request(method, self.get_url(path),
headers=self._token_headers, **options)
response.raise_for_status()
def make_request():
return http.request(
method, self.get_url(path),
headers=self._token_headers, **options)

response = retry_request(
make_request, self.retry_status_list, self.tries)
return response.json()

def get(self, path, **options):
Expand Down

0 comments on commit bd024d1

Please sign in to comment.