From f42801ae90c26dabca3fe4f4c8de7bf164d93e8a Mon Sep 17 00:00:00 2001 From: LoisaidaSam Date: Fri, 23 Feb 2018 13:37:59 -0500 Subject: [PATCH] Propagate refresh token error #259 https://github.com/plamere/spotipy/issues/259 (before upstream support) --- spotipy/__init__.py | 3 ++- spotipy/client.py | 18 ++---------------- spotipy/exceptions.py | 16 ++++++++++++++++ spotipy/oauth2.py | 20 +++++++++++++------- 4 files changed, 33 insertions(+), 24 deletions(-) create mode 100644 spotipy/exceptions.py diff --git a/spotipy/__init__.py b/spotipy/__init__.py index 9be1dce..8f9c66f 100644 --- a/spotipy/__init__.py +++ b/spotipy/__init__.py @@ -1,2 +1,3 @@ VERSION='2.0.1' -from .client import Spotify, SpotifyException +from .client import Spotify +from .exceptions import SpotifyException \ No newline at end of file diff --git a/spotipy/client.py b/spotipy/client.py index 06858b9..a63c8cb 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -9,26 +9,12 @@ import time import six +from exceptions import SpotifyException + """ A simple and thin Python library for the Spotify Web API """ -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) - - class Spotify(object): """ Example usage:: diff --git a/spotipy/exceptions.py b/spotipy/exceptions.py new file mode 100644 index 0000000..5ba1ac6 --- /dev/null +++ b/spotipy/exceptions.py @@ -0,0 +1,16 @@ + + +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) diff --git a/spotipy/oauth2.py b/spotipy/oauth2.py index cef7908..9090207 100644 --- a/spotipy/oauth2.py +++ b/spotipy/oauth2.py @@ -11,6 +11,8 @@ import sys import six import six.moves.urllib.parse as urllibparse +from exceptions import SpotifyException + class SpotifyOauthError(Exception): pass @@ -236,13 +238,17 @@ class SpotifyOAuth(object): response = requests.post(self.OAUTH_TOKEN_URL, data=payload, headers=headers, proxies=self.proxies) - 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)) - return None + try: + response.raise_for_status() + except: + message = "Couldn't refresh token: code:%d reason:%s" % ( + response.status_code, + response.reason, + ) + raise SpotifyException(response.status_code, + -1, + message, + headers) token_info = response.json() token_info = self._add_custom_values_to_token_info(token_info) if not 'refresh_token' in token_info: