mirror of
https://github.com/c0de-archive/spotipy.git
synced 2025-01-04 22:32:40 +00:00
Close request object, not the whole connection. Allows keepalive
This merges [PR-269](https://github.com/plamere/spotipy/pull/269) from the base spotipy project ahead of it getting merged in upstream.
This commit is contained in:
parent
4c2c1d763a
commit
9eb1c67726
@ -107,36 +107,37 @@ 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:
|
||||
try:
|
||||
msg = r.json()['error']['message']
|
||||
except:
|
||||
msg = 'error'
|
||||
raise SpotifyException(r.status_code,
|
||||
-1, '%s:\n %s' % (r.url, msg), headers=r.headers)
|
||||
|
||||
try:
|
||||
results = r.json()
|
||||
except:
|
||||
results = None
|
||||
|
||||
if self.trace: # pragma: no cover
|
||||
print('RESP', 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:
|
||||
if r.text and len(r.text) > 0 and r.text != 'null':
|
||||
raise SpotifyException(r.status_code,
|
||||
-1, '%s:\n %s' % (r.url, r.json()['error']['message']),
|
||||
headers=r.headers)
|
||||
else:
|
||||
raise SpotifyException(r.status_code,
|
||||
-1, '%s:\n %s' % (r.url, 'error'), headers=r.headers)
|
||||
finally:
|
||||
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:
|
||||
|
Loading…
Reference in New Issue
Block a user