Fall back to per-track reads when batching is withheld - #22
Open
jonico wants to merge 1 commit into
Open
Conversation
get_track_info fails for two or more IDs on a restricted app. It routes anything
longer than one ID through /v1/tracks?ids=, which answers 403 Forbidden, while
/v1/tracks/{id} still works for each of those same IDs. So the batching the
docstring advertises -- "50 tracks = 1 API call instead of 50" -- is not merely
slower than promised, it is unavailable, and asking for two tracks errors out.
The boundary is exactly at the len(ids) == 1 branch:
get_track_info("6ADPS...") -> ok
get_track_info(["6ADPS..."]) -> ok
get_track_info(["6ADPS...", "7AvWm..."]) -> playback_restricted
with_fallback does not fit: there is no alternative batch path to swap in, only a
different number of requests. So try the batch, and on a 401/403 degrade to one
request per track and remember that for the life of the process, so the 403 is
paid once rather than on every call. A single ID keeps using the single-track
route and never provokes the 403. Any other status still raises.
Callers see no difference beyond latency, which is why the fix belongs here
rather than in every caller.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
get_track_infofails for two or more IDs on a restricted app.Anything longer than one ID goes through
/v1/tracks?ids=, which is withheld — while the single-track route works fine for those exact same IDs:The failure boundary sits exactly on the
len(ids) == 1branch, which makes it easy to miss:get_track_info("6ADPS...")get_track_info(["6ADPS..."])get_track_info(["6ADPS...", "7AvWm..."])playback_restrictedSo the batching the docstring advertises — "50 tracks = 1 API call instead of 50" — isn't merely slower than promised, it's unavailable, and simply asking for two tracks errors out. I found this trying to put two track IDs in one request and assuming I'd hit a rate limit or a bad ID.
Fix
with_fallbackdoesn't fit here: there's no alternative batch path to swap in, only a different number of requests. So try the batch, and on 401/403 degrade to one request per track.Callers see no difference beyond latency, which is why this belongs in
spotify_apirather than in each caller. The docstring now says batching may fall back instead of promising something the app may not be able to do.Testing
ruff check,ruff format --check,mypy src/,bandit -r src/all cleanpytest: 177 passed (8 new — single-ID never batches, batch when allowed, null entries dropped, 401 and 403 both degrade, the decision is cached, other statuses propagate, URIs accepted)The 3-ID call previously failed outright.
Relationship to my other PRs
Independent of #19, #20 and #21 — no shared lines, any merge order works. It's the same class of defect as #19 (
get_artist_info): a withheld endpoint inside a tool taking the whole tool down rather than degrading. If you'd prefer these consolidated into one "degrade instead of failing on withheld endpoints" PR, happy to do that.Note
No
CHANGELOG.mdentry deliberately — sibling fixes would all conflict on the same lines. Happy to add one to whichever lands first.