mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-10-10 17:00:46 +00:00
Added MemoryCacheHandler, a cache handler that simply stores the token info in memory as an instance attribute of this class.
This commit is contained in:
1 parent
36bdeb0a65
commit
0b6ede6876
3 files changed
+27
-16
No files matched your search
+3
-1
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## 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.
|
||||
|
||||
## [2.18.0] - 2021-04-13
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
__all__ = ['CacheHandler', 'CacheFileHandler']
|
||||
__all__ = ['CacheHandler', 'CacheFileHandler', 'MemoryCacheHandler']
|
||||
|
||||
import errno
|
||||
import json
|
||||
@@ -82,3 +82,24 @@ class CacheFileHandler(CacheHandler):
|
||||
except IOError:
|
||||
logger.warning('Couldn\'t write token to cache at: %s',
|
||||
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
|
||||
@@ -6,7 +6,7 @@ import unittest
|
||||
import six.moves.urllib.parse as urllibparse
|
||||
|
||||
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 SpotifyStateError
|
||||
|
||||
@@ -51,18 +51,6 @@ def _make_pkceauth(*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):
|
||||
|
||||
@patch.multiple(SpotifyOAuth,
|
||||
@@ -161,7 +149,7 @@ class OAuthCacheTest(unittest.TestCase):
|
||||
scope = "playlist-modify-private"
|
||||
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)
|
||||
cached_tok = spot.cache_handler.get_cached_token()
|
||||
|
||||
|
||||
Reference in new issue
Block a user