Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing optional market field to some APIs #432

Merged
merged 6 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
**Breaking changes**
- ([#409](https://github.com/ramsayleung/rspotify/pull/409)) Change type of `position` parameter in `playlist_add_items` endpoint from `Opinion<Duration>` to `Opinion<u32>`
- ([#421](https://github.com/ramsayleung/rspotify/issues/421)) Change type of `AudioFeaturesPayload.audio_features` from `Vec<AudioFeatures>` to `Vec<Option<AudioFeatures>>`
- ([#432](https://github.com/ramsayleung/rspotify/pull/432)) Add optional `market` field to `track`, `album`, `albums`, and `album_track[_manual]`. Make `market` field in `artist_top_tracks` optional.

**Bugfixes**
- ([#419](https://github.com/ramsayleung/rspotify/issues/419)) Base64url encode instead of plain base64 encode for PKCE challenge code.
Expand Down
2 changes: 1 addition & 1 deletion examples/client_creds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async fn main() {

// Running the requests
let birdy_uri = AlbumId::from_uri("spotify:album:0sNOF9WDwhWunNAHPD3Baj").unwrap();
let albums = spotify.album(birdy_uri).await;
let albums = spotify.album(birdy_uri, None).await;

println!("Response: {albums:#?}");
}
2 changes: 1 addition & 1 deletion examples/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async fn main() {
let spotify = Arc::clone(&spotify);
let wr = wr.clone();
let handle = task::spawn(async move {
let albums = spotify.album(id).await.unwrap();
let albums = spotify.album(id, None).await.unwrap();
wr.send(albums).unwrap();
});

Expand Down
2 changes: 1 addition & 1 deletion examples/ureq/threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() {
let spotify = Arc::clone(&spotify);
let wr = wr.clone();
let handle = thread::spawn(move || {
let albums = spotify.album(id).unwrap();
let albums = spotify.album(id, None).unwrap();
wr.send(albums).unwrap();
});

Expand Down
4 changes: 2 additions & 2 deletions examples/with_auto_reauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ async fn auth_code_do_things(spotify: &AuthCodeSpotify) {
async fn client_creds_do_things(spotify: &ClientCredsSpotify) {
// Running the requests
let birdy_uri = AlbumId::from_uri("spotify:album:0sNOF9WDwhWunNAHPD3Baj").unwrap();
let albums = spotify.album(birdy_uri).await;
let albums = spotify.album(birdy_uri, None).await;
println!("Get albums: {}", albums.unwrap().id);
}

async fn expire_token<S: BaseClient>(spotify: &S) {
let token_mutex = spotify.get_token();
let mut token = token_mutex.lock().await.unwrap();
let mut token = token.as_mut().expect("Token can't be empty as this point");
let token = token.as_mut().expect("Token can't be empty as this point");
// In a regular case, the token would expire with time. Here we just do
// it manually.
let now = Utc::now().checked_sub_signed(Duration::seconds(10));
Expand Down
40 changes: 30 additions & 10 deletions src/clients/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,15 @@ where
/// - track_id - a spotify URI, URL or ID
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-track)
async fn track(&self, track_id: TrackId<'_>) -> ClientResult<FullTrack> {
async fn track(
&self,
track_id: TrackId<'_>,
market: Option<Market>,
) -> ClientResult<FullTrack> {
let params = build_map([("market", market.map(Into::into))]);

let url = format!("tracks/{}", track_id.id());
let result = self.api_get(&url, &Query::new()).await?;
let result = self.api_get(&url, &params).await?;
convert_result(&result)
}

Expand Down Expand Up @@ -338,9 +344,9 @@ where
async fn artist_top_tracks(
&self,
artist_id: ArtistId<'_>,
market: Market,
market: Option<Market>,
) -> ClientResult<Vec<FullTrack>> {
let params = build_map([("market", Some(market.into()))]);
let params = build_map([("market", market.map(Into::into))]);

let url = format!("artists/{}/top-tracks", artist_id.id());
let result = self.api_get(&url, &params).await?;
Expand Down Expand Up @@ -370,10 +376,15 @@ where
/// - album_id - the album ID, URI or URL
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-an-album)
async fn album(&self, album_id: AlbumId<'_>) -> ClientResult<FullAlbum> {
let url = format!("albums/{}", album_id.id());
async fn album(
&self,
album_id: AlbumId<'_>,
market: Option<Market>,
) -> ClientResult<FullAlbum> {
let params = build_map([("market", market.map(Into::into))]);

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

Expand All @@ -386,10 +397,13 @@ where
async fn albums<'a>(
&self,
album_ids: impl IntoIterator<Item = AlbumId<'a>> + Send + 'a,
market: Option<Market>,
) -> ClientResult<Vec<FullAlbum>> {
let params = build_map([("market", market.map(Into::into))]);

let ids = join_ids(album_ids);
let url = format!("albums/?ids={ids}");
let result = self.api_get(&url, &Query::new()).await?;
let result = self.api_get(&url, &params).await?;
convert_result::<FullAlbums>(&result).map(|x| x.albums)
}

Expand Down Expand Up @@ -446,11 +460,12 @@ where
fn album_track<'a>(
&'a self,
album_id: AlbumId<'a>,
market: Option<Market>,
) -> Paginator<'_, ClientResult<SimplifiedTrack>> {
paginate_with_ctx(
(self, album_id),
move |(slf, album_id), limit, offset| {
slf.album_track_manual(album_id.as_ref(), Some(limit), Some(offset))
slf.album_track_manual(album_id.as_ref(), market, Some(limit), Some(offset))
},
self.get_config().pagination_chunks,
)
Expand All @@ -460,12 +475,17 @@ where
async fn album_track_manual(
&self,
album_id: AlbumId<'_>,
market: Option<Market>,
limit: Option<u32>,
offset: Option<u32>,
) -> ClientResult<Page<SimplifiedTrack>> {
let limit = limit.map(|s| s.to_string());
let offset = offset.map(|s| s.to_string());
let params = build_map([("limit", limit.as_deref()), ("offset", offset.as_deref())]);
let params = build_map([
("limit", limit.as_deref()),
("offset", offset.as_deref()),
("market", market.map(Into::into)),
]);

let url = format!("albums/{}/tracks", album_id.id());
let result = self.api_get(&url, &params).await?;
Expand Down
14 changes: 7 additions & 7 deletions tests/test_with_credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub async fn creds_client() -> ClientCredsSpotify {
#[maybe_async::test(feature = "__sync", async(feature = "__async", tokio::test))]
async fn test_album() {
let birdy_uri = AlbumId::from_uri("spotify:album:0sNOF9WDwhWunNAHPD3Baj").unwrap();
creds_client().await.album(birdy_uri).await.unwrap();
creds_client().await.album(birdy_uri, None).await.unwrap();
}

#[maybe_async::test(feature = "__sync", async(feature = "__async", tokio::test))]
Expand All @@ -36,15 +36,15 @@ async fn test_albums() {
AlbumId::from_uri("spotify:album:6JWc4iAiJ9FjyK0B59ABb4").unwrap(),
AlbumId::from_uri("spotify:album:6UXCm6bOO4gFlDQZV5yL37").unwrap(),
];
creds_client().await.albums(track_uris).await.unwrap();
creds_client().await.albums(track_uris, None).await.unwrap();
}

#[maybe_async::test(feature = "__sync", async(feature = "__async", tokio::test))]
async fn test_album_tracks() {
let birdy_uri = AlbumId::from_uri("spotify:album:6akEvsycLGftJxYudPjmqK").unwrap();
creds_client()
.await
.album_track_manual(birdy_uri, Some(2), None)
.album_track_manual(birdy_uri, None, Some(2), None)
.await
.unwrap();
}
Expand Down Expand Up @@ -133,7 +133,7 @@ async fn test_artist_top_tracks() {
let birdy_uri = ArtistId::from_uri("spotify:artist:2WX2uTcsvV5OnS0inACecP").unwrap();
creds_client()
.await
.artist_top_tracks(birdy_uri, Market::Country(Country::UnitedStates))
.artist_top_tracks(birdy_uri, Some(Market::Country(Country::UnitedStates)))
.await
.unwrap();
}
Expand Down Expand Up @@ -175,7 +175,7 @@ async fn test_user() {
#[maybe_async::test(feature = "__sync", async(feature = "__async", tokio::test))]
async fn test_track() {
let birdy_uri = TrackId::from_uri("spotify:track:6rqhFgbbKwnb9MLmUQDhG6").unwrap();
creds_client().await.track(birdy_uri).await.unwrap();
creds_client().await.track(birdy_uri, None).await.unwrap();
}

#[maybe_async::test(feature = "__sync", async(feature = "__async", tokio::test))]
Expand Down Expand Up @@ -230,7 +230,7 @@ mod test_pagination {
let album = AlbumId::from_uri(ALBUM).unwrap();

let names = client
.album_track(album)
.album_track(album, None)
.map(|track| track.unwrap().name)
.collect::<Vec<_>>();

Expand All @@ -248,7 +248,7 @@ mod test_pagination {
let album = AlbumId::from_uri(ALBUM).unwrap();

let names = client
.album_track(album)
.album_track(album, None)
.map(|track| track.unwrap().name)
.collect::<Vec<_>>()
.await;
Expand Down