Skip to content

Commit

Permalink
Rename endpoint to api for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
marioortizmanero committed Dec 29, 2022
1 parent e6b141e commit 4cabf6a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 105 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.11.7 (Unreleased)

- ([#375](https://github.com/ramsayleung/rspotify/pull/375)) We now use `chrono::Duration` in more places for consistency and usability: `start_uris_playback`, `start_context_playback`, `rspotify_model::Offset`, `resume_playback`, `seek_track`. Some of these fields have been renamed from `position_ms` to `position`.
- ((#356)[https://github.com/ramsayleung/rspotify/pull/356]) We now support custom authentication base URLs. `Config::prefix` has been renamed to `Config::api_base_url`, and we've introduced `Config::auth_base_url`.

## 0.11.6 (2022.12.14)

Expand Down
2 changes: 1 addition & 1 deletion doc/uml/trait_hierarchy.plantuml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract class BaseClient {
---
ClientResult<()> auto_reauth()
ClientResult<()> refresh_token()
String endpoint_url()
String api_url()
String auth_url()
ClientResult<Headers> auth_headers()
ClientResult<()> write_token_cache()
Expand Down
84 changes: 41 additions & 43 deletions src/clients/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ where
/// be mutable (the token is accessed to from every endpoint).
fn get_token(&self) -> Arc<Mutex<Option<Token>>>;

/// Returns the absolute URL for an endpoint in the API
fn endpoint_url(&self, url: &str) -> String {
/// Returns the absolute URL for an endpoint in the API.
fn api_url(&self, url: &str) -> String {
let mut base = self.get_config().api_base_url.clone();
if !base.ends_with('/') {
base.push('/');
}
base + url
}

/// Returns the absolute URL for an authentication step in the API
/// Returns the absolute URL for an authentication step in the API.
fn auth_url(&self, url: &str) -> String {
let mut base = self.get_config().auth_base_url.clone();
if !base.ends_with('/') {
Expand Down Expand Up @@ -114,8 +114,8 @@ where
/// API.
#[doc(hidden)]
#[inline]
async fn endpoint_get(&self, url: &str, payload: &Query<'_>) -> ClientResult<String> {
let url = self.endpoint_url(url);
async fn api_get(&self, url: &str, payload: &Query<'_>) -> ClientResult<String> {
let url = self.api_url(url);
let headers = self.auth_headers().await;
Ok(self.get_http().get(&url, Some(&headers), payload).await?)
}
Expand All @@ -124,8 +124,8 @@ where
/// API.
#[doc(hidden)]
#[inline]
async fn endpoint_post(&self, url: &str, payload: &Value) -> ClientResult<String> {
let url = self.endpoint_url(url);
async fn api_post(&self, url: &str, payload: &Value) -> ClientResult<String> {
let url = self.api_url(url);
let headers = self.auth_headers().await;
Ok(self.get_http().post(&url, Some(&headers), payload).await?)
}
Expand All @@ -134,8 +134,8 @@ where
/// API.
#[doc(hidden)]
#[inline]
async fn endpoint_put(&self, url: &str, payload: &Value) -> ClientResult<String> {
let url = self.endpoint_url(url);
async fn api_put(&self, url: &str, payload: &Value) -> ClientResult<String> {
let url = self.api_url(url);
let headers = self.auth_headers().await;
Ok(self.get_http().put(&url, Some(&headers), payload).await?)
}
Expand All @@ -144,8 +144,8 @@ where
/// API.
#[doc(hidden)]
#[inline]
async fn endpoint_delete(&self, url: &str, payload: &Value) -> ClientResult<String> {
let url = self.endpoint_url(url);
async fn api_delete(&self, url: &str, payload: &Value) -> ClientResult<String> {
let url = self.api_url(url);
let headers = self.auth_headers().await;
Ok(self
.get_http()
Expand Down Expand Up @@ -207,7 +207,7 @@ where
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-track)
async fn track(&self, track_id: TrackId<'_>) -> ClientResult<FullTrack> {
let url = format!("tracks/{}", track_id.id());
let result = self.endpoint_get(&url, &Query::new()).await?;
let result = self.api_get(&url, &Query::new()).await?;
convert_result(&result)
}

Expand All @@ -227,7 +227,7 @@ where
let params = build_map([("market", market.map(Into::into))]);

let url = format!("tracks/?ids={ids}");
let result = self.endpoint_get(&url, &params).await?;
let result = self.api_get(&url, &params).await?;
convert_result::<FullTracks>(&result).map(|x| x.tracks)
}

Expand All @@ -239,7 +239,7 @@ where
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-an-artist)
async fn artist(&self, artist_id: ArtistId<'_>) -> ClientResult<FullArtist> {
let url = format!("artists/{}", artist_id.id());
let result = self.endpoint_get(&url, &Query::new()).await?;
let result = self.api_get(&url, &Query::new()).await?;
convert_result(&result)
}

Expand All @@ -255,7 +255,7 @@ where
) -> ClientResult<Vec<FullArtist>> {
let ids = join_ids(artist_ids);
let url = format!("artists/?ids={ids}");
let result = self.endpoint_get(&url, &Query::new()).await?;
let result = self.api_get(&url, &Query::new()).await?;

convert_result::<FullArtists>(&result).map(|x| x.artists)
}
Expand Down Expand Up @@ -313,7 +313,7 @@ where
]);

let url = format!("artists/{}/albums", artist_id.id());
let result = self.endpoint_get(&url, &params).await?;
let result = self.api_get(&url, &params).await?;
convert_result(&result)
}

Expand All @@ -333,7 +333,7 @@ where
let params = build_map([("market", Some(market.into()))]);

let url = format!("artists/{}/top-tracks", artist_id.id());
let result = self.endpoint_get(&url, &params).await?;
let result = self.api_get(&url, &params).await?;
convert_result::<FullTracks>(&result).map(|x| x.tracks)
}

Expand All @@ -350,7 +350,7 @@ where
artist_id: ArtistId<'_>,
) -> ClientResult<Vec<FullArtist>> {
let url = format!("artists/{}/related-artists", artist_id.id());
let result = self.endpoint_get(&url, &Query::new()).await?;
let result = self.api_get(&url, &Query::new()).await?;
convert_result::<FullArtists>(&result).map(|x| x.artists)
}

Expand All @@ -363,7 +363,7 @@ where
async fn album(&self, album_id: AlbumId<'_>) -> ClientResult<FullAlbum> {
let url = format!("albums/{}", album_id.id());

let result = self.endpoint_get(&url, &Query::new()).await?;
let result = self.api_get(&url, &Query::new()).await?;
convert_result(&result)
}

Expand All @@ -379,7 +379,7 @@ where
) -> ClientResult<Vec<FullAlbum>> {
let ids = join_ids(album_ids);
let url = format!("albums/?ids={ids}");
let result = self.endpoint_get(&url, &Query::new()).await?;
let result = self.api_get(&url, &Query::new()).await?;
convert_result::<FullAlbums>(&result).map(|x| x.albums)
}

Expand Down Expand Up @@ -418,7 +418,7 @@ where
("offset", offset.as_deref()),
]);

let result = self.endpoint_get("search", &params).await?;
let result = self.api_get("search", &params).await?;
convert_result(&result)
}

Expand Down Expand Up @@ -458,7 +458,7 @@ where
let params = build_map([("limit", limit.as_deref()), ("offset", offset.as_deref())]);

let url = format!("albums/{}/tracks", album_id.id());
let result = self.endpoint_get(&url, &params).await?;
let result = self.api_get(&url, &params).await?;
convert_result(&result)
}

