Bound Spotify auth retry during search

Replace recursive 401 handling with a single retry loop.

This prevents unbounded recursion when authentication keeps failing.
This commit is contained in:
Šarūnas Nejus 2026-03-06 20:51:18 +00:00
parent 9b63985989
commit 9855d46901
No known key found for this signature in database

View file

@ -27,6 +27,7 @@ import re
import threading
import time
import webbrowser
from http import HTTPStatus
from typing import TYPE_CHECKING, Any, ClassVar, Literal
import confuse
@ -477,27 +478,33 @@ class SpotifyPlugin(
:param filters: Field filters to apply.
:param query_string: Additional query to include in the search.
"""
response = requests.get(
self.search_url,
headers={"Authorization": f"Bearer {self.access_token}"},
params={
**params.filters,
"q": params.query,
"type": params.query_type,
"limit": str(params.limit),
},
timeout=10,
)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
if response.status_code == 401:
self._authenticate()
return self.get_search_response(params)
for _ in range(2):
response = requests.get(
self.search_url,
headers={"Authorization": f"Bearer {self.access_token}"},
params={
**params.filters,
"q": params.query,
"type": params.query_type,
"limit": str(params.limit),
},
timeout=10,
)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
if response.status_code == HTTPStatus.UNAUTHORIZED:
self._authenticate()
continue
raise
raise
return (
response.json()
.get(f"{params.query_type}s", {})
.get("items", [])
)
return response.json().get(f"{params.query_type}s", {}).get("items", [])
return ()
def commands(self) -> list[ui.Subcommand]:
# autotagger import command