From 412cfd8c7e470a6dfdeab4eec1a70042d03b1653 Mon Sep 17 00:00:00 2001 From: Quentin POLLET Date: Mon, 9 Mar 2020 22:22:04 +0100 Subject: [PATCH] Errors are Exceptions [breaking-change] - PyiCloudAPIResponseError --> PyiCloudAPIResponseException - PyiCloud2SARequiredError --> PyiCloud2SARequiredException - NoStoredPasswordAvailable --> PyiCloudNoStoredPasswordAvailableException - PyiCloudServiceNotActivatedErrror --> PyiCloudServiceNotActivatedException --- pyicloud/base.py | 18 +++++++++--------- pyicloud/exceptions.py | 12 ++++++------ pyicloud/services/photos.py | 4 ++-- pyicloud/utils.py | 8 ++++---- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/pyicloud/base.py b/pyicloud/base.py index da1246a..488864f 100644 --- a/pyicloud/base.py +++ b/pyicloud/base.py @@ -12,9 +12,9 @@ from re import match from pyicloud.exceptions import ( PyiCloudFailedLoginException, - PyiCloudAPIResponseError, - PyiCloud2SARequiredError, - PyiCloudServiceNotActivatedErrror + PyiCloudAPIResponseException, + PyiCloud2SARequiredException, + PyiCloudServiceNotActivatedException ) from pyicloud.services import ( FindMyiPhoneServiceManager, @@ -73,7 +73,7 @@ class PyiCloudSession(requests.Session): 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( + api_error = PyiCloudAPIResponseException( response.reason, response.status_code, retry=True @@ -114,11 +114,11 @@ class PyiCloudSession(requests.Session): def _raise_error(self, code, reason): if self.service.requires_2sa and \ reason == 'Missing X-APPLE-WEBAUTH-TOKEN cookie': - raise PyiCloud2SARequiredError(self.service.user['apple_id']) + raise PyiCloud2SARequiredException(self.service.user['apple_id']) if code == 'ZONE_NOT_FOUND' or code == 'AUTHENTICATION_FAILED': reason = 'Please log into https://icloud.com/ to manually ' \ 'finish setting up your iCloud service' - api_error = PyiCloudServiceNotActivatedErrror(reason, code) + api_error = PyiCloudServiceNotActivatedException(reason, code) logger.error(api_error) raise(api_error) @@ -127,7 +127,7 @@ class PyiCloudSession(requests.Session): 'again. The remote servers might be trying to ' \ 'throttle requests.' - api_error = PyiCloudAPIResponseError(reason, code) + api_error = PyiCloudAPIResponseException(reason, code) logger.error(api_error) raise api_error @@ -221,7 +221,7 @@ class PyiCloudService(object): params=self.params, data=json.dumps(data) ) - except PyiCloudAPIResponseError as error: + except PyiCloudAPIResponseException as error: msg = 'Invalid email/password combination.' raise PyiCloudFailedLoginException(msg, error) @@ -286,7 +286,7 @@ class PyiCloudService(object): params=self.params, data=data ) - except PyiCloudAPIResponseError as error: + except PyiCloudAPIResponseException as error: if error.code == -21669: # Wrong verification code return False diff --git a/pyicloud/exceptions.py b/pyicloud/exceptions.py index e9eb6f5..c26f23f 100644 --- a/pyicloud/exceptions.py +++ b/pyicloud/exceptions.py @@ -7,7 +7,7 @@ class PyiCloudNoDevicesException(PyiCloudException): pass -class PyiCloudAPIResponseError(PyiCloudException): +class PyiCloudAPIResponseException(PyiCloudException): def __init__(self, reason, code, retry=False): self.reason = reason self.code = code @@ -17,22 +17,22 @@ class PyiCloudAPIResponseError(PyiCloudException): if retry: message += ". Retrying ..." - super(PyiCloudAPIResponseError, self).__init__(message) + super(PyiCloudAPIResponseException, self).__init__(message) class PyiCloudFailedLoginException(PyiCloudException): pass -class PyiCloud2SARequiredError(PyiCloudException): +class PyiCloud2SARequiredException(PyiCloudException): def __init__(self, apple_id): message = "Two-step authentication required for account: %s" % apple_id - super(PyiCloud2SARequiredError, self).__init__(message) + super(PyiCloud2SARequiredException, self).__init__(message) -class NoStoredPasswordAvailable(PyiCloudException): +class PyiCloudNoStoredPasswordAvailableException(PyiCloudException): pass -class PyiCloudServiceNotActivatedErrror(PyiCloudAPIResponseError): +class PyiCloudServiceNotActivatedException(PyiCloudAPIResponseException): pass diff --git a/pyicloud/services/photos.py b/pyicloud/services/photos.py index e2dc74b..9a5f777 100644 --- a/pyicloud/services/photos.py +++ b/pyicloud/services/photos.py @@ -4,7 +4,7 @@ import logging import base64 from datetime import datetime -from pyicloud.exceptions import PyiCloudServiceNotActivatedErrror +from pyicloud.exceptions import PyiCloudServiceNotActivatedException import pytz from future.moves.urllib.parse import urlencode @@ -159,7 +159,7 @@ class PhotosService(object): response = request.json() indexing_state = response['records'][0]['fields']['state']['value'] if indexing_state != 'FINISHED': - raise PyiCloudServiceNotActivatedErrror( + raise PyiCloudServiceNotActivatedException( ('iCloud Photo Library not finished indexing. Please try ' 'again in a few minutes'), None) diff --git a/pyicloud/utils.py b/pyicloud/utils.py index 378b52c..b7da91d 100644 --- a/pyicloud/utils.py +++ b/pyicloud/utils.py @@ -2,7 +2,7 @@ import getpass import keyring import sys -from .exceptions import NoStoredPasswordAvailable +from .exceptions import PyiCloudNoStoredPasswordAvailableException KEYRING_SYSTEM = 'pyicloud://icloud-password' @@ -11,7 +11,7 @@ KEYRING_SYSTEM = 'pyicloud://icloud-password' def get_password(username, interactive=sys.stdout.isatty()): try: return get_password_from_keyring(username) - except NoStoredPasswordAvailable: + except PyiCloudNoStoredPasswordAvailableException: if not interactive: raise @@ -25,7 +25,7 @@ def get_password(username, interactive=sys.stdout.isatty()): def password_exists_in_keyring(username): try: get_password_from_keyring(username) - except NoStoredPasswordAvailable: + except PyiCloudNoStoredPasswordAvailableException: return False return True @@ -37,7 +37,7 @@ def get_password_from_keyring(username): username ) if result is None: - raise NoStoredPasswordAvailable( + raise PyiCloudNoStoredPasswordAvailableException( "No pyicloud password for {username} could be found " "in the system keychain. Use the `--store-in-keyring` " "command-line option for storing a password for this "