mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-06-19 01:03:53 +00:00
Refactor SpotifyOauthError class
- add error and error description to SpotifyOauthError to reflect the data returned by web api when error happens - unit test to SpotifyClientCredentials.get_access_token is added for invalid client
This commit is contained in:
parent
5c981a1d62
commit
901b2031a2
@ -29,7 +29,10 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SpotifyOauthError(Exception):
|
||||
pass
|
||||
def __init__(self, message, error=None, error_description=None, *args, **kwargs):
|
||||
self.error = error
|
||||
self.error_description = error_description
|
||||
super().__init__(message, *args, **kwargs)
|
||||
|
||||
|
||||
def _make_authorization_headers(client_id, client_secret):
|
||||
@ -165,7 +168,12 @@ class SpotifyClientCredentials(SpotifyAuthBase):
|
||||
timeout=self.requests_timeout,
|
||||
)
|
||||
if response.status_code != 200:
|
||||
raise SpotifyOauthError(response.reason)
|
||||
error_payload = response.json()
|
||||
raise SpotifyOauthError(
|
||||
'error: {0}, error_description: {1}'.format(
|
||||
error_payload['error'], error_payload['error_description']),
|
||||
error=error_payload['error'],
|
||||
error_description=error_payload['error_description'])
|
||||
token_info = response.json()
|
||||
return token_info
|
||||
|
||||
@ -431,7 +439,12 @@ class SpotifyOAuth(SpotifyAuthBase):
|
||||
timeout=self.requests_timeout,
|
||||
)
|
||||
if response.status_code != 200:
|
||||
raise SpotifyOauthError(response.reason)
|
||||
error_payload = response.json()
|
||||
raise SpotifyOauthError(
|
||||
'error: {0}, error_description: {1}'.format(
|
||||
error_payload['error'], error_payload['error_description']),
|
||||
error=error_payload['error'],
|
||||
error_description=error_payload['error_description'])
|
||||
token_info = response.json()
|
||||
token_info = self._add_custom_values_to_token_info(token_info)
|
||||
self._save_token_info(token_info)
|
||||
|
||||
@ -6,6 +6,7 @@ import unittest
|
||||
import six.moves.urllib.parse as urllibparse
|
||||
|
||||
from spotipy import SpotifyOAuth
|
||||
from spotipy.oauth2 import SpotifyClientCredentials, SpotifyOauthError
|
||||
|
||||
try:
|
||||
import unittest.mock as mock
|
||||
@ -166,3 +167,9 @@ class TestSpotifyOAuth(unittest.TestCase):
|
||||
parsed_url = urllibparse.urlparse(url)
|
||||
parsed_qs = urllibparse.parse_qs(parsed_url.query)
|
||||
self.assertTrue(parsed_qs['show_dialog'])
|
||||
|
||||
def test_spotify_client_credentials_get_access_token(self):
|
||||
oauth = SpotifyClientCredentials(client_id='ID', client_secret='SECRET')
|
||||
with self.assertRaises(SpotifyOauthError) as error:
|
||||
oauth.get_access_token()
|
||||
self.assertEqual(error.exception.error, 'invalid_client')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user