Add session in SpotifyAuthBase (#383)

Co-authored-by: XIS01536 <apozadel@extern.isban.es>
Co-authored-by: Stephane Bruckert <contact@stephanebruckert.com>
This commit is contained in:
AlPoza 2020-04-02 00:02:56 +02:00 committed by GitHub
parent 6d7fe6c195
commit 5b5b30dd0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 9 deletions

View File

@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- status_retries - status_retries
- backoff_factor - backoff_factor
- Spin up a local webserver to auto-fill authentication URL - Spin up a local webserver to auto-fill authentication URL
- Use session in SpotifyAuthBase
### Fixed ### Fixed

View File

@ -134,6 +134,11 @@ class Spotify(object):
self.client_credentials_manager or self.oauth_manager 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): def _build_session(self):
self._session = requests.Session() self._session = requests.Session()
retry = urllib3.Retry( retry = urllib3.Retry(
@ -148,11 +153,6 @@ class Spotify(object):
self._session.mount('http://', adapter) self._session.mount('http://', adapter)
self._session.mount('https://', 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): def _auth_headers(self):
if self._auth: if self._auth:
return {"Authorization": "Bearer {0}".format(self._auth)} return {"Authorization": "Bearer {0}".format(self._auth)}

View File

@ -55,6 +55,16 @@ def _ensure_value(value, env_key):
class SpotifyAuthBase(object): 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 @property
def client_id(self): def client_id(self):
return self._client_id return self._client_id
@ -79,17 +89,29 @@ class SpotifyAuthBase(object):
def redirect_uri(self, val): def redirect_uri(self, val):
self._redirect_uri = _ensure_value(val, "redirect_uri") 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): class SpotifyClientCredentials(SpotifyAuthBase):
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, 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 You can either provide 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
environment variables environment variables
""" """
super(self.__class__, self).__init__(requests_session)
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
@ -132,7 +154,7 @@ class SpotifyClientCredentials(SpotifyAuthBase):
self.client_id, self.client_secret self.client_id, self.client_secret
) )
response = requests.post( response = self._session.post(
self.OAUTH_TOKEN_URL, self.OAUTH_TOKEN_URL,
data=payload, data=payload,
headers=headers, headers=headers,
@ -176,6 +198,7 @@ class SpotifyOAuth(SpotifyAuthBase):
username=None, username=None,
proxies=None, proxies=None,
show_dialog=False, show_dialog=False,
requests_session=True,
requests_timeout=None requests_timeout=None
): ):
""" """
@ -193,6 +216,8 @@ class SpotifyOAuth(SpotifyAuthBase):
- username - username of current client - username - username of current client
""" """
super(self.__class__, self).__init__(requests_session)
self.client_id = client_id self.client_id = client_id
self.client_secret = client_secret self.client_secret = client_secret
self.redirect_uri = redirect_uri self.redirect_uri = redirect_uri
@ -399,7 +424,7 @@ class SpotifyOAuth(SpotifyAuthBase):
headers = self._make_authorization_headers() headers = self._make_authorization_headers()
response = requests.post( response = self._session.post(
self.OAUTH_TOKEN_URL, self.OAUTH_TOKEN_URL,
data=payload, data=payload,
headers=headers, headers=headers,
@ -429,7 +454,7 @@ class SpotifyOAuth(SpotifyAuthBase):
headers = self._make_authorization_headers() headers = self._make_authorization_headers()
response = requests.post( response = self._session.post(
self.OAUTH_TOKEN_URL, self.OAUTH_TOKEN_URL,
data=payload, data=payload,
headers=headers, headers=headers,