Added support for proxy

This commit is contained in:
Sebastian Bischoff 2016-08-23 16:48:49 +02:00
parent 3f87a8b879
commit 96e9d3b7df
2 changed files with 10 additions and 6 deletions

View File

@ -45,7 +45,7 @@ class Spotify(object):
max_get_retries = 10 max_get_retries = 10
def __init__(self, auth=None, requests_session=True, def __init__(self, auth=None, requests_session=True,
client_credentials_manager=None): client_credentials_manager=None, proxies=None):
''' '''
Create a Spotify API object. Create a Spotify API object.
@ -57,11 +57,14 @@ class Spotify(object):
for performance reasons (connection pooling). for performance reasons (connection pooling).
:param client_credentials_manager: :param client_credentials_manager:
SpotifyClientCredentials object SpotifyClientCredentials object
:param proxies:
Definition of proxies (optional)
''' '''
self.prefix = 'https://api.spotify.com/v1/' self.prefix = 'https://api.spotify.com/v1/'
self._auth = auth self._auth = auth
self.client_credentials_manager = client_credentials_manager self.client_credentials_manager = client_credentials_manager
self.proxies = proxies
if isinstance(requests_session, requests.Session): if isinstance(requests_session, requests.Session):
self._session = requests_session self._session = requests_session
@ -93,7 +96,7 @@ class Spotify(object):
if self.trace_out: if self.trace_out:
print(url) print(url)
r = self._session.request(method, url, headers=headers, **args) r = self._session.request(method, url, headers=headers, proxies=self.proxies, **args)
if self.trace: # pragma: no cover if self.trace: # pragma: no cover
print() print()

View File

@ -23,7 +23,7 @@ class SpotifyOauthError(Exception):
class SpotifyClientCredentials(object): class SpotifyClientCredentials(object):
OAUTH_TOKEN_URL = 'https://accounts.spotify.com/api/token' OAUTH_TOKEN_URL = 'https://accounts.spotify.com/api/token'
def __init__(self, client_id=None, client_secret=None): def __init__(self, client_id=None, client_secret=None, proxies=None):
""" """
You can either provid a client_id and client_secret to the You can either provid a client_id and client_secret to the
constructor or set SPOTIPY_CLIENT_ID and SPOTIPY_CLIENT_SECRET constructor or set SPOTIPY_CLIENT_ID and SPOTIPY_CLIENT_SECRET
@ -44,6 +44,7 @@ class SpotifyClientCredentials(object):
self.client_id = client_id self.client_id = client_id
self.client_secret = client_secret self.client_secret = client_secret
self.token_info = None self.token_info = None
self.proxies = proxies
def get_access_token(self): def get_access_token(self):
""" """
@ -70,7 +71,7 @@ class SpotifyClientCredentials(object):
headers = {'Authorization': 'Basic %s' % auth_header} headers = {'Authorization': 'Basic %s' % auth_header}
response = requests.post(self.OAUTH_TOKEN_URL, data=payload, response = requests.post(self.OAUTH_TOKEN_URL, data=payload,
headers=headers, verify=True) headers=headers, verify=True, proxies=self.proxies)
if response.status_code is not 200: if response.status_code is not 200:
raise SpotifyOauthError(response.reason) raise SpotifyOauthError(response.reason)
token_info = response.json() token_info = response.json()
@ -205,7 +206,7 @@ class SpotifyOAuth(object):
headers = {'Authorization': 'Basic %s' % auth_header} headers = {'Authorization': 'Basic %s' % auth_header}
response = requests.post(self.OAUTH_TOKEN_URL, data=payload, response = requests.post(self.OAUTH_TOKEN_URL, data=payload,
headers=headers, verify=True) headers=headers, verify=True, proxies=self.proxies)
if response.status_code is not 200: if response.status_code is not 200:
raise SpotifyOauthError(response.reason) raise SpotifyOauthError(response.reason)
token_info = response.json() token_info = response.json()
@ -233,7 +234,7 @@ class SpotifyOAuth(object):
headers = {'Authorization': 'Basic %s' % auth_header} headers = {'Authorization': 'Basic %s' % auth_header}
response = requests.post(self.OAUTH_TOKEN_URL, data=payload, response = requests.post(self.OAUTH_TOKEN_URL, data=payload,
headers=headers) headers=headers, proxies=self.proxies)
if response.status_code != 200: if response.status_code != 200:
if False: # debugging code if False: # debugging code
print('headers', headers) print('headers', headers)