Merge pull request #269 from sonneveld/client-internal-call-refactor

Refactor client internal calls to allow keepalive
This commit is contained in:
Stéphane Bruckert 2020-01-14 13:57:45 +00:00 committed by GitHub
commit c2b6be6446
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 35 deletions

View File

@ -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

View File

@ -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('headers', headers)
print('http status', r.status_code)
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))
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'])
try:
msg = r.json()['error']['message']
except BaseException:
msg = 'error'
raise SpotifyException(r.status_code,
-1, msg,
-1, '%s:\n %s' % (r.url, 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':
try:
results = r.json()
except BaseException:
results = None
if self.trace: # pragma: no cover
print('RESP', results)
print('Response:', results)
print()
return results
else:
return None
def _get(self, url, args=None, payload=None, **kwargs):
if args:

View File

@ -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)