From 51b65fe7970348b6b6e78a1adf90b1f2601264ed Mon Sep 17 00:00:00 2001 From: jm Date: Tue, 24 Feb 2026 12:45:03 +0100 Subject: [PATCH 1/2] add timeout --- dspace_rest_client/client.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dspace_rest_client/client.py b/dspace_rest_client/client.py index 8e238f2..b2d3a89 100644 --- a/dspace_rest_client/client.py +++ b/dspace_rest_client/client.py @@ -218,7 +218,7 @@ def api_get(self, url, params=None, data=None, headers=None): self.update_token(r) return r - def api_post(self, url, params, json, retry=False): + def api_post(self, url, params, json, retry=False, timeout=None): """ Perform a POST request. Refresh XSRF token if necessary. POSTs are typically used to create objects. @@ -230,7 +230,7 @@ def api_post(self, url, params, json, retry=False): """ self._last_err = None r = self.session.post(url, json=json, params=params, headers=self.request_headers, - proxies=self.proxies) + proxies=self.proxies, timeout=timeout) self.update_token(r) if r.status_code == 403: @@ -244,7 +244,7 @@ def api_post(self, url, params, json, retry=False): _logger.warning(f'Too many retries updating token: {r.status_code}: {r.text}') else: _logger.debug("Retrying request with updated CSRF token") - return self.api_post(url, params=params, json=json, retry=True) + return self.api_post(url, params=params, json=json, retry=True, timeout=timeout) # we need to log in again, if there is login error. This is a bad # solution copied from the past @@ -260,7 +260,7 @@ def api_post(self, url, params, json, retry=False): self.authenticate() # Try to authenticate and repeat the request 3 times - # if it won't happen log error - return self.api_post(url, params=params, json=json, retry=False) + return self.api_post(url, params=params, json=json, retry=False, timeout=timeout) return r def api_post_uri(self, url, params, uri_list, retry=False): From db03111fd9ac629dce0a22ee76af1ba68b06de6d Mon Sep 17 00:00:00 2001 From: jm Date: Wed, 25 Feb 2026 10:46:21 +0100 Subject: [PATCH 2/2] more robust check --- dspace_rest_client/client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dspace_rest_client/client.py b/dspace_rest_client/client.py index b2d3a89..0115557 100644 --- a/dspace_rest_client/client.py +++ b/dspace_rest_client/client.py @@ -239,7 +239,7 @@ def api_post(self, url, params, json, retry=False, timeout=None): # After speaking in #dev it seems that these do need occasional refreshes but I suspect # it's happening too often for me, so check for accidentally triggering it r_json = parse_json(r) - if 'message' in r_json and 'CSRF token' in r_json['message']: + if 'message' in (r_json or {}) and 'CSRF token' in r_json['message']: if retry: _logger.warning(f'Too many retries updating token: {r.status_code}: {r.text}') else: @@ -250,7 +250,7 @@ def api_post(self, url, params, json, retry=False, timeout=None): # solution copied from the past elif r.status_code == 401: r_json = parse_json(r) - if 'message' in r_json and 'Authentication is required' in r_json['message']: + if 'message' in (r_json or {}) and 'Authentication is required' in r_json['message']: if retry: logging.error( 'API Post: Already retried... something must be wrong') @@ -260,7 +260,7 @@ def api_post(self, url, params, json, retry=False, timeout=None): self.authenticate() # Try to authenticate and repeat the request 3 times - # if it won't happen log error - return self.api_post(url, params=params, json=json, retry=False, timeout=timeout) + return self.api_post(url, params=params, json=json, retry=True, timeout=timeout) return r def api_post_uri(self, url, params, uri_list, retry=False):