Upgrade rspotify to 0.11.6

Passing IDs to rspotify still feels a little wonky with their `Id` changes
making use of `Cow`. Maybe it can be simplified on our end, but it's a start.

Fixes #844

See also:

https://github.com/ramsayleung/rspotify/pull/332
This commit is contained in:
Henrik Friedrichsen
2022-12-14 21:12:08 +01:00
parent 7063c9a9aa
commit 23e9ecfd5c
3 changed files with 115 additions and 117 deletions

View File

@@ -141,6 +141,19 @@ impl From<&PlayableItem> for Playable {
}
}
impl From<&Playable> for rspotify::prelude::PlayableId<'_> {
fn from(p: &Playable) -> Self {
match p {
Playable::Track(t) => rspotify::prelude::PlayableId::Track(
rspotify::model::TrackId::from_id(t.id.clone().unwrap()).unwrap(),
),
Playable::Episode(e) => rspotify::prelude::PlayableId::Episode(
rspotify::model::EpisodeId::from_id(e.id.clone()).unwrap(),
),
}
}
}
impl fmt::Display for Playable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {

View File

@@ -151,17 +151,9 @@ impl WebApi {
position: Option<i32>,
) -> bool {
self.api_with_retry(|api| {
let trackids: Vec<Box<dyn PlayableId>> = tracks
.iter()
.map(|playable| {
Box::new(
TrackId::from_id(playable.id().as_ref().unwrap_or(&"".to_string()))
.unwrap(),
) as Box<dyn PlayableId>
})
.collect();
let trackids: Vec<PlayableId> = tracks.iter().map(|playable| playable.into()).collect();
api.playlist_add_items(
&PlaylistId::from_id(playlist_id).unwrap(),
PlaylistId::from_id(playlist_id).unwrap(),
trackids.iter().map(|id| id.as_ref()),
position,
)
@@ -176,18 +168,8 @@ impl WebApi {
playables: &[Playable],
) -> bool {
self.api_with_retry(move |api| {
let playable_ids: Vec<Box<dyn PlayableId>> = playables
.iter()
.map(|playable| match playable {
Playable::Track(track) => {
Box::new(TrackId::from_id(&track.id.clone().unwrap_or_default()).unwrap())
as Box<dyn PlayableId>
}
Playable::Episode(episode) => {
Box::new(EpisodeId::from_id(&episode.id).unwrap()) as Box<dyn PlayableId>
}
})
.collect();
let playable_ids: Vec<PlayableId> =
playables.iter().map(|playable| playable.into()).collect();
let positions = playables
.iter()
.map(|playable| [playable.list_index() as u32])
@@ -201,7 +183,7 @@ impl WebApi {
})
.collect();
api.playlist_remove_specific_occurrences_of_items(
&PlaylistId::from_id(playlist_id).unwrap(),
PlaylistId::from_id(playlist_id).unwrap(),
item_pos,
Some(snapshot_id),
)
@@ -221,20 +203,10 @@ impl WebApi {
};
if let Some(()) = self.api_with_retry(|api| {
let playable_ids: Vec<Box<dyn PlayableId>> = tracks
.iter()
.map(|playable| match playable {
Playable::Track(track) => {
Box::new(TrackId::from_id(&track.id.clone().unwrap_or_default()).unwrap())
as Box<dyn PlayableId>
}
Playable::Episode(episode) => {
Box::new(EpisodeId::from_id(&episode.id).unwrap()) as Box<dyn PlayableId>
}
})
.collect();
let playable_ids: Vec<PlayableId> =
tracks.iter().map(|playable| playable.into()).collect();
api.playlist_replace_items(
&PlaylistId::from_id(id).unwrap(),
PlaylistId::from_id(id).unwrap(),
playable_ids.iter().map(|p| p.as_ref()),
)
}) {
@@ -261,7 +233,7 @@ impl WebApi {
}
pub fn delete_playlist(&self, id: &str) -> bool {
self.api_with_retry(|api| api.playlist_unfollow(&PlaylistId::from_id(id).unwrap()))
self.api_with_retry(|api| api.playlist_unfollow(PlaylistId::from_id(id).unwrap()))
.is_some()
}
@@ -273,7 +245,7 @@ impl WebApi {
) -> Option<String> {
let result = self.api_with_retry(|api| {
api.user_playlist_create(
&UserId::from_id(self.user.as_ref().unwrap()).unwrap(),
UserId::from_id(self.user.as_ref().unwrap()).unwrap(),
name,
public,
None,
@@ -285,32 +257,32 @@ impl WebApi {
pub fn album(&self, album_id: &str) -> Option<FullAlbum> {
let aid = AlbumId::from_id(album_id).ok()?;
self.api_with_retry(|api| api.album(&aid))
self.api_with_retry(|api| api.album(aid.clone()))
}
pub fn artist(&self, artist_id: &str) -> Option<FullArtist> {
let aid = ArtistId::from_id(artist_id).ok()?;
self.api_with_retry(|api| api.artist(&aid))
self.api_with_retry(|api| api.artist(aid.clone()))
}
pub fn playlist(&self, playlist_id: &str) -> Option<FullPlaylist> {
let pid = PlaylistId::from_id(playlist_id).ok()?;
self.api_with_retry(|api| api.playlist(&pid, None, Some(&Market::FromToken)))
self.api_with_retry(|api| api.playlist(pid.clone(), None, Some(Market::FromToken)))
}
pub fn track(&self, track_id: &str) -> Option<FullTrack> {
let tid = TrackId::from_id(track_id).ok()?;
self.api_with_retry(|api| api.track(&tid))
self.api_with_retry(|api| api.track(tid.clone()))
}
pub fn get_show(&self, show_id: &str) -> Option<FullShow> {
let sid = ShowId::from_id(show_id).ok()?;
self.api_with_retry(|api| api.get_a_show(&sid, Some(&Market::FromToken)))
self.api_with_retry(|api| api.get_a_show(sid.clone(), Some(Market::FromToken)))
}
pub fn episode(&self, episode_id: &str) -> Option<FullEpisode> {
let eid = EpisodeId::from_id(episode_id).ok()?;
self.api_with_retry(|api| api.get_an_episode(&eid, Some(&Market::FromToken)))
self.api_with_retry(|api| api.get_an_episode(eid.clone(), Some(Market::FromToken)))
}
pub fn recommendations(
@@ -323,21 +295,21 @@ impl WebApi {
let seed_artistids = seed_artists.as_ref().map(|artistids| {
artistids
.iter()
.map(|id| ArtistId::from_id(id).unwrap())
.map(|id| ArtistId::from_id(id.clone()).unwrap())
.collect::<Vec<ArtistId>>()
});
let seed_trackids = seed_tracks.as_ref().map(|trackids| {
trackids
.iter()
.map(|id| TrackId::from_id(id).unwrap())
.map(|id| TrackId::from_id(id.clone()).unwrap())
.collect::<Vec<TrackId>>()
});
api.recommendations(
std::iter::empty(),
seed_artistids.as_ref(),
seed_artistids,
seed_genres.clone(),
seed_trackids.as_ref(),
Some(&Market::FromToken),
seed_trackids,
Some(Market::FromToken),
Some(100),
)
})
@@ -353,8 +325,8 @@ impl WebApi {
self.api_with_retry(|api| {
api.search(
query,
&searchtype,
Some(&Market::FromToken),
searchtype,
Some(Market::FromToken),
None,
Some(limit),
Some(offset),
@@ -393,9 +365,9 @@ impl WebApi {
);
spotify.api_with_retry(|api| {
match api.playlist_items_manual(
&PlaylistId::from_id(&playlist_id).unwrap(),
PlaylistId::from_id(&playlist_id).unwrap(),
None,
Some(&Market::FromToken),
Some(Market::FromToken),
Some(MAX_LIMIT),
Some(offset),
) {
@@ -425,7 +397,7 @@ impl WebApi {
}
pub fn full_album(&self, album_id: &str) -> Option<FullAlbum> {
self.api_with_retry(|api| api.album(&AlbumId::from_id(album_id).unwrap()))
self.api_with_retry(|api| api.album(AlbumId::from_id(album_id).unwrap()))
}
pub fn album_tracks(
@@ -436,7 +408,7 @@ impl WebApi {
) -> Option<Page<SimplifiedTrack>> {
self.api_with_retry(|api| {
api.album_track_manual(
&AlbumId::from_id(album_id).unwrap(),
AlbumId::from_id(album_id).unwrap(),
Some(limit),
Some(offset),
)
@@ -455,9 +427,9 @@ impl WebApi {
debug!("fetching artist {} albums, offset: {}", artist_id, offset);
spotify.api_with_retry(|api| {
match api.artist_albums_manual(
&ArtistId::from_id(&artist_id).unwrap(),
album_type.as_ref(),
Some(&Market::FromToken),
ArtistId::from_id(&artist_id).unwrap(),
album_type.as_ref().copied(),
Some(Market::FromToken),
Some(MAX_SIZE),
Some(offset),
) {
@@ -487,8 +459,8 @@ impl WebApi {
debug!("fetching show {} episodes, offset: {}", &show_id, offset);
spotify.api_with_retry(|api| {
match api.get_shows_episodes_manual(
&ShowId::from_id(&show_id).unwrap(),
Some(&Market::FromToken),
ShowId::from_id(&show_id).unwrap(),
Some(Market::FromToken),
Some(50),
Some(offset),
) {
@@ -512,8 +484,8 @@ impl WebApi {
pub fn save_shows(&self, ids: Vec<&str>) -> bool {
self.api_with_retry(|api| {
api.save_shows(
&ids.iter()
.map(|id| ShowId::from_id(id).unwrap())
ids.iter()
.map(|id| ShowId::from_id(id.clone()).unwrap())
.collect::<Vec<ShowId>>(),
)
})
@@ -523,10 +495,10 @@ impl WebApi {
pub fn unsave_shows(&self, ids: Vec<&str>) -> bool {
self.api_with_retry(|api| {
api.remove_users_saved_shows(
&ids.iter()
.map(|id| ShowId::from_id(id).unwrap())
ids.iter()
.map(|id| ShowId::from_id(id.clone()).unwrap())
.collect::<Vec<ShowId>>(),
Some(&Market::FromToken),
Some(Market::FromToken),
)
})
.is_some()
@@ -542,8 +514,8 @@ impl WebApi {
pub fn user_follow_artists(&self, ids: Vec<&str>) -> Option<()> {
self.api_with_retry(|api| {
api.user_follow_artists(
&ids.iter()
.map(|id| ArtistId::from_id(id).unwrap())
ids.iter()
.map(|id| ArtistId::from_id(id.clone()).unwrap())
.collect::<Vec<ArtistId>>(),
)
})
@@ -552,8 +524,8 @@ impl WebApi {
pub fn user_unfollow_artists(&self, ids: Vec<&str>) -> Option<()> {
self.api_with_retry(|api| {
api.user_unfollow_artists(
&ids.iter()
.map(|id| ArtistId::from_id(id).unwrap())
ids.iter()
.map(|id| ArtistId::from_id(id.clone()).unwrap())
.collect::<Vec<ArtistId>>(),
)
})
@@ -561,15 +533,15 @@ impl WebApi {
pub fn current_user_saved_albums(&self, offset: u32) -> Option<Page<SavedAlbum>> {
self.api_with_retry(|api| {
api.current_user_saved_albums_manual(Some(&Market::FromToken), Some(50), Some(offset))
api.current_user_saved_albums_manual(Some(Market::FromToken), Some(50), Some(offset))
})
}
pub fn current_user_saved_albums_add(&self, ids: Vec<&str>) -> Option<()> {
self.api_with_retry(|api| {
api.current_user_saved_albums_add(
&ids.iter()
.map(|id| AlbumId::from_id(id).unwrap())
ids.iter()
.map(|id| AlbumId::from_id(id.clone()).unwrap())
.collect::<Vec<AlbumId>>(),
)
})
@@ -578,8 +550,8 @@ impl WebApi {
pub fn current_user_saved_albums_delete(&self, ids: Vec<&str>) -> Option<()> {
self.api_with_retry(|api| {
api.current_user_saved_albums_delete(
&ids.iter()
.map(|id| AlbumId::from_id(id).unwrap())
ids.iter()
.map(|id| AlbumId::from_id(id.clone()).unwrap())
.collect::<Vec<AlbumId>>(),
)
})
@@ -587,15 +559,15 @@ impl WebApi {
pub fn current_user_saved_tracks(&self, offset: u32) -> Option<Page<SavedTrack>> {
self.api_with_retry(|api| {
api.current_user_saved_tracks_manual(Some(&Market::FromToken), Some(50), Some(offset))
api.current_user_saved_tracks_manual(Some(Market::FromToken), Some(50), Some(offset))
})
}
pub fn current_user_saved_tracks_add(&self, ids: Vec<&str>) -> Option<()> {
self.api_with_retry(|api| {
api.current_user_saved_tracks_add(
&ids.iter()
.map(|id| TrackId::from_id(id).unwrap())
ids.iter()
.map(|id| TrackId::from_id(id.clone()).unwrap())
.collect::<Vec<TrackId>>(),
)
})
@@ -604,26 +576,26 @@ impl WebApi {
pub fn current_user_saved_tracks_delete(&self, ids: Vec<&str>) -> Option<()> {
self.api_with_retry(|api| {
api.current_user_saved_tracks_delete(
&ids.iter()
.map(|id| TrackId::from_id(id).unwrap())
ids.iter()
.map(|id| TrackId::from_id(id.clone()).unwrap())
.collect::<Vec<TrackId>>(),
)
})
}
pub fn user_playlist_follow_playlist(&self, id: &str) -> Option<()> {
self.api_with_retry(|api| api.playlist_follow(&PlaylistId::from_id(id).unwrap(), None))
self.api_with_retry(|api| api.playlist_follow(PlaylistId::from_id(id).unwrap(), None))
}
pub fn artist_top_tracks(&self, id: &str) -> Option<Vec<Track>> {
self.api_with_retry(|api| {
api.artist_top_tracks(&ArtistId::from_id(id).unwrap(), &Market::FromToken)
api.artist_top_tracks(ArtistId::from_id(id).unwrap(), Market::FromToken)
})
.map(|ft| ft.iter().map(|t| t.into()).collect())
}
pub fn artist_related_artists(&self, id: &str) -> Option<Vec<Artist>> {
self.api_with_retry(|api| api.artist_related_artists(&ArtistId::from_id(id).unwrap()))
self.api_with_retry(|api| api.artist_related_artists(ArtistId::from_id(id).unwrap()))
.map(|fa| fa.iter().map(|a| a.into()).collect())
}
@@ -635,7 +607,7 @@ impl WebApi {
spotify.api_with_retry(|api| {
match api.categories_manual(
None,
Some(&Market::FromToken),
Some(Market::FromToken),
Some(MAX_LIMIT),
Some(offset),
) {
@@ -660,7 +632,7 @@ impl WebApi {
spotify.api_with_retry(|api| {
match api.category_playlists_manual(
&category_id,
Some(&Market::FromToken),
Some(Market::FromToken),
Some(MAX_LIMIT),
Some(offset),
) {