From 41d8272603c4550f64aeac2ea0f13735b34a0a7e Mon Sep 17 00:00:00 2001 From: Mark Schouten Date: Wed, 10 Jun 2020 17:06:07 +0200 Subject: [PATCH] Allow setting of a timeout. sometimes API requests never finish... --- xolphin/client.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/xolphin/client.py b/xolphin/client.py index 6e3fa65..e1fd3bf 100644 --- a/xolphin/client.py +++ b/xolphin/client.py @@ -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 @@ -34,6 +34,7 @@ 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', @@ -41,14 +42,14 @@ def __init__(self, username, password, test = False): #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: @@ -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: