mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-06-19 01:03:53 +00:00
Merge pull request #269 from sonneveld/client-internal-call-refactor
Refactor client internal calls to allow keepalive
This commit is contained in:
commit
c2b6be6446
@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Changed
|
||||
|
||||
- Allow session keepalive
|
||||
|
||||
## [2.6.1] - 2020-01-13
|
||||
|
||||
### Fixed
|
||||
|
||||
@ -113,44 +113,39 @@ class Spotify(object):
|
||||
|
||||
if self.trace_out:
|
||||
print(url)
|
||||
r = self._session.request(
|
||||
method,
|
||||
url,
|
||||
headers=headers,
|
||||
proxies=self.proxies,
|
||||
**args)
|
||||
|
||||
with self._session.request(method, url, headers=headers,
|
||||
proxies=self.proxies, **args) as r:
|
||||
|
||||
if self.trace: # pragma: no cover
|
||||
print()
|
||||
print('Request headers:', headers)
|
||||
print('Response headers:', r.headers)
|
||||
print('HTTP status', r.status_code)
|
||||
print(method, r.url)
|
||||
if payload:
|
||||
print("Data", json.dumps(payload))
|
||||
|
||||
try:
|
||||
r.raise_for_status()
|
||||
except BaseException:
|
||||
try:
|
||||
msg = r.json()['error']['message']
|
||||
except BaseException:
|
||||
msg = 'error'
|
||||
raise SpotifyException(r.status_code,
|
||||
-1, '%s:\n %s' % (r.url, msg),
|
||||
headers=r.headers)
|
||||
|
||||
try:
|
||||
results = r.json()
|
||||
except BaseException:
|
||||
results = None
|
||||
|
||||
if self.trace: # pragma: no cover
|
||||
print('Response:', results)
|
||||
print()
|
||||
print('headers', headers)
|
||||
print('http status', r.status_code)
|
||||
print(method, r.url)
|
||||
if payload:
|
||||
print("DATA", json.dumps(payload))
|
||||
|
||||
try:
|
||||
r.raise_for_status()
|
||||
except BaseException:
|
||||
if r.text and len(r.text) > 0 and r.text != 'null':
|
||||
msg = '%s:\n %s' % (r.url, r.json()['error']['message'])
|
||||
raise SpotifyException(r.status_code,
|
||||
-1, msg,
|
||||
headers=r.headers)
|
||||
else:
|
||||
raise SpotifyException(r.status_code,
|
||||
-1, '%s:\n %s' % (r.url, 'error'),
|
||||
headers=r.headers)
|
||||
finally:
|
||||
if hasattr(r, "connection"):
|
||||
r.connection.close()
|
||||
if r.text and len(r.text) > 0 and r.text != 'null':
|
||||
results = r.json()
|
||||
if self.trace: # pragma: no cover
|
||||
print('RESP', results)
|
||||
print()
|
||||
return results
|
||||
else:
|
||||
return None
|
||||
return results
|
||||
|
||||
def _get(self, url, args=None, payload=None, **kwargs):
|
||||
if args:
|
||||
|
||||
@ -203,6 +203,7 @@ class TestSpotipy(unittest.TestCase):
|
||||
self.assertTrue(
|
||||
with_custom_session.user(
|
||||
user="akx")["uri"] == "spotify:user:akx")
|
||||
sess.close()
|
||||
|
||||
def test_force_no_requests_session(self):
|
||||
with_no_session = Spotify(auth=self.token, requests_session=False)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user