refactor: lazy load credentials for testing
This commit is contained in:
@@ -15,8 +15,8 @@ from ..cache import CacheEngine
|
||||
from ..config import (
|
||||
HTTP_TIMEOUT,
|
||||
MUSIXMATCH_TOKEN_URL,
|
||||
MUSIXMATCH_USERTOKEN,
|
||||
MUSIXMATCH_COOLDOWN_MS,
|
||||
credentials,
|
||||
)
|
||||
|
||||
_MXM_HEADERS = {"Cookie": "x-mxm-token-guid="}
|
||||
@@ -105,8 +105,8 @@ class MusixmatchAuthenticator(BaseAuthenticator):
|
||||
|
||||
async def _get_token(self) -> Optional[str]:
|
||||
"""Return a valid token: env var > memory > DB > fresh fetch."""
|
||||
if MUSIXMATCH_USERTOKEN:
|
||||
return MUSIXMATCH_USERTOKEN
|
||||
if credentials.MUSIXMATCH_USERTOKEN:
|
||||
return credentials.MUSIXMATCH_USERTOKEN
|
||||
|
||||
if self._cached_token:
|
||||
return self._cached_token
|
||||
|
||||
@@ -7,7 +7,7 @@ Description: QQ Music API authenticator - currently only a proxy
|
||||
from typing import Optional
|
||||
|
||||
from .base import BaseAuthenticator
|
||||
from ..config import QQ_MUSIC_API_URL
|
||||
from ..config import credentials
|
||||
|
||||
|
||||
class QQMusicAuthenticator(BaseAuthenticator):
|
||||
@@ -19,7 +19,7 @@ class QQMusicAuthenticator(BaseAuthenticator):
|
||||
return "qqmusic"
|
||||
|
||||
def is_configured(self) -> bool:
|
||||
return bool(QQ_MUSIC_API_URL)
|
||||
return bool(credentials.QQ_MUSIC_API_URL)
|
||||
|
||||
async def authenticate(self) -> Optional[str]:
|
||||
return QQ_MUSIC_API_URL
|
||||
return credentials.QQ_MUSIC_API_URL
|
||||
|
||||
@@ -18,9 +18,9 @@ from ..config import (
|
||||
HTTP_TIMEOUT,
|
||||
SPOTIFY_SERVER_TIME_URL,
|
||||
SPOTIFY_SECRET_URL,
|
||||
SPOTIFY_SP_DC,
|
||||
SPOTIFY_TOKEN_URL,
|
||||
UA_BROWSER,
|
||||
credentials,
|
||||
)
|
||||
|
||||
_SPOTIFY_BASE_HEADERS = {
|
||||
@@ -43,7 +43,7 @@ class SpotifyAuthenticator(BaseAuthenticator):
|
||||
return "spotify"
|
||||
|
||||
def is_configured(self) -> bool:
|
||||
return bool(SPOTIFY_SP_DC)
|
||||
return bool(credentials.SPOTIFY_SP_DC)
|
||||
|
||||
@staticmethod
|
||||
def _generate_totp(server_time_s: int, secret: str) -> str:
|
||||
@@ -133,14 +133,16 @@ class SpotifyAuthenticator(BaseAuthenticator):
|
||||
if db_token and time.time() < self._token_expires_at - 30:
|
||||
return db_token
|
||||
|
||||
if not SPOTIFY_SP_DC:
|
||||
logger.error("Spotify: SPOTIFY_SP_DC env var not set — cannot authenticate")
|
||||
if not credentials.SPOTIFY_SP_DC:
|
||||
logger.error(
|
||||
"Spotify: settings.SPOTIFY_SP_DC env var not set — cannot authenticate"
|
||||
)
|
||||
return None
|
||||
|
||||
headers = {
|
||||
"User-Agent": UA_BROWSER,
|
||||
"Accept": "*/*",
|
||||
"Cookie": f"sp_dc={SPOTIFY_SP_DC}",
|
||||
"Cookie": f"sp_dc={credentials.SPOTIFY_SP_DC}",
|
||||
**_SPOTIFY_BASE_HEADERS,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user