Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting of a timeout. sometimes API requests never finish... #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions xolphin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Client(object):
BASE_URL_TEST = 'https://test-api.xolphin.com/v1/'
VERSION = '1.6.0'

def __init__(self, username, password, test = False):
def __init__(self, username, password, test=False, timeout=None):

if(test):
self.url = self.BASE_URL_TEST
Expand All @@ -34,21 +34,22 @@ def __init__(self, username, password, test = False):
self._session = requests.session()
self._session.headers.update({'Accept': 'application/json', 'User-Agent': 'xolphin-api-python/%s' % Client.VERSION})
self._session.auth = HTTPBasicAuth(username, password)
self.timeout = timeout
#self._session.proxies.update({
# 'http': '127.0.0.1:8888',
# 'https': '127.0.0.1:8888',
#})
#self._session.verify = False

def get(self, method, data={}):
response = self._session.get("%s%s" % (self.url, method), params=data)
response = self._session.get("%s%s" % (self.url, method), params=data, timeout=self.timeout)
if 200 <= response.status_code < 300:
return json.loads(response.content.decode('utf-8'))
else:
raise Exception(response.content)

def download(self, method, data={}):
response = self._session.get("%s%s" % (self.url, method), params=data)
response = self._session.get("%s%s" % (self.url, method), params=data, timeout=self.timeout)
if 200 <= response.status_code < 300:
return response
else:
Expand All @@ -61,7 +62,7 @@ def post(self, method, data={}):
payload[k] = (data['description'], data[k], 'application/pdf')
else:
payload[k] = (None, str(data[k]))
response = self._session.post("%s%s" % (self.url, method), files=payload)
response = self._session.post("%s%s" % (self.url, method), files=payload, timeout=self.timeout)
if 200 <= response.status_code < 300:
return json.loads(response.content.decode('utf-8'))
else:
Expand Down