aboutsummaryrefslogtreecommitdiff
path: root/pandora/transport.py
diff options
context:
space:
mode:
Diffstat (limited to 'pandora/transport.py')
-rw-r--r--pandora/transport.py35
1 files changed, 21 insertions, 14 deletions
diff --git a/pandora/transport.py b/pandora/transport.py
index edec8a8..84f153b 100644
--- a/pandora/transport.py
+++ b/pandora/transport.py
@@ -40,6 +40,7 @@ def retries(max_tries, exceptions=(Exception,)):
40 function will only be retried if it raises one of the specified 40 function will only be retried if it raises one of the specified
41 exceptions. 41 exceptions.
42 """ 42 """
43
43 def decorator(func): 44 def decorator(func):
44 def function(*args, **kwargs): 45 def function(*args, **kwargs):
45 46
@@ -55,8 +56,9 @@ def retries(max_tries, exceptions=(Exception,)):
55 if isinstance(exc, PandoraException): 56 if isinstance(exc, PandoraException):
56 raise 57 raise
57 if retries_left > 0: 58 if retries_left > 0:
58 time.sleep(delay_exponential( 59 time.sleep(
59 0.5, 2, max_tries - retries_left)) 60 delay_exponential(0.5, 2, max_tries - retries_left)
61 )
60 else: 62 else:
61 raise 63 raise
62 64
@@ -76,11 +78,12 @@ def delay_exponential(base, growth_factor, attempts):
76 Base must be greater than 0, otherwise a ValueError will be 78 Base must be greater than 0, otherwise a ValueError will be
77 raised. 79 raised.
78 """ 80 """
79 if base == 'rand': 81 if base == "rand":
80 base = random.random() 82 base = random.random()
81 elif base <= 0: 83 elif base <= 0:
82 raise ValueError("The 'base' param must be greater than 0, " 84 raise ValueError(
83 "got: {}".format(base)) 85 "The 'base' param must be greater than 0, got: {}".format(base)
86 )
84 time_to_sleep = base * (growth_factor ** (attempts - 1)) 87 time_to_sleep = base * (growth_factor ** (attempts - 1))
85 return time_to_sleep 88 return time_to_sleep
86 89
@@ -95,8 +98,8 @@ class RetryingSession(requests.Session):
95 98
96 def __init__(self): 99 def __init__(self):
97 super().__init__() 100 super().__init__()
98 self.mount('https://', HTTPAdapter(max_retries=3)) 101 self.mount("https://", HTTPAdapter(max_retries=3))
99 self.mount('http://', HTTPAdapter(max_retries=3)) 102 self.mount("http://", HTTPAdapter(max_retries=3))
100 103
101 104
102class APITransport: 105class APITransport:
@@ -109,10 +112,14 @@ class APITransport:
109 112
110 API_VERSION = "5" 113 API_VERSION = "5"
111 114
112 REQUIRE_RESET = ("auth.partnerLogin", ) 115 REQUIRE_RESET = ("auth.partnerLogin",)
113 NO_ENCRYPT = ("auth.partnerLogin", ) 116 NO_ENCRYPT = ("auth.partnerLogin",)
114 REQUIRE_TLS = ("auth.partnerLogin", "auth.userLogin", 117 REQUIRE_TLS = (
115 "station.getPlaylist", "user.createUser") 118 "auth.partnerLogin",
119 "auth.userLogin",
120 "station.getPlaylist",
121 "user.createUser",
122 )
116 123
117 def __init__(self, cryptor, api_host=DEFAULT_API_HOST, proxy=None): 124 def __init__(self, cryptor, api_host=DEFAULT_API_HOST, proxy=None):
118 self.cryptor = cryptor 125 self.cryptor = cryptor
@@ -199,8 +206,8 @@ class APITransport:
199 206
200 def _build_url(self, method): 207 def _build_url(self, method):
201 return "{}://{}".format( 208 return "{}://{}".format(
202 "https" if method in self.REQUIRE_TLS else "http", 209 "https" if method in self.REQUIRE_TLS else "http", self.api_host
203 self.api_host) 210 )
204 211
205 def _build_data(self, method, data): 212 def _build_data(self, method, data):
206 data["userAuthToken"] = self.user_auth_token 213 data["userAuthToken"] = self.user_auth_token
@@ -260,7 +267,7 @@ class BlowfishCryptor:
260 267
261 computed = b"".join([chr(pad_size).encode("ascii")] * pad_size) 268 computed = b"".join([chr(pad_size).encode("ascii")] * pad_size)
262 if not data[-pad_size:] == computed: 269 if not data[-pad_size:] == computed:
263 raise ValueError('Invalid padding') 270 raise ValueError("Invalid padding")
264 271
265 return data[:-pad_size] 272 return data[:-pad_size]
266 273