Skip to content

Commit

Permalink
Switched to typed dict for cache type
Browse files Browse the repository at this point in the history
  • Loading branch information
JackDyre committed Aug 9, 2024
1 parent 174c4ec commit 68a5399
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ dev-dependencies = [
"flake8>=7.1.1",
"isort>=5.13.2",
"autopep8>=2.3.1",
"pyupgrade>=3.16.0",
]

3 changes: 3 additions & 0 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ pyflakes==3.2.0
pylint==3.2.6
pymemcache==4.0.0
# via spotipy
pyupgrade==3.16.0
redis==5.0.8
# via spotipy
requests==2.32.3
# via spotipy
tokenize-rt==6.0.0
# via pyupgrade
tomli==2.0.1
# via autopep8
# via mypy
Expand Down
47 changes: 28 additions & 19 deletions spotipy/cache_handler.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
__all__ = [
"CacheHandler",
"CacheFileHandler",
"DjangoSessionCacheHandler",
"FlaskSessionCacheHandler",
"MemoryCacheHandler",
"RedisCacheHandler",
"MemcacheCacheHandler",
]
from __future__ import annotations

import errno
import json
Expand All @@ -15,15 +7,32 @@
from abc import ABC, abstractmethod
from json import JSONEncoder
from pathlib import Path
from typing import Dict, Optional, Type, Union
from typing import TypedDict

from redis import RedisError

from .util import CLIENT_CREDS_ENV_VARS

__all__ = [
"CacheHandler",
"CacheFileHandler",
"DjangoSessionCacheHandler",
"FlaskSessionCacheHandler",
"MemoryCacheHandler",
"RedisCacheHandler",
"MemcacheCacheHandler",
]

logger = logging.getLogger(__name__)

TokenInfoType = Dict[str, Union[str, int]]

class TokenInfo(TypedDict):
access_token: str
token_type: str
scope: str
expires_in: int
refresh_token: str
expires_at: int


class CacheHandler(ABC):
Expand All @@ -37,11 +46,11 @@ class CacheHandler(ABC):
"""

@abstractmethod
def get_cached_token(self) -> Optional[TokenInfoType]:
def get_cached_token(self) -> TokenInfo | None:
"""Get and return a token_info dictionary object."""

@abstractmethod
def save_token_to_cache(self, token_info: TokenInfoType) -> None:
def save_token_to_cache(self, token_info: TokenInfo) -> None:
"""Save a token_info dictionary object to the cache and return None."""


Expand All @@ -50,9 +59,9 @@ class CacheFileHandler(CacheHandler):

def __init__(
self,
cache_path: Optional[str] = None,
username: Optional[str] = None,
encoder_cls: Optional[Type[JSONEncoder]] = None,
cache_path: str | None = None,
username: str | None = None,
encoder_cls: type[JSONEncoder] | None = None,
) -> None:
"""
Initialize CacheFileHandler instance.
Expand All @@ -71,9 +80,9 @@ def __init__(
cache_path += "-" + str(username)
self.cache_path = cache_path

def get_cached_token(self) -> Optional[TokenInfoType]:
def get_cached_token(self) -> TokenInfo | None:
"""Get cached token from file."""
token_info: Optional[TokenInfoType] = None
token_info: TokenInfo | None = None

try:
with Path(self.cache_path).open("r") as f:
Expand All @@ -87,7 +96,7 @@ def get_cached_token(self) -> Optional[TokenInfoType]:

return token_info

def save_token_to_cache(self, token_info: TokenInfoType) -> None:
def save_token_to_cache(self, token_info: TokenInfo) -> None:
"""Save token cache to file."""
try:
with Path(self.cache_path).open("w") as f:
Expand Down

0 comments on commit 68a5399

Please sign in to comment.