Skip to content

Commit d3c8673

Browse files
authored
Merge pull request #14 from dataquest-dev/add_timeout
add timeout
2 parents e1c05a9 + db03111 commit d3c8673

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

dspace_rest_client/client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def api_get(self, url, params=None, data=None, headers=None):
218218
self.update_token(r)
219219
return r
220220

221-
def api_post(self, url, params, json, retry=False):
221+
def api_post(self, url, params, json, retry=False, timeout=None):
222222
"""
223223
Perform a POST request. Refresh XSRF token if necessary.
224224
POSTs are typically used to create objects.
@@ -230,7 +230,7 @@ def api_post(self, url, params, json, retry=False):
230230
"""
231231
self._last_err = None
232232
r = self.session.post(url, json=json, params=params, headers=self.request_headers,
233-
proxies=self.proxies)
233+
proxies=self.proxies, timeout=timeout)
234234
self.update_token(r)
235235

236236
if r.status_code == 403:
@@ -239,18 +239,18 @@ def api_post(self, url, params, json, retry=False):
239239
# After speaking in #dev it seems that these do need occasional refreshes but I suspect
240240
# it's happening too often for me, so check for accidentally triggering it
241241
r_json = parse_json(r)
242-
if 'message' in r_json and 'CSRF token' in r_json['message']:
242+
if 'message' in (r_json or {}) and 'CSRF token' in r_json['message']:
243243
if retry:
244244
_logger.warning(f'Too many retries updating token: {r.status_code}: {r.text}')
245245
else:
246246
_logger.debug("Retrying request with updated CSRF token")
247-
return self.api_post(url, params=params, json=json, retry=True)
247+
return self.api_post(url, params=params, json=json, retry=True, timeout=timeout)
248248

249249
# we need to log in again, if there is login error. This is a bad
250250
# solution copied from the past
251251
elif r.status_code == 401:
252252
r_json = parse_json(r)
253-
if 'message' in r_json and 'Authentication is required' in r_json['message']:
253+
if 'message' in (r_json or {}) and 'Authentication is required' in r_json['message']:
254254
if retry:
255255
logging.error(
256256
'API Post: Already retried... something must be wrong')
@@ -260,7 +260,7 @@ def api_post(self, url, params, json, retry=False):
260260
self.authenticate()
261261
# Try to authenticate and repeat the request 3 times -
262262
# if it won't happen log error
263-
return self.api_post(url, params=params, json=json, retry=False)
263+
return self.api_post(url, params=params, json=json, retry=True, timeout=timeout)
264264
return r
265265

266266
def api_post_uri(self, url, params, uri_list, retry=False):

0 commit comments

Comments
 (0)