Pass by reference

This commit is contained in:
Roderick van Domburg 2022-08-03 22:26:52 +02:00
parent 70eb3f9d72
commit 80f0d3c59b
No known key found for this signature in database
GPG key ID: 87F5FDE8A56219F4
11 changed files with 55 additions and 58 deletions

View file

@ -67,7 +67,7 @@ impl CdnUrl {
pub async fn resolve_audio(&self, session: &Session) -> Result<Self, Error> {
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)?;

View file

@ -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<HeaderMap>,
body: Option<String>,
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<HeaderMap>,
body: Option<String>,
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<u32>,
artist_limit: Option<u32>,
) -> 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<SpotifyId>,
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
}
}

View file

@ -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(),
}
}
}

View file

@ -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<Self, Error> {
fn parse(msg: &Self::Message, _: &SpotifyId) -> Result<Self, Error> {
Self::try_from(msg)
}
}

View file

@ -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<Self, Error> {
fn parse(msg: &Self::Message, _: &SpotifyId) -> Result<Self, Error> {
Self::try_from(msg)
}
}

View file

@ -60,7 +60,7 @@ impl_deref_wrapped!(Episodes, Vec<SpotifyId>);
#[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<Self, Error> {
fn parse(msg: &Self::Message, _: &SpotifyId) -> Result<Self, Error> {
Self::try_from(msg)
}
}

View file

@ -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<Self, Error> {
async fn get(session: &Session, id: &SpotifyId) -> Result<Self, Error> {
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<Self, Error>;
fn parse(msg: &Self::Message, _: &SpotifyId) -> Result<Self, Error>;
}

View file

@ -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, &current_user, playlist_id).await
}
fn parse(msg: &Self::Message, _: SpotifyId) -> Result<Self, Error> {
fn parse(msg: &Self::Message, _: &SpotifyId) -> Result<Self, Error> {
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<Self, Error> {
let response = Self::request_for_user(session, username, playlist_id).await?;
let msg = <Self as Metadata>::Message::parse_from_bytes(&response)?;

View file

@ -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<Self, Error> {
fn parse(msg: &Self::Message, id: &SpotifyId) -> Result<Self, Error> {
// 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,

View file

@ -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<Self, Error> {
fn parse(msg: &Self::Message, _: &SpotifyId) -> Result<Self, Error> {
Self::try_from(msg)
}
}

View file

@ -61,7 +61,7 @@ impl_deref_wrapped!(Tracks, Vec<SpotifyId>);
#[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<Self, Error> {
fn parse(msg: &Self::Message, _: &SpotifyId) -> Result<Self, Error> {
Self::try_from(msg)
}
}