mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-06-19 09:13:53 +00:00
* Propagate refresh token error #259 https://github.com/plamere/spotipy/issues/259 * Lint * Format Co-authored-by: Stephane Bruckert <contact@stephanebruckert.com>
This commit is contained in:
parent
5b5b30dd0f
commit
ccfda079c8
@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
### Fixed
|
||||
|
||||
- Close session when Spotipy object is unloaded
|
||||
- Propagate refresh token error
|
||||
|
||||
## [2.10.0] - 2020-03-18
|
||||
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
from .client import * # noqa
|
||||
from .oauth2 import * # noqa
|
||||
from .util import * # noqa
|
||||
from .exceptions import * # noqa
|
||||
|
||||
@ -4,8 +4,6 @@
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
__all__ = ["Spotify", "SpotifyException"]
|
||||
|
||||
import json
|
||||
import sys
|
||||
import warnings
|
||||
@ -14,22 +12,7 @@ import requests
|
||||
import urllib3
|
||||
import six
|
||||
|
||||
|
||||
class SpotifyException(Exception):
|
||||
def __init__(self, http_status, code, msg, headers=None):
|
||||
self.http_status = http_status
|
||||
self.code = code
|
||||
self.msg = msg
|
||||
# `headers` is used to support `Retry-After` in the event of a
|
||||
# 429 status code.
|
||||
if headers is None:
|
||||
headers = {}
|
||||
self.headers = headers
|
||||
|
||||
def __str__(self):
|
||||
return "http status: {0}, code:{1} - {2}".format(
|
||||
self.http_status, self.code, self.msg
|
||||
)
|
||||
from spotipy.exceptions import SpotifyException
|
||||
|
||||
|
||||
class Spotify(object):
|
||||
|
||||
15
spotipy/exceptions.py
Normal file
15
spotipy/exceptions.py
Normal file
@ -0,0 +1,15 @@
|
||||
class SpotifyException(Exception):
|
||||
|
||||
def __init__(self, http_status, code, msg, headers=None):
|
||||
self.http_status = http_status
|
||||
self.code = code
|
||||
self.msg = msg
|
||||
# `headers` is used to support `Retry-After` in the event of a
|
||||
# 429 status code.
|
||||
if headers is None:
|
||||
headers = {}
|
||||
self.headers = headers
|
||||
|
||||
def __str__(self):
|
||||
return 'http status: {0}, code:{1} - {2}'.format(
|
||||
self.http_status, self.code, self.msg)
|
||||
@ -18,6 +18,7 @@ import warnings
|
||||
|
||||
import requests
|
||||
from spotipy.util import CLIENT_CREDS_ENV_VARS, get_host_port
|
||||
from spotipy.exceptions import SpotifyException
|
||||
|
||||
# Workaround to support both python 2 & 3
|
||||
import six
|
||||
@ -273,7 +274,6 @@ class SpotifyOAuth(SpotifyAuthBase):
|
||||
f.close()
|
||||
except IOError:
|
||||
self._warn("couldn't write token cache to " + self.cache_path)
|
||||
pass
|
||||
|
||||
def _is_scope_subset(self, needle_scope, haystack_scope):
|
||||
needle_scope = set(needle_scope.split()) if needle_scope else set()
|
||||
@ -461,15 +461,17 @@ class SpotifyOAuth(SpotifyAuthBase):
|
||||
proxies=self.proxies,
|
||||
timeout=self.requests_timeout,
|
||||
)
|
||||
if response.status_code != 200:
|
||||
if False: # debugging code
|
||||
print("headers", headers)
|
||||
print("request", response.url)
|
||||
self._warn(
|
||||
"couldn't refresh token: code:%d reason:%s"
|
||||
% (response.status_code, response.reason)
|
||||
try:
|
||||
response.raise_for_status()
|
||||
except BaseException:
|
||||
message = "Couldn't refresh token: code:%d reason:%s" % (
|
||||
response.status_code,
|
||||
response.reason,
|
||||
)
|
||||
return None
|
||||
raise SpotifyException(response.status_code,
|
||||
-1,
|
||||
message,
|
||||
headers)
|
||||
token_info = response.json()
|
||||
token_info = self._add_custom_values_to_token_info(token_info)
|
||||
if "refresh_token" not in token_info:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user