Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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