Added MemoryCacheHandler (#670)

* Added `MemoryCacheHandler`, a cache handler that simply stores the token info in memory as an instance attribute of this class.

* Fixed a bug in `CacheFileHandler.__init__`: The documentation says that the username will be retrieved from the environment, but it wasn't.
This commit is contained in:
Peter Schorn 2021-04-14 10:59:37 -05:00 committed by GitHub
parent 36bdeb0a65
commit 07915e21e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 16 deletions

View File

@ -7,7 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased ## Unreleased
// Add your changes here and then delete this line ### Added
* Added `MemoryCacheHandler`, a cache handler that simply stores the token info in memory as an instance attribute of this class.
### Fixed
* Fixed a bug in `CacheFileHandler.__init__`: The documentation says that the username will be retrieved from the environment, but it wasn't.
## [2.18.0] - 2021-04-13 ## [2.18.0] - 2021-04-13

View File

@ -1,8 +1,10 @@
__all__ = ['CacheHandler', 'CacheFileHandler'] __all__ = ['CacheHandler', 'CacheFileHandler', 'MemoryCacheHandler']
import errno import errno
import json import json
import logging import logging
import os
from spotipy.util import CLIENT_CREDS_ENV_VARS
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -53,6 +55,7 @@ class CacheFileHandler(CacheHandler):
self.cache_path = cache_path self.cache_path = cache_path
else: else:
cache_path = ".cache" cache_path = ".cache"
username = (username or os.getenv(CLIENT_CREDS_ENV_VARS["client_username"]))
if username: if username:
cache_path += "-" + str(username) cache_path += "-" + str(username)
self.cache_path = cache_path self.cache_path = cache_path
@ -82,3 +85,24 @@ class CacheFileHandler(CacheHandler):
except IOError: except IOError:
logger.warning('Couldn\'t write token to cache at: %s', logger.warning('Couldn\'t write token to cache at: %s',
self.cache_path) self.cache_path)
class MemoryCacheHandler(CacheHandler):
"""
A cache handler that simply stores the token info in memory as an
instance attribute of this class. The token info will be lost when this
instance is freed.
"""
def __init__(self, token_info=None):
"""
Parameters:
* token_info: The token info to store in memory. Can be None.
"""
self.token_info = token_info
def get_cached_token(self):
return self.token_info
def save_token_to_cache(self, token_info):
self.token_info = token_info

View File

@ -6,7 +6,7 @@ import unittest
import six.moves.urllib.parse as urllibparse import six.moves.urllib.parse as urllibparse
from spotipy import SpotifyOAuth, SpotifyImplicitGrant, SpotifyPKCE from spotipy import SpotifyOAuth, SpotifyImplicitGrant, SpotifyPKCE
from spotipy.cache_handler import CacheHandler from spotipy.cache_handler import MemoryCacheHandler
from spotipy.oauth2 import SpotifyClientCredentials, SpotifyOauthError from spotipy.oauth2 import SpotifyClientCredentials, SpotifyOauthError
from spotipy.oauth2 import SpotifyStateError from spotipy.oauth2 import SpotifyStateError
@ -51,18 +51,6 @@ def _make_pkceauth(*args, **kwargs):
return SpotifyPKCE("CLID", "REDIR", "STATE", *args, **kwargs) return SpotifyPKCE("CLID", "REDIR", "STATE", *args, **kwargs)
class MemoryCache(CacheHandler):
def __init__(self, token_info=None):
self.token_info = token_info
def get_cached_token(self):
return self.token_info
def save_token_to_cache(self, token_info):
self.token_info = token_info
return None
class OAuthCacheTest(unittest.TestCase): class OAuthCacheTest(unittest.TestCase):
@patch.multiple(SpotifyOAuth, @patch.multiple(SpotifyOAuth,
@ -161,7 +149,7 @@ class OAuthCacheTest(unittest.TestCase):
scope = "playlist-modify-private" scope = "playlist-modify-private"
tok = _make_fake_token(1, 1, scope) tok = _make_fake_token(1, 1, scope)
spot = _make_oauth(scope, cache_handler=MemoryCache()) spot = _make_oauth(scope, cache_handler=MemoryCacheHandler())
spot.cache_handler.save_token_to_cache(tok) spot.cache_handler.save_token_to_cache(tok)
cached_tok = spot.cache_handler.get_cached_token() cached_tok = spot.cache_handler.get_cached_token()