Skip to content

Commit

Permalink
fix(spotify): invalid pagination causing double requests
Browse files Browse the repository at this point in the history
  • Loading branch information
SilentVoid13 committed Oct 15, 2024
1 parent 0e78d42 commit 16a8e33
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
32 changes: 13 additions & 19 deletions src/spotify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,19 @@ impl SpotifyApi {
&self,
path: &str,
method: HttpMethod<'_>,
limit: u32,
limit: usize,
) -> Result<SpotifyPageResponse<T>>
where
T: DeserializeOwned,
{
let mut offset = 0;
let mut response: SpotifyPageResponse<T> =
self.make_request_json(path, &method, limit, offset).await?;
let mut total = response.total;

while offset < total {
offset += limit;
let mut response2: SpotifyPageResponse<T> =
self.make_request_json(path, &method, limit, offset).await?;
total = response2.total;
response.merge(&mut response2);
let mut res: SpotifyPageResponse<T> =
self.make_request_json(path, &method, limit, 0).await?;
while res.next.is_some() {
let offset = res.items.len();
let res2 = self.make_request_json(path, &method, limit, offset).await?;
res.merge(res2);
}
Ok(response)
Ok(res)
}

async fn api_rate_wait(&self, res: &Response) -> Result<()> {
Expand All @@ -194,8 +189,8 @@ impl SpotifyApi {
&self,
path: &str,
method: &HttpMethod<'_>,
limit: u32,
offset: u32,
limit: usize,
offset: usize,
) -> Result<Response> {
let endpoint = self.build_endpoint(path);

Expand All @@ -205,7 +200,7 @@ impl SpotifyApi {
HttpMethod::Put(b) => self.client.put(endpoint).json(b),
HttpMethod::Delete(b) => self.client.delete(endpoint).json(b),
};
//request = request.query(&[("limit", limit), ("offset", offset)]);
request = request.query(&[("limit", limit), ("offset", offset)]);
let res = request.send().await?;
if res.status() == StatusCode::TOO_MANY_REQUESTS {
self.api_rate_wait(&res).await?;
Expand All @@ -223,8 +218,8 @@ impl SpotifyApi {
&self,
path: &str,
method: &HttpMethod<'_>,
limit: u32,
offset: u32,
limit: usize,
offset: usize,
) -> Result<T>
where
T: DeserializeOwned,
Expand Down Expand Up @@ -422,7 +417,6 @@ impl MusicApi for SpotifyApi {
let body = json!({
"ids": ids,
});
dbg!(&body);
self.make_request("/me/tracks", &HttpMethod::Put(&body), 50, 0)
.await?;
}
Expand Down
7 changes: 5 additions & 2 deletions src/spotify/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ pub struct SpotifySearchResponse {
pub struct SpotifyPageResponse<T> {
pub items: Vec<T>,
pub total: u32,
pub next: Option<String>,
}
impl<T> SpotifyPageResponse<T> {
pub fn merge(&mut self, other: &mut Self) {
self.items.append(&mut other.items);
pub fn merge(&mut self, other: Self) {
self.items.extend(other.items);
self.total += other.total;
self.next = other.next;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/spotify/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl TryInto<Playlist> for SpotifyPlaylistResponse {
fn try_into(self) -> Result<Playlist, Self::Error> {
Ok(Playlist {
id: self.id,
name: self.name,
name: self.name.trim().to_string(),
songs: vec![],
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/tidal/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl TryInto<Playlist> for TidalPlaylistResponse {
fn try_into(self) -> Result<Playlist, Self::Error> {
Ok(Playlist {
id: self.uuid,
name: self.title,
name: self.title.trim().to_string(),
songs: vec![],
})
}
Expand Down
6 changes: 5 additions & 1 deletion src/yt_music/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ impl TryInto<Playlists> for YtMusicResponse {
{
let id = mtrir.get_id().ok_or(eyre!("No playlist id"))?;
let id = YtMusicApi::clean_playlist_id(&id);
let name = mtrir.get_name().ok_or(eyre!("No playlist name"))?;
let name = mtrir
.get_name()
.ok_or(eyre!("No playlist name"))?
.trim()
.to_string();
let playlist = Playlist {
id,
name,
Expand Down

0 comments on commit 16a8e33

Please sign in to comment.