mirror of
https://github.com/c0de-archive/spotipy.git
synced 2025-01-08 15:42:48 +00:00
Use Requests sessions for connection pooling.
This behavior is on by default but can be easily disabled.
This commit is contained in:
parent
6b9d28de01
commit
62ced74126
@ -38,21 +38,32 @@ class Spotify(object):
|
|||||||
print(user)
|
print(user)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
trace = False
|
trace = False # Enable tracing?
|
||||||
"""enable tracing"""
|
|
||||||
|
|
||||||
_auth = None
|
def __init__(self, auth=None, requests_session=True):
|
||||||
|
|
||||||
def __init__(self, auth=None):
|
|
||||||
'''
|
'''
|
||||||
creates a spotify object
|
Create a Spotify API object.
|
||||||
|
|
||||||
|
:param auth: An authorization token (optional)
|
||||||
|
:param requests_session:
|
||||||
|
A Requests session object or a truthy value to create one.
|
||||||
|
A falsy value disables sessions.
|
||||||
|
It should generally be a good idea to keep sessions enabled
|
||||||
|
for performance reasons (connection pooling).
|
||||||
|
|
||||||
Parameters:
|
|
||||||
- auth - the optional authorization token
|
|
||||||
'''
|
'''
|
||||||
self.prefix = 'https://api.spotify.com/v1/'
|
self.prefix = 'https://api.spotify.com/v1/'
|
||||||
self._auth = auth
|
self._auth = auth
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
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)}
|
||||||
@ -67,10 +78,9 @@ class Spotify(object):
|
|||||||
headers['Content-Type'] = 'application/json'
|
headers['Content-Type'] = 'application/json'
|
||||||
|
|
||||||
if payload:
|
if payload:
|
||||||
r = requests.request(method, url, headers=headers,
|
args["data"] = json.dumps(payload)
|
||||||
data=json.dumps(payload), **args)
|
|
||||||
else:
|
r = self._session.request(method, url, headers=headers, **args)
|
||||||
r = requests.request(method, url, headers=headers, **args)
|
|
||||||
|
|
||||||
if self.trace:
|
if self.trace:
|
||||||
print()
|
print()
|
||||||
|
Loading…
Reference in New Issue
Block a user