From 96e9d3b7dfa10897323fab0c27f44d26ab9e4f70 Mon Sep 17 00:00:00 2001 From: Sebastian Bischoff Date: Tue, 23 Aug 2016 16:48:49 +0200 Subject: [PATCH] Added support for proxy --- spotipy/client.py | 7 +++++-- spotipy/oauth2.py | 9 +++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/spotipy/client.py b/spotipy/client.py index c3b4b69..d497176 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -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() diff --git a/spotipy/oauth2.py b/spotipy/oauth2.py index bb10e32..cc52e63 100644 --- a/spotipy/oauth2.py +++ b/spotipy/oauth2.py @@ -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)