diff --git a/core/src/cdn_url.rs b/core/src/cdn_url.rs index 9df43ea9..e7016143 100644 --- a/core/src/cdn_url.rs +++ b/core/src/cdn_url.rs @@ -67,7 +67,7 @@ impl CdnUrl { pub async fn resolve_audio(&self, session: &Session) -> Result { let file_id = self.file_id; - let response = session.spclient().get_audio_storage(file_id).await?; + let response = session.spclient().get_audio_storage(&file_id).await?; let msg = CdnUrlMessage::parse_from_bytes(&response)?; let urls = MaybeExpiringUrls::try_from(msg)?; diff --git a/core/src/spclient.rs b/core/src/spclient.rs index 479c86e0..e65e0bff 100644 --- a/core/src/spclient.rs +++ b/core/src/spclient.rs @@ -235,7 +235,7 @@ impl SpClient { HeaderValue::from_static("application/x-protobuf"), ); - self.request(method, endpoint, Some(headers), Some(body)) + self.request(method, endpoint, Some(headers), Some(&body)) .await } @@ -244,7 +244,7 @@ impl SpClient { method: &Method, endpoint: &str, headers: Option, - body: Option, + body: Option<&str>, ) -> SpClientResult { let mut headers = headers.unwrap_or_else(HeaderMap::new); headers.insert(ACCEPT, HeaderValue::from_static("application/json")); @@ -257,7 +257,7 @@ impl SpClient { method: &Method, endpoint: &str, headers: Option, - body: Option, + body: Option<&str>, ) -> SpClientResult { let mut tries: usize = 0; let mut last_response; @@ -283,7 +283,7 @@ impl SpClient { let mut request = Request::builder() .method(method) .uri(url) - .body(Body::from(body.clone()))?; + .body(Body::from(body.to_owned()))?; // Reconnection logic: keep getting (cached) tokens because they might have expired. let token = self @@ -348,44 +348,44 @@ impl SpClient { pub async fn put_connect_state( &self, - connection_id: String, - state: PutStateRequest, + connection_id: &str, + state: &PutStateRequest, ) -> SpClientResult { let endpoint = format!("/connect-state/v1/devices/{}", self.session().device_id()); let mut headers = HeaderMap::new(); headers.insert("X-Spotify-Connection-Id", connection_id.parse()?); - self.request_with_protobuf(&Method::PUT, &endpoint, Some(headers), &state) + self.request_with_protobuf(&Method::PUT, &endpoint, Some(headers), state) .await } - pub async fn get_metadata(&self, scope: &str, id: SpotifyId) -> SpClientResult { + pub async fn get_metadata(&self, scope: &str, id: &SpotifyId) -> SpClientResult { let endpoint = format!("/metadata/4/{}/{}", scope, id.to_base16()?); self.request(&Method::GET, &endpoint, None, None).await } - pub async fn get_track_metadata(&self, track_id: SpotifyId) -> SpClientResult { + pub async fn get_track_metadata(&self, track_id: &SpotifyId) -> SpClientResult { self.get_metadata("track", track_id).await } - pub async fn get_episode_metadata(&self, episode_id: SpotifyId) -> SpClientResult { + pub async fn get_episode_metadata(&self, episode_id: &SpotifyId) -> SpClientResult { self.get_metadata("episode", episode_id).await } - pub async fn get_album_metadata(&self, album_id: SpotifyId) -> SpClientResult { + pub async fn get_album_metadata(&self, album_id: &SpotifyId) -> SpClientResult { self.get_metadata("album", album_id).await } - pub async fn get_artist_metadata(&self, artist_id: SpotifyId) -> SpClientResult { + pub async fn get_artist_metadata(&self, artist_id: &SpotifyId) -> SpClientResult { self.get_metadata("artist", artist_id).await } - pub async fn get_show_metadata(&self, show_id: SpotifyId) -> SpClientResult { + pub async fn get_show_metadata(&self, show_id: &SpotifyId) -> SpClientResult { self.get_metadata("show", show_id).await } - pub async fn get_lyrics(&self, track_id: SpotifyId) -> SpClientResult { + pub async fn get_lyrics(&self, track_id: &SpotifyId) -> SpClientResult { let endpoint = format!("/color-lyrics/v2/track/{}", track_id.to_base62()?); self.request_as_json(&Method::GET, &endpoint, None, None) @@ -394,8 +394,8 @@ impl SpClient { pub async fn get_lyrics_for_image( &self, - track_id: SpotifyId, - image_id: FileId, + track_id: &SpotifyId, + image_id: &FileId, ) -> SpClientResult { let endpoint = format!( "/color-lyrics/v2/track/{}/image/spotify:image:{}", @@ -407,7 +407,7 @@ impl SpClient { .await } - pub async fn get_playlist(&self, playlist_id: SpotifyId) -> SpClientResult { + pub async fn get_playlist(&self, playlist_id: &SpotifyId) -> SpClientResult { let endpoint = format!("/playlist/v2/playlist/{}", playlist_id); self.request(&Method::GET, &endpoint, None, None).await @@ -415,7 +415,7 @@ impl SpClient { pub async fn get_user_profile( &self, - username: String, + username: &str, playlist_limit: Option, artist_limit: Option, ) -> SpClientResult { @@ -440,14 +440,14 @@ impl SpClient { .await } - pub async fn get_user_followers(&self, username: String) -> SpClientResult { + pub async fn get_user_followers(&self, username: &str) -> SpClientResult { let endpoint = format!("/user-profile-view/v3/profile/{}/followers", username); self.request_as_json(&Method::GET, &endpoint, None, None) .await } - pub async fn get_user_following(&self, username: String) -> SpClientResult { + pub async fn get_user_following(&self, username: &str) -> SpClientResult { let endpoint = format!("/user-profile-view/v3/profile/{}/following", username); self.request_as_json(&Method::GET, &endpoint, None, None) @@ -466,9 +466,9 @@ impl SpClient { pub async fn get_apollo_station( &self, - context: SpotifyId, + context_uri: &str, count: u32, - previous_tracks: Vec, + previous_tracks: Vec<&SpotifyId>, autoplay: bool, ) -> SpClientResult { let previous_track_str = previous_tracks @@ -478,10 +478,7 @@ impl SpClient { .join(","); let endpoint = format!( "/radio-apollo/v3/stations/{}?count={}&prev_tracks={}&autoplay={}", - context.to_uri()?, - count, - previous_track_str, - autoplay, + context_uri, count, previous_track_str, autoplay, ); self.request_as_json(&Method::GET, &endpoint, None, None) @@ -501,7 +498,7 @@ impl SpClient { .await } - pub async fn get_audio_storage(&self, file_id: FileId) -> SpClientResult { + pub async fn get_audio_storage(&self, file_id: &FileId) -> SpClientResult { let endpoint = format!( "/storage-resolve/files/audio/interactive/{}", file_id.to_base16()? @@ -530,7 +527,7 @@ impl SpClient { Ok(stream) } - pub async fn request_url(&self, url: String) -> SpClientResult { + pub async fn request_url(&self, url: &str) -> SpClientResult { let request = Request::builder() .method(&Method::GET) .uri(url) @@ -554,11 +551,11 @@ impl SpClient { }; let _ = write!(url, "{}cid={}", separator, self.session().client_id()); - self.request_url(url).await + self.request_url(&url).await } // The first 128 kB of a track, unencrypted - pub async fn get_head_file(&self, file_id: FileId) -> SpClientResult { + pub async fn get_head_file(&self, file_id: &FileId) -> SpClientResult { let attribute = "head-files-url"; let template = self .session() @@ -567,10 +564,10 @@ impl SpClient { let url = template.replace("{file_id}", &file_id.to_base16()?); - self.request_url(url).await + self.request_url(&url).await } - pub async fn get_image(&self, image_id: FileId) -> SpClientResult { + pub async fn get_image(&self, image_id: &FileId) -> SpClientResult { let attribute = "image-url"; let template = self .session() @@ -578,6 +575,6 @@ impl SpClient { .ok_or_else(|| SpClientError::Attribute(attribute.to_string()))?; let url = template.replace("{file_id}", &image_id.to_base16()?); - self.request_url(url).await + self.request_url(&url).await } } diff --git a/core/src/spotify_id.rs b/core/src/spotify_id.rs index 66e3ac36..f1c1f1b8 100644 --- a/core/src/spotify_id.rs +++ b/core/src/spotify_id.rs @@ -329,10 +329,10 @@ impl NamedSpotifyId { Ok(dst) } - pub fn from_spotify_id(id: SpotifyId, username: String) -> Self { + pub fn from_spotify_id(id: SpotifyId, username: &str) -> Self { Self { inner_id: id, - username, + username: username.to_owned(), } } } diff --git a/metadata/src/album.rs b/metadata/src/album.rs index c2c439c2..45db39ea 100644 --- a/metadata/src/album.rs +++ b/metadata/src/album.rs @@ -76,11 +76,11 @@ impl Album { impl Metadata for Album { type Message = protocol::metadata::Album; - async fn request(session: &Session, album_id: SpotifyId) -> RequestResult { + async fn request(session: &Session, album_id: &SpotifyId) -> RequestResult { session.spclient().get_album_metadata(album_id).await } - fn parse(msg: &Self::Message, _: SpotifyId) -> Result { + fn parse(msg: &Self::Message, _: &SpotifyId) -> Result { Self::try_from(msg) } } diff --git a/metadata/src/artist.rs b/metadata/src/artist.rs index db297b59..1a7e1862 100644 --- a/metadata/src/artist.rs +++ b/metadata/src/artist.rs @@ -171,11 +171,11 @@ impl Artist { impl Metadata for Artist { type Message = protocol::metadata::Artist; - async fn request(session: &Session, artist_id: SpotifyId) -> RequestResult { + async fn request(session: &Session, artist_id: &SpotifyId) -> RequestResult { session.spclient().get_artist_metadata(artist_id).await } - fn parse(msg: &Self::Message, _: SpotifyId) -> Result { + fn parse(msg: &Self::Message, _: &SpotifyId) -> Result { Self::try_from(msg) } } diff --git a/metadata/src/episode.rs b/metadata/src/episode.rs index e3a5c7a3..e65a1045 100644 --- a/metadata/src/episode.rs +++ b/metadata/src/episode.rs @@ -60,7 +60,7 @@ impl_deref_wrapped!(Episodes, Vec); #[async_trait] impl InnerAudioItem for Episode { async fn get_audio_item(session: &Session, id: SpotifyId) -> AudioItemResult { - let episode = Self::get(session, id).await?; + let episode = Self::get(session, &id).await?; let availability = Self::available_for_user( &session.user_data(), &episode.availability, @@ -84,11 +84,11 @@ impl InnerAudioItem for Episode { impl Metadata for Episode { type Message = protocol::metadata::Episode; - async fn request(session: &Session, episode_id: SpotifyId) -> RequestResult { + async fn request(session: &Session, episode_id: &SpotifyId) -> RequestResult { session.spclient().get_episode_metadata(episode_id).await } - fn parse(msg: &Self::Message, _: SpotifyId) -> Result { + fn parse(msg: &Self::Message, _: &SpotifyId) -> Result { Self::try_from(msg) } } diff --git a/metadata/src/lib.rs b/metadata/src/lib.rs index 577af387..ef8443db 100644 --- a/metadata/src/lib.rs +++ b/metadata/src/lib.rs @@ -42,15 +42,15 @@ pub trait Metadata: Send + Sized + 'static { type Message: protobuf::Message; // Request a protobuf - async fn request(session: &Session, id: SpotifyId) -> RequestResult; + async fn request(session: &Session, id: &SpotifyId) -> RequestResult; // Request a metadata struct - async fn get(session: &Session, id: SpotifyId) -> Result { + async fn get(session: &Session, id: &SpotifyId) -> Result { let response = Self::request(session, id).await?; let msg = Self::Message::parse_from_bytes(&response)?; trace!("Received metadata: {:#?}", msg); Self::parse(&msg, id) } - fn parse(msg: &Self::Message, _: SpotifyId) -> Result; + fn parse(msg: &Self::Message, _: &SpotifyId) -> Result; } diff --git a/metadata/src/playlist/annotation.rs b/metadata/src/playlist/annotation.rs index fd8863cf..9cb7f144 100644 --- a/metadata/src/playlist/annotation.rs +++ b/metadata/src/playlist/annotation.rs @@ -27,12 +27,12 @@ pub struct PlaylistAnnotation { impl Metadata for PlaylistAnnotation { type Message = protocol::playlist_annotate3::PlaylistAnnotation; - async fn request(session: &Session, playlist_id: SpotifyId) -> RequestResult { + async fn request(session: &Session, playlist_id: &SpotifyId) -> RequestResult { let current_user = session.username(); Self::request_for_user(session, ¤t_user, playlist_id).await } - fn parse(msg: &Self::Message, _: SpotifyId) -> Result { + fn parse(msg: &Self::Message, _: &SpotifyId) -> Result { Ok(Self { description: msg.get_description().to_owned(), picture: msg.get_picture().to_owned(), // TODO: is this a URL or Spotify URI? @@ -47,7 +47,7 @@ impl PlaylistAnnotation { async fn request_for_user( session: &Session, username: &str, - playlist_id: SpotifyId, + playlist_id: &SpotifyId, ) -> RequestResult { let uri = format!( "hm://playlist-annotate/v1/annotation/user/{}/playlist/{}", @@ -61,7 +61,7 @@ impl PlaylistAnnotation { async fn get_for_user( session: &Session, username: &str, - playlist_id: SpotifyId, + playlist_id: &SpotifyId, ) -> Result { let response = Self::request_for_user(session, username, playlist_id).await?; let msg = ::Message::parse_from_bytes(&response)?; diff --git a/metadata/src/playlist/list.rs b/metadata/src/playlist/list.rs index 414e1fda..c813a14d 100644 --- a/metadata/src/playlist/list.rs +++ b/metadata/src/playlist/list.rs @@ -97,14 +97,14 @@ impl Playlist { impl Metadata for Playlist { type Message = protocol::playlist4_external::SelectedListContent; - async fn request(session: &Session, playlist_id: SpotifyId) -> RequestResult { + async fn request(session: &Session, playlist_id: &SpotifyId) -> RequestResult { session.spclient().get_playlist(playlist_id).await } - fn parse(msg: &Self::Message, id: SpotifyId) -> Result { + fn parse(msg: &Self::Message, id: &SpotifyId) -> Result { // the playlist proto doesn't contain the id so we decorate it let playlist = SelectedListContent::try_from(msg)?; - let id = NamedSpotifyId::from_spotify_id(id, playlist.owner_username); + let id = NamedSpotifyId::from_spotify_id(*id, &playlist.owner_username); Ok(Self { id, diff --git a/metadata/src/show.rs b/metadata/src/show.rs index 19e910d8..0d3acef8 100644 --- a/metadata/src/show.rs +++ b/metadata/src/show.rs @@ -39,11 +39,11 @@ pub struct Show { impl Metadata for Show { type Message = protocol::metadata::Show; - async fn request(session: &Session, show_id: SpotifyId) -> RequestResult { + async fn request(session: &Session, show_id: &SpotifyId) -> RequestResult { session.spclient().get_show_metadata(show_id).await } - fn parse(msg: &Self::Message, _: SpotifyId) -> Result { + fn parse(msg: &Self::Message, _: &SpotifyId) -> Result { Self::try_from(msg) } } diff --git a/metadata/src/track.rs b/metadata/src/track.rs index 7d416f14..f4855d8a 100644 --- a/metadata/src/track.rs +++ b/metadata/src/track.rs @@ -61,7 +61,7 @@ impl_deref_wrapped!(Tracks, Vec); #[async_trait] impl InnerAudioItem for Track { async fn get_audio_item(session: &Session, id: SpotifyId) -> AudioItemResult { - let track = Self::get(session, id).await?; + let track = Self::get(session, &id).await?; let alternatives = { if track.alternatives.is_empty() { None @@ -98,11 +98,11 @@ impl InnerAudioItem for Track { impl Metadata for Track { type Message = protocol::metadata::Track; - async fn request(session: &Session, track_id: SpotifyId) -> RequestResult { + async fn request(session: &Session, track_id: &SpotifyId) -> RequestResult { session.spclient().get_track_metadata(track_id).await } - fn parse(msg: &Self::Message, _: SpotifyId) -> Result { + fn parse(msg: &Self::Message, _: &SpotifyId) -> Result { Self::try_from(msg) } }