Matatika / tap-spotify

Singer tap for Spotify
GNU Affero General Public License v3.0
6 stars 2 forks source link

`502 Bad Gateway` from top items endpoints #26

Closed ReubenFrankel closed 7 months ago

ReubenFrankel commented 8 months ago

Looks like something changed on Spotify's side that started causing 502 Bad Gateway errors when accessing top tracks/artists, where the combined value of limit and offset parameters exceeds the total number of items - 100:

{
    "error": {
        "status": 502,
        "message": ""
    }
}

Working

https://api.spotify.com/v1/me/top/tracks?offset=99&limit=1
https://api.spotify.com/v1/me/top/tracks?offset=50&limit=50
https://api.spotify.com/v1/me/top/tracks?offset=49&limit=49

Broken

https://api.spotify.com/v1/me/top/tracks?offset=100&limit=1
https://api.spotify.com/v1/me/top/tracks?offset=51&limit=50
https://api.spotify.com/v1/me/top/tracks?offset=98&limit=49

We originally defined a limit of 49 for top items streams in order to maximise the amount of data returned (i.e. over 100 items) by exploiting a previous quirk of the API, where paginating with next links yielded an "extra" 49 items (147 total):

1st request

{
    "items": [...
    ],
    "total": 100,
    "limit": 49,
    "offset": 0,
    "href": "https://api.spotify.com/v1/me/top/tracks?limit=49",
    "next": "https://api.spotify.com/v1/me/top/tracks?offset=49&limit=49",
    "previous": null
}

2nd request

{
    "items": [...
    ],
    "total": 100,
    "limit": 49,
    "offset": 49,
    "href": "https://api.spotify.com/v1/me/top/tracks?offset=49&limit=49",
    "next": "https://api.spotify.com/v1/me/top/tracks?offset=98&limit=49",
    "previous": "https://api.spotify.com/v1/me/top/tracks?offset=0&limit=49"
}

3rd request (now 502 Bad Gateway) - no next link, pagination stops

{
    "items": [...
    ],
    "total": 100,
    "limit": 49,
    "offset": 98,
    "href": "https://api.spotify.com/v1/me/top/tracks?offset=98&limit=49",
    "next": null,
    "previous": "https://api.spotify.com/v1/me/top/tracks?offset=49&limit=49"
}

If 50 was used instead, pagination would stop at ?limit=50&offset=50 (100 total):

1st request

{
    "items": [...
    ],
    "total": 100,
    "limit": 50,
    "offset": 0,
    "href": "https://api.spotify.com/v1/me/top/tracks?limit=50",
    "next": "https://api.spotify.com/v1/me/top/tracks?offset=50&limit=50",
    "previous": null
}

2nd request - no next link, pagination stops


{
    "items": [...
    ],
    "total": 100,
    "limit": 50,
    "offset": 50,
    "href": "https://api.spotify.com/v1/me/top/tracks?offset=50&limit=50",
    "next": null,
    "previous": "https://api.spotify.com/v1/me/top/tracks?offset=0&limit=50"
}
ReubenFrankel commented 8 months ago

https://community.spotify.com/t5/Spotify-for-Developers/Web-API-502-Bad-Gateway-for-top-items-endpoints-when-offset-and/m-p/5897842#M12779