[PR #625] [MERGED] Create CacheHandler to abstract caching tokens #968

Closed
opened 2026-02-28 00:02:59 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/spotipy-dev/spotipy/pull/625
Author: @tonyaajjackson
Created: 12/16/2020
Status: Merged
Merged: 12/21/2020
Merged by: @stephanebruckert

Base: masterHead: master


📝 Commits (9)

  • 7afe673 Refactor functions into static methods of AuthBase
  • 5a48891 Create CacheHandler to abstract caching tokens
  • 0f201b6 Merge branch 'master' of github.com:plamere/spotipy
  • b824d6d Fix cache_handler subclass check for Python 2
  • a4ad2db Split assert message to fix line over max length
  • 9d8c455 Split cache handlers into cache_handler.py
  • 58d46eb flake8 and autopep fixes
  • db17532 Fix init to allow importing CacheHandler
  • 5a5970c flake8 fix

📊 Changes

6 files changed (+243 additions, -205 deletions)

View changed files

📝 CHANGELOG.md (+2 -1)
📝 spotipy/__init__.py (+1 -0)
spotipy/cache_handler.py (+84 -0)
📝 spotipy/oauth2.py (+108 -179)
📝 spotipy/util.py (+1 -1)
📝 tests/unit/test_oauth.py (+47 -24)

📄 Description

Contributing.md Checklist:

  • Tests pass
  • Autopep8 fixing performed
  • flake8 verification passes
  • isort: raises issues, but issues appear unrelated to changes in pull request

Change Description

Previous code only supported caching to and from json files in a given directory. In addition, the get_cached_token method mixed getting and getting the token in the same method.

This change creates a CacheHandler class to abstract out the caching implementation and allow the user to cache tokens in any way they see fit. For example, the user could create a MongoCache class to store and retrieve tokens from a Mongo database and specify that cache_handler=MongoCache when creating an auth_manager object.

cache_handler is added as an argument to SpotifyOAuth, SpotifyPKCE, and SpotifyImplicitGrant. Specifying a cache_handler now overrides any specification of cache_path and/or username.

To preserve backwards compatibility in handling cache files, a CacheFileHandler class extending CacheHandler is created that implements previous functionality in getting and saving to json files. If no cache_handler is specified, the cache_path and username arguments are used to create an instance of CacheFileHandler. It may be worth deprecating the cache_path and username fields in favour of using CacheFileHandler to simplify the interface.

Tests are also modified and extended to cover the new functionality. A sample MemoryCache CacheHandler that stores the cached token as a variable in memory is created to test getting and saving to a custom CacheHandler.

Minor refactoring of is_token_expired and _is_scope_subset also performed to make adding the CacheHandler abstraction easier.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/spotipy-dev/spotipy/pull/625 **Author:** [@tonyaajjackson](https://github.com/tonyaajjackson) **Created:** 12/16/2020 **Status:** ✅ Merged **Merged:** 12/21/2020 **Merged by:** [@stephanebruckert](https://github.com/stephanebruckert) **Base:** `master` ← **Head:** `master` --- ### 📝 Commits (9) - [`7afe673`](https://github.com/spotipy-dev/spotipy/commit/7afe673f0b5b494d7915e8bb460a8752f6e701d2) Refactor functions into static methods of AuthBase - [`5a48891`](https://github.com/spotipy-dev/spotipy/commit/5a48891dccbab6c4be698930f12cf452f9178bef) Create CacheHandler to abstract caching tokens - [`0f201b6`](https://github.com/spotipy-dev/spotipy/commit/0f201b6e9bf8dc0291b148884455d2acf5e66872) Merge branch 'master' of github.com:plamere/spotipy - [`b824d6d`](https://github.com/spotipy-dev/spotipy/commit/b824d6da19c378175a6a3d11304b1fdb161b01a3) Fix cache_handler subclass check for Python 2 - [`a4ad2db`](https://github.com/spotipy-dev/spotipy/commit/a4ad2db01ee1a857dbce9728998e2e869b08dd97) Split assert message to fix line over max length - [`9d8c455`](https://github.com/spotipy-dev/spotipy/commit/9d8c4554a5515e6eb54fea0881d50406c00f12ab) Split cache handlers into cache_handler.py - [`58d46eb`](https://github.com/spotipy-dev/spotipy/commit/58d46eb5a14ba09491e5c8891283bc55e818dbb4) flake8 and autopep fixes - [`db17532`](https://github.com/spotipy-dev/spotipy/commit/db17532cf17554ac2025bad21856a8e15b2b7147) Fix init to allow importing CacheHandler - [`5a5970c`](https://github.com/spotipy-dev/spotipy/commit/5a5970c43fddae4b2da497e835d639cc521e0fac) flake8 fix ### 📊 Changes **6 files changed** (+243 additions, -205 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+2 -1) 📝 `spotipy/__init__.py` (+1 -0) ➕ `spotipy/cache_handler.py` (+84 -0) 📝 `spotipy/oauth2.py` (+108 -179) 📝 `spotipy/util.py` (+1 -1) 📝 `tests/unit/test_oauth.py` (+47 -24) </details> ### 📄 Description ## Contributing.md Checklist: - [X] Tests pass - [X] Autopep8 fixing performed - [X] flake8 verification passes - [ ] isort: raises issues, but issues appear unrelated to changes in pull request ## Change Description Previous code only supported caching to and from json files in a given directory. In addition, the get_cached_token method mixed getting and getting the token in the same method. This change creates a `CacheHandler` class to abstract out the caching implementation and allow the user to cache tokens in any way they see fit. For example, the user could create a `MongoCache` class to store and retrieve tokens from a Mongo database and specify that `cache_handler=MongoCache` when creating an `auth_manager` object. `cache_handler` is added as an argument to `SpotifyOAuth`, `SpotifyPKCE`, and `SpotifyImplicitGrant`. Specifying a `cache_handler` now overrides any specification of `cache_path` and/or `username`. To preserve backwards compatibility in handling cache files, a `CacheFileHandler` class extending `CacheHandler` is created that implements previous functionality in getting and saving to json files. If no `cache_handler` is specified, the `cache_path` and `username` arguments are used to create an instance of `CacheFileHandler`. It may be worth deprecating the `cache_path` and `username` fields in favour of using `CacheFileHandler` to simplify the interface. Tests are also modified and extended to cover the new functionality. A sample `MemoryCache` `CacheHandler` that stores the cached token as a variable in memory is created to test getting and saving to a custom `CacheHandler`. Minor refactoring of `is_token_expired` and `_is_scope_subset` also performed to make adding the `CacheHandler` abstraction easier. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-28 00:02:59 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#968
No description provided.