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
def __init__(self, auth=None, requests_session=True,
client_credentials_manager=None):
client_credentials_manager=None, proxies=None):
'''
Create a Spotify API object.
@ -57,11 +57,14 @@ class Spotify(object):
for performance reasons (connection pooling).
:param client_credentials_manager:
SpotifyClientCredentials object
:param proxies:
Definition of proxies (optional)
'''
self.prefix = 'https://api.spotify.com/v1/'
self._auth = auth
self.client_credentials_manager = client_credentials_manager
self.proxies = proxies
if isinstance(requests_session, requests.Session):
self._session = requests_session
@ -93,7 +96,7 @@ class Spotify(object):
if self.trace_out:
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
print()

View File

@ -23,7 +23,7 @@ class SpotifyOauthError(Exception):
class SpotifyClientCredentials(object):
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
constructor or set SPOTIPY_CLIENT_ID and SPOTIPY_CLIENT_SECRET
@ -44,6 +44,7 @@ class SpotifyClientCredentials(object):
self.client_id = client_id
self.client_secret = client_secret
self.token_info = None
self.proxies = proxies
def get_access_token(self):
"""
@ -70,7 +71,7 @@ class SpotifyClientCredentials(object):
headers = {'Authorization': 'Basic %s' % auth_header}
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:
raise SpotifyOauthError(response.reason)
token_info = response.json()
@ -205,7 +206,7 @@ class SpotifyOAuth(object):
headers = {'Authorization': 'Basic %s' % auth_header}
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:
raise SpotifyOauthError(response.reason)
token_info = response.json()
@ -233,7 +234,7 @@ class SpotifyOAuth(object):
headers = {'Authorization': 'Basic %s' % auth_header}
response = requests.post(self.OAUTH_TOKEN_URL, data=payload,
headers=headers)
headers=headers, proxies=self.proxies)
if response.status_code != 200:
if False: # debugging code
print('headers', headers)