Expand All @@ -470,7 +470,7 @@ where
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-users-profile)
async fn user(&self, user_id: UserId<'_>) -> ClientResult<PublicUser> {
let url = format!("users/{}", user_id.id());
let result = self.endpoint_get(&url, &Query::new()).await?;
let result = self.api_get(&url, &Query::new()).await?;
convert_result(&result)
}

Expand All @@ -490,7 +490,7 @@ where
let params = build_map([("fields", fields), ("market", market.map(Into::into))]);

let url = format!("playlists/{}", playlist_id.id());
let result = self.endpoint_get(&url, &params).await?;
let result = self.api_get(&url, &params).await?;
convert_result(&result)
}

Expand All @@ -514,7 +514,7 @@ where
Some(playlist_id) => format!("users/{}/playlists/{}", user_id.id(), playlist_id.id()),
None => format!("users/{}/starred", user_id.id()),
};
let result = self.endpoint_get(&url, &params).await?;
let result = self.api_get(&url, &params).await?;
convert_result(&result)
}

Expand All @@ -540,7 +540,7 @@ where
playlist_id.id(),
user_ids.iter().map(Id::id).collect::<Vec<_>>().join(","),
);
let result = self.endpoint_get(&url, &Query::new()).await?;
let result = self.api_get(&url, &Query::new()).await?;
convert_result(&result)
}

Expand All @@ -557,7 +557,7 @@ where
let params = build_map([("market", market.map(Into::into))]);

let url = format!("shows/{}", id.id());
let result = self.endpoint_get(&url, &params).await?;
let result = self.api_get(&url, &params).await?;
convert_result(&result)
}

Expand All @@ -577,7 +577,7 @@ where
let ids = join_ids(ids);
let params = build_map([("ids", Some(&ids)), ("market", market.map(Into::into))]);

let result = self.endpoint_get("shows", &params).await?;
let result = self.api_get("shows", &params).await?;
convert_result::<SeversalSimplifiedShows>(&result).map(|x| x.shows)
}

Expand Down Expand Up @@ -627,7 +627,7 @@ where
]);

let url = format!("shows/{}/episodes", id.id());
let result = self.endpoint_get(&url, &params).await?;
let result = self.api_get(&url, &params).await?;
convert_result(&result)
}

