Skip to content

Commit

Permalink
Fix 450 error (#230)
Browse files Browse the repository at this point in the history
- while refreshing client after 20 min of non request
  • Loading branch information
Quentame authored Feb 12, 2020
1 parent 623fb66 commit 5368081
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 10 additions & 0 deletions pyicloud/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,22 @@ def request(self, *args, **kwargs):

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:
Expand Down
6 changes: 4 additions & 2 deletions pyicloud/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

5 comments on commit 5368081

@vajonam
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Quentame when are you going to push this to a release? 0.9.3?

@Quentame
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah 😅

Next week 😉

@vajonam
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should add I have been testing this and its been working fine.

@Quentame
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback

@Quentame
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.