From 5b5b30dd0fbce05f17f55c2673a6de6c775cfd0a Mon Sep 17 00:00:00 2001 From: AlPoza Date: Thu, 2 Apr 2020 00:02:56 +0200 Subject: [PATCH] Add session in SpotifyAuthBase (#383) Co-authored-by: XIS01536 Co-authored-by: Stephane Bruckert --- CHANGELOG.md | 1 + spotipy/client.py | 10 +++++----- spotipy/oauth2.py | 33 +++++++++++++++++++++++++++++---- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 444cbf2..d9453b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - status_retries - backoff_factor - Spin up a local webserver to auto-fill authentication URL + - Use session in SpotifyAuthBase ### Fixed diff --git a/spotipy/client.py b/spotipy/client.py index 66539d6..d6d12d5 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -134,6 +134,11 @@ class Spotify(object): self.client_credentials_manager or self.oauth_manager ) + def __del__(self): + """Make sure the connection (pool) gets closed""" + if isinstance(self._session, requests.Session): + self._session.close() + def _build_session(self): self._session = requests.Session() retry = urllib3.Retry( @@ -148,11 +153,6 @@ class Spotify(object): self._session.mount('http://', adapter) self._session.mount('https://', adapter) - def __del__(self): - """Make sure the connection (pool) gets closed""" - if isinstance(self._session, requests.Session): - self._session.close() - def _auth_headers(self): if self._auth: return {"Authorization": "Bearer {0}".format(self._auth)} diff --git a/spotipy/oauth2.py b/spotipy/oauth2.py index 483271f..6271a72 100644 --- a/spotipy/oauth2.py +++ b/spotipy/oauth2.py @@ -55,6 +55,16 @@ def _ensure_value(value, env_key): class SpotifyAuthBase(object): + def __init__(self, requests_session): + if isinstance(requests_session, requests.Session): + self._session = requests_session + else: + if requests_session: # Build a new session. + self._session = requests.Session() + else: # Use the Requests API module as a "session". + from requests import api + self._session = api + @property def client_id(self): return self._client_id @@ -79,17 +89,29 @@ class SpotifyAuthBase(object): def redirect_uri(self, val): self._redirect_uri = _ensure_value(val, "redirect_uri") + def __del__(self): + """Make sure the connection (pool) gets closed""" + if isinstance(self._session, requests.Session): + self._session.close() + class SpotifyClientCredentials(SpotifyAuthBase): OAUTH_TOKEN_URL = "https://accounts.spotify.com/api/token" - def __init__(self, client_id=None, client_secret=None, proxies=None, requests_timeout=None): + def __init__(self, + client_id=None, + client_secret=None, + proxies=None, + requests_session=True, + requests_timeout=None): """ You can either provide a client_id and client_secret to the constructor or set SPOTIPY_CLIENT_ID and SPOTIPY_CLIENT_SECRET environment variables """ + super(self.__class__, self).__init__(requests_session) + self.client_id = client_id self.client_secret = client_secret self.token_info = None @@ -132,7 +154,7 @@ class SpotifyClientCredentials(SpotifyAuthBase): self.client_id, self.client_secret ) - response = requests.post( + response = self._session.post( self.OAUTH_TOKEN_URL, data=payload, headers=headers, @@ -176,6 +198,7 @@ class SpotifyOAuth(SpotifyAuthBase): username=None, proxies=None, show_dialog=False, + requests_session=True, requests_timeout=None ): """ @@ -193,6 +216,8 @@ class SpotifyOAuth(SpotifyAuthBase): - username - username of current client """ + super(self.__class__, self).__init__(requests_session) + self.client_id = client_id self.client_secret = client_secret self.redirect_uri = redirect_uri @@ -399,7 +424,7 @@ class SpotifyOAuth(SpotifyAuthBase): headers = self._make_authorization_headers() - response = requests.post( + response = self._session.post( self.OAUTH_TOKEN_URL, data=payload, headers=headers, @@ -429,7 +454,7 @@ class SpotifyOAuth(SpotifyAuthBase): headers = self._make_authorization_headers() - response = requests.post( + response = self._session.post( self.OAUTH_TOKEN_URL, data=payload, headers=headers,