Expand All @@ -648,7 +648,7 @@ where
let url = format!("episodes/{}", id.id());
let params = build_map([("market", market.map(Into::into))]);

let result = self.endpoint_get(&url, &params).await?;
let result = self.api_get(&url, &params).await?;
convert_result(&result)
}

Expand All @@ -667,7 +667,7 @@ where
let ids = join_ids(ids);
let params = build_map([("ids", Some(&ids)), ("market", market.map(Into::into))]);

let result = self.endpoint_get("episodes", &params).await?;
let result = self.api_get("episodes", &params).await?;
convert_result::<EpisodesPayload>(&result).map(|x| x.episodes)
}

Expand All @@ -679,7 +679,7 @@ where
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-audio-features)
async fn track_features(&self, track_id: TrackId<'_>) -> ClientResult<AudioFeatures> {
let url = format!("audio-features/{}", track_id.id());
let result = self.endpoint_get(&url, &Query::new()).await?;
let result = self.api_get(&url, &Query::new()).await?;
convert_result(&result)
}

Expand All @@ -695,7 +695,7 @@ where
) -> ClientResult<Option<Vec<AudioFeatures>>> {
let url = format!("audio-features/?ids={}", join_ids(track_ids));

let result = self.endpoint_get(&url, &Query::new()).await?;
let result = self.api_get(&url, &Query::new()).await?;
if result.is_empty() {
Ok(None)
} else {
Expand All @@ -712,7 +712,7 @@ where
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-audio-analysis)
async fn track_analysis(&self, track_id: TrackId<'_>) -> ClientResult<AudioAnalysis> {
let url = format!("audio-analysis/{}", track_id.id());
let result = self.endpoint_get(&url, &Query::new()).await?;
let result = self.api_get(&url, &Query::new()).await?;
convert_result(&result)
}

Expand Down Expand Up @@ -758,7 +758,7 @@ where
("limit", limit.as_deref()),
("offset", offset.as_deref()),
]);
let result = self.endpoint_get("browse/categories", &params).await?;
let result = self.api_get("browse/categories", &params).await?;
convert_result::<PageCategory>(&result).map(|x| x.categories)
}

Expand Down Expand Up @@ -806,7 +806,7 @@ where
]);

let url = format!("browse/categories/{category_id}/playlists");
let result = self.endpoint_get(&url, &params).await?;
let result = self.api_get(&url, &params).await?;
convert_result::<CategoryPlaylists>(&result).map(|x| x.playlists)
}

Expand Down Expand Up @@ -846,9 +846,7 @@ where
("offset", offset.as_deref()),
]);

let result = self
.endpoint_get("browse/featured-playlists", &params)
.await?;
let result = self.api_get("browse/featured-playlists", &params).await?;
convert_result(&result)
}

Expand Down Expand Up @@ -890,7 +888,7 @@ where
("offset", offset.as_deref()),
]);

let result = self.endpoint_get("browse/new-releases", &params).await?;
let result = self.api_get("browse/new-releases", &params).await?;
convert_result::<PageSimplifiedAlbums>(&result).map(|x| x.albums)
}

Expand Down Expand Up @@ -945,7 +943,7 @@ where
// And finally adding all of them to the payload
params.extend(borrowed_attributes);

let result = self.endpoint_get("recommendations", &params).await?;
let result = self.api_get("recommendations", &params).await?;
convert_result(&result)
}

Expand Down Expand Up @@ -1002,7 +1000,7 @@ where
]);

let url = format!("playlists/{}/tracks", playlist_id.id());
let result = self.endpoint_get(&url, &params).await?;
let result = self.api_get(&url, &params).await?;
convert_result(&result)
}

Expand Down Expand Up @@ -1042,7 +1040,7 @@ where
let params = build_map([("limit", limit.as_deref()), ("offset", offset.as_deref())]);

let url = format!("users/{}/playlists", user_id.id());
let result = self.endpoint_get(&url, &params).await?;
let result = self.api_get(&url, &params).await?;
convert_result(&result)
}
}
8 changes: 4 additions & 4 deletions src/clients/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ mod test {
}

#[test]
fn test_endpoint_url() {
fn test_api_url() {
let mut spotify = ClientCredsSpotify::default();
assert_eq!(
spotify.endpoint_url("me/player/play"),
spotify.api_url("me/player/play"),
"https://api.spotify.com/v1/me/player/play"
);

Expand All @@ -67,7 +67,7 @@ mod test {
..Default::default()
};
assert_eq!(
spotify.endpoint_url("me/player/play"),
spotify.api_url("me/player/play"),
"http://localhost:8080/api/v1/me/player/play"
);

Expand All @@ -77,7 +77,7 @@ mod test {
..Default::default()
};
assert_eq!(
spotify.endpoint_url("me/player/play"),
spotify.api_url("me/player/play"),
"http://localhost:8080/api/v1/me/player/play"
);
}
Expand Down
Loading

0 comments on commit 4cabf6a

Please sign in to comment.