From 5368081b6b825ada4979787fd6f8fe090b23f7fa Mon Sep 17 00:00:00 2001 From: Quentame Date: Wed, 12 Feb 2020 13:45:35 +0100 Subject: [PATCH] Fix 450 error (#230) - while refreshing client after 20 min of non request --- pyicloud/base.py | 10 ++++++++++ pyicloud/exceptions.py | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pyicloud/base.py b/pyicloud/base.py index 65baa50..87fa533 100644 --- a/pyicloud/base.py +++ b/pyicloud/base.py @@ -65,12 +65,22 @@ class PyiCloudSession(requests.Session): logger.debug("%s %s %s", args[0], args[1], kwargs.get('data', '')) + kwargs.pop('retried', None) response = super(PyiCloudSession, self).request(*args, **kwargs) content_type = response.headers.get('Content-Type', '').split(';')[0] json_mimetypes = ['application/json', 'text/json'] if not response.ok and content_type not in json_mimetypes: + if kwargs.get('retried') is None and response.status_code == 450: + api_error = PyiCloudAPIResponseError( + response.reason, + response.status_code, + retry=True + ) + logger.warn(api_error) + kwargs['retried'] = True + return self.request(*args, **kwargs) self._raise_error(response.status_code, response.reason) if content_type not in json_mimetypes: diff --git a/pyicloud/exceptions.py b/pyicloud/exceptions.py index e3bee50..f3cadea 100644 --- a/pyicloud/exceptions.py +++ b/pyicloud/exceptions.py @@ -8,12 +8,14 @@ class PyiCloudNoDevicesException(PyiCloudException): class PyiCloudAPIResponseError(PyiCloudException): - def __init__(self, reason, code): + def __init__(self, reason, code, retry=False): self.reason = reason self.code = code - message = reason + message = reason or "" if code: message += " (%s)" % code + if retry: + message += ". Retrying ..." super(PyiCloudAPIResponseError, self).__init__(message)