mirror of
https://github.com/c0de-archive/spotipy.git
synced 2024-11-05 07:27:47 +00:00
Avoid unneeded token renewal when cached token's scope contains currently required scope
Before that there was a strict string comparision "cached scope" == "currently required scope". Added a scope subset detection method.
This commit is contained in:
parent
1bdd8b32c3
commit
8de2591c18
@ -130,7 +130,7 @@ class SpotifyOAuth(object):
|
|||||||
token_info = json.loads(token_info_string)
|
token_info = json.loads(token_info_string)
|
||||||
|
|
||||||
# if scopes don't match, then bail
|
# if scopes don't match, then bail
|
||||||
if 'scope' not in token_info or self.scope != token_info['scope']:
|
if 'scope' not in token_info or not self._is_scope_subset(self.scope, token_info['scope']):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if self._is_token_expired(token_info):
|
if self._is_token_expired(token_info):
|
||||||
@ -150,6 +150,11 @@ class SpotifyOAuth(object):
|
|||||||
self._warn("couldn't write token cache to " + self.cache_path)
|
self._warn("couldn't write token cache to " + self.cache_path)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def _is_scope_subset(self, needle_scope, haystack_scope):
|
||||||
|
needle_scope = set(needle_scope.split())
|
||||||
|
haystack_scope = set(haystack_scope.split())
|
||||||
|
|
||||||
|
return needle_scope <= haystack_scope
|
||||||
|
|
||||||
def _is_token_expired(self, token_info):
|
def _is_token_expired(self, token_info):
|
||||||
now = int(time.time())
|
now = int(time.time())
|
||||||
|
Loading…
Reference in New Issue
Block a user