Skip to content

Commit 85c584e

Browse files
use custom exception for 503 on all responses (#1194)
1 parent 75f40b0 commit 85c584e

File tree

5 files changed

+16
-21
lines changed

5 files changed

+16
-21
lines changed

src/icloudpd/base.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
from icloudpd.xmp_sidecar import generate_xmp_file
5656
from pyicloud_ipd.base import PyiCloudService
5757
from pyicloud_ipd.exceptions import (
58-
PyiCloudAPIResponseException,
5958
PyiCloudServiceNotActivatedException,
59+
PyiCloudServiceUnavailableException,
6060
)
6161
from pyicloud_ipd.file_match import FileMatchPolicy
6262
from pyicloud_ipd.raw_policy import RawTreatmentPolicy
@@ -1503,18 +1503,8 @@ def should_break(counter: Counter) -> bool:
15031503
)
15041504
else:
15051505
pass
1506-
except PyiCloudAPIResponseException as error:
1507-
if error.code == "503":
1508-
logger.info("Apple iCloud is temporary refusing to serve icloudpd")
1509-
# it not watching then return error
1510-
if not watch_interval:
1511-
return 1
1512-
else:
1513-
pass
1514-
else:
1515-
raise
1516-
except PyiCloudServiceNotActivatedException as error:
1517-
logger.info(error.reason)
1506+
except (PyiCloudServiceNotActivatedException, PyiCloudServiceUnavailableException) as error:
1507+
logger.info(error)
15181508
# it not watching then return error
15191509
if not watch_interval:
15201510
return 1

src/pyicloud_ipd/base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,6 @@ def encode(self) -> bytes:
272272
if response.status_code == 401:
273273
raise PyiCloudAPIResponseException(response.text, str(response.status_code))
274274
except PyiCloudAPIResponseException as error:
275-
if error.code == "503":
276-
raise
277275
msg = "Failed to initiate srp authentication."
278276
raise PyiCloudFailedLoginException(msg, error) from error
279277

src/pyicloud_ipd/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class PyiCloudServiceNotActivatedException(PyiCloudAPIResponseException):
2626
"""iCloud service not activated exception."""
2727
pass
2828

29+
class PyiCloudServiceUnavailableException(PyiCloudException):
30+
"""iCloud service not available (503)"""
31+
pass
2932

3033
# Login
3134
class PyiCloudFailedLoginException(PyiCloudException):

src/pyicloud_ipd/session.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
PyiCloud2SARequiredException,
1212
PyiCloudServiceNotActivatedException,
1313
)
14+
from pyicloud_ipd.utils import throw_on_503
1415

1516
LOGGER = logging.getLogger(__name__)
1617

@@ -63,17 +64,13 @@ def request(self, method: str, url, **kwargs): # type: ignore
6364
kwargs.pop("retried", None)
6465
if "timeout" not in kwargs and self.service.http_timeout is not None:
6566
kwargs["timeout"] = self.service.http_timeout
66-
response = super().request(method, url, **kwargs)
67+
response = throw_on_503(super().request(method, url, **kwargs))
6768

6869
content_type = response.headers.get("Content-Type", "").split(";")[0]
6970
json_mimetypes = ["application/json", "text/json"]
7071

7172
request_logger.debug(response.headers)
7273

73-
if response.status_code == 503:
74-
api_error = PyiCloudAPIResponseException(response.reason, str(response.status_code))
75-
raise api_error
76-
7774
for header, value in HEADER_DATA.items():
7875
if response.headers.get(header):
7976
session_arg = value

src/pyicloud_ipd/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
from typing import Dict, Optional, Sequence
44
import typing
55
import keyring
6+
from requests import Response
67

78
from pyicloud_ipd.asset_version import AssetVersion
89
from pyicloud_ipd.version_size import AssetVersionSize, VersionSize
910

10-
from .exceptions import PyiCloudNoStoredPasswordAvailableException
11+
from .exceptions import PyiCloudNoStoredPasswordAvailableException, PyiCloudServiceUnavailableException
1112

1213
KEYRING_SYSTEM = 'pyicloud://icloud-password'
1314

@@ -135,3 +136,9 @@ def disambiguate_filenames(_versions: Dict[VersionSize, AssetVersion], _sizes:Se
135136

136137

137138
return _results
139+
140+
def throw_on_503(response: Response) -> Response:
141+
if response.status_code == 503:
142+
raise PyiCloudServiceUnavailableException("Apple iCloud is temporary refusing to serve icloudpd")
143+
else:
144+
return response

0 commit comments

Comments
 (0)