From 72a2fe489dc8d9750ee993335b75cea8b65c52d6 Mon Sep 17 00:00:00 2001 From: Michael Birtwell Date: Sat, 7 Jan 2017 15:18:46 +0000 Subject: [PATCH] Use six to simplify python 2/3 compatibilty code where possible --- requirements.txt | 1 + setup.py | 5 ++++- spotipy/client.py | 9 +++------ spotipy/oauth2.py | 37 +++++++++++++------------------------ tox.ini | 1 + 5 files changed, 22 insertions(+), 31 deletions(-) diff --git a/requirements.txt b/requirements.txt index 65b84e9..47f25d8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ requests==2.3.0 +six==1.10.0 \ No newline at end of file diff --git a/setup.py b/setup.py index ae169d5..20cfdfe 100644 --- a/setup.py +++ b/setup.py @@ -7,6 +7,9 @@ setup( author="@plamere", author_email="paul@echonest.com", url='http://spotipy.readthedocs.org/', - install_requires=['requests>=1.0'], + install_requires=[ + 'requests>=1.0', + 'six>=1.10.0', + ], license='LICENSE.txt', packages=['spotipy']) diff --git a/spotipy/client.py b/spotipy/client.py index d2542ab..6f5dae7 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -7,6 +7,8 @@ import requests import json import time +import six + ''' A simple and thin Python library for the Spotify Web API ''' @@ -418,12 +420,7 @@ class Spotify(object): - collaborative - optional is the playlist collaborative ''' data = {} - # Add Python2 and Python3 compatibility checking string types - try: - basestring - except NameError: - basestring = str - if isinstance(name, basestring): + if isinstance(name, six.string_types): data['name'] = name if isinstance(public, bool): data['public'] = public diff --git a/spotipy/oauth2.py b/spotipy/oauth2.py index 0d30bd3..3ea17c8 100644 --- a/spotipy/oauth2.py +++ b/spotipy/oauth2.py @@ -8,18 +8,19 @@ import time import sys # Workaround to support both python 2 & 3 -try: - import urllib.request, urllib.error - import urllib.parse as urllibparse -except ImportError: - import urllib as urllibparse - +import six +import six.moves.urllib.parse as urllibparse class SpotifyOauthError(Exception): pass +def _make_authorization_headers(client_id, client_secret): + auth_header = base64.b64encode(six.text_type(client_id + ':' + client_secret).encode('ascii')) + return {'Authorization': 'Basic %s' % auth_header.decode('ascii')} + + class SpotifyClientCredentials(object): OAUTH_TOKEN_URL = 'https://accounts.spotify.com/api/token' @@ -63,12 +64,7 @@ class SpotifyClientCredentials(object): """Gets client credentials access token """ payload = { 'grant_type': 'client_credentials'} - if sys.version_info[0] >= 3: # Python 3 - auth_header = base64.b64encode(str(self.client_id + ':' + self.client_secret).encode()) - headers = {'Authorization': 'Basic %s' % auth_header.decode()} - else: # Python 2 - auth_header = base64.b64encode(self.client_id + ':' + self.client_secret) - headers = {'Authorization': 'Basic %s' % auth_header} + headers = _make_authorization_headers(self.client_id, self.client_secret) response = requests.post(self.OAUTH_TOKEN_URL, data=payload, headers=headers, verify=True, proxies=self.proxies) @@ -189,6 +185,9 @@ class SpotifyOAuth(object): except IndexError: return None + def _make_authorization_headers(self): + return _make_authorization_headers(self.client_id, self.client_secret) + def get_access_token(self, code): """ Gets the access token for the app given the code @@ -204,12 +203,7 @@ class SpotifyOAuth(object): if self.state: payload['state'] = self.state - if sys.version_info[0] >= 3: # Python 3 - auth_header = base64.b64encode(str(self.client_id + ':' + self.client_secret).encode()) - headers = {'Authorization': 'Basic %s' % auth_header.decode()} - else: # Python 2 - auth_header = base64.b64encode(self.client_id + ':' + self.client_secret) - headers = {'Authorization': 'Basic %s' % auth_header} + headers = self._make_authorization_headers() response = requests.post(self.OAUTH_TOKEN_URL, data=payload, headers=headers, verify=True, proxies=self.proxies) @@ -232,12 +226,7 @@ class SpotifyOAuth(object): payload = { 'refresh_token': refresh_token, 'grant_type': 'refresh_token'} - if sys.version_info[0] >= 3: # Python 3 - auth_header = base64.b64encode(str(self.client_id + ':' + self.client_secret).encode()) - headers = {'Authorization': 'Basic %s' % auth_header.decode()} - else: # Python 2 - auth_header = base64.b64encode(self.client_id + ':' + self.client_secret) - headers = {'Authorization': 'Basic %s' % auth_header} + headers = self._make_authorization_headers() response = requests.post(self.OAUTH_TOKEN_URL, data=payload, headers=headers, proxies=self.proxies) diff --git a/tox.ini b/tox.ini index 705b07e..e3af7ee 100644 --- a/tox.ini +++ b/tox.ini @@ -3,5 +3,6 @@ envlist = py27,py34 [testenv] deps= requests + six py27: mock commands=python -m unittest discover tests