Make redis dependency optional (#869)

This commit is contained in:
Andrew J. Hesford 2022-12-26 13:08:04 -05:00 committed by GitHub
parent 4918305aba
commit f005732a38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 3 deletions

View File

@ -12,6 +12,10 @@ While this is unreleased, please only add v3 features here. Rebasing master onto
### Added ### Added
* `Scope` - An enum which contains all of the authorization scopes (see [here](https://github.com/plamere/spotipy/issues/652#issuecomment-797461311)). * `Scope` - An enum which contains all of the authorization scopes (see [here](https://github.com/plamere/spotipy/issues/652#issuecomment-797461311)).
* Added `RedisCacheHandler`, a cache handler that stores the token info in Redis.
* Added a new parameter to `RedisCacheHandler` to allow custom keys (instead of the default `token_info` key)
* Simplify check for existing token in `RedisCacheHandler`
* Make redis an optional dependency
### Changed ### Changed

View File

@ -26,6 +26,12 @@ or upgrade
pip install spotipy --upgrade pip install spotipy --upgrade
``` ```
Spotipy includes optional support for caching Spotify access tokens to a Redis server. To use this support, make sure to specify the `redis` extra:
```bash
pip install spotipy[redis]
```
## Quick Start ## Quick Start
A full set of examples can be found in the [online documentation](http://spotipy.readthedocs.org/) and in the [Spotipy examples directory](https://github.com/plamere/spotipy/tree/master/examples). A full set of examples can be found in the [online documentation](http://spotipy.readthedocs.org/) and in the [Spotipy examples directory](https://github.com/plamere/spotipy/tree/master/examples).

View File

@ -11,9 +11,15 @@ doc_reqs = [
'Sphinx>=1.5.2' 'Sphinx>=1.5.2'
] ]
redis_reqs = [
"redis>=3.5.3",
"redis<4.0.0;python_version<'3.4'",
]
extra_reqs = { extra_reqs = {
'doc': doc_reqs, 'doc': doc_reqs,
'test': test_reqs 'test': test_reqs,
'redis': redis_reqs,
} }
setup( setup(

View File

@ -12,8 +12,7 @@ import logging
import os import os
from spotipy.util import CLIENT_CREDS_ENV_VARS from spotipy.util import CLIENT_CREDS_ENV_VARS
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
import warnings
from redis import RedisError
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -188,8 +187,19 @@ class RedisCacheHandler(CacheHandler):
self.redis = redis self.redis = redis
self.key = key if key else 'token_info' self.key = key if key else 'token_info'
try:
from redis import RedisError # noqa: F401
except ImportError:
warnings.warn(
'Error importing module "redis"; '
'it is required for RedisCacheHandler',
UserWarning
)
def get_cached_token(self): def get_cached_token(self):
token_info = None token_info = None
from redis import RedisError
try: try:
token_info = self.redis.get(self.key) token_info = self.redis.get(self.key)
if token_info: if token_info:
@ -200,6 +210,8 @@ class RedisCacheHandler(CacheHandler):
return token_info return token_info
def save_token_to_cache(self, token_info): def save_token_to_cache(self, token_info):
from redis import RedisError
try: try:
self.redis.set(self.key, json.dumps(token_info)) self.redis.set(self.key, json.dumps(token_info))
except RedisError as e: except RedisError as e: