Skip to content

Commit

Permalink
Merge pull request #244 from ramsayleung/fix-ids-4
Browse files Browse the repository at this point in the history
Fix IDs v4
  • Loading branch information
marioortizmanero authored Sep 15, 2021
2 parents e762c5a + 98c299c commit 89b3721
Show file tree
Hide file tree
Showing 26 changed files with 719 additions and 535 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,16 @@ More in the [`examples` directory](https://github.com/ramsayleung/rspotify/tree/
- The `Spotify` client has been split up by authorization flows (`ClientCredsSpotify`, `AuthCodeSpotify`, `AuthCodePkceSpotify`), which allows us to remove the builder pattern. The authentication process has been rewritten. ([#216](https://github.com/ramsayleung/rspotify/pull/216)).
- Fix typo in `user_playlist_remove_specific_occurrenes_of_tracks`, now it's `user_playlist_remove_specific_occurrences_of_tracks`.
- ([#123](https://github.com/ramsayleung/rspotify/pull/123)) All fallible calls in the client return a `ClientError` rather than using `failure`.
- ([#161](https://github.com/ramsayleung/rspotify/pull/161)) Endpoints taking `Vec<String>/&[String]` as parameter have changed to `impl IntoIterator<Item = &Id<Type>>`.
+ The endpoints which changes parameter from `Vec<String>` to `impl IntoIterator<Item = &Id<Type>>`:
- ([#244](https://github.com/ramsayleung/rspotify/pull/244)) Model objects like `FullTrack` or `AudioFeatures` have had their `_type` and `uri` fields removed. These can be accessed instead with the `id` field: `id._type()` or `id.uri()`.
- ([#244](https://github.com/ramsayleung/rspotify/pull/244)) Endpoints taking `Vec<String>/&[String]` as parameter have changed to `impl IntoIterator<Item = &Id>`.
+ The endpoints which changes parameter from `Vec<String>` to `impl IntoIterator<Item = &Id>`:
- `albums`
- `artists`
- `check_users_saved_shows`
- `get_several_episodes`
- `remove_users_saved_shows`
- `save_shows`
+ The endpoints which changes parameter from `&[String]` to `impl IntoIterator<Item = &Id<Type>>`:
+ The endpoints which changes parameter from `&[String]` to `impl IntoIterator<Item = &Id>`:
- `audios_features`
- `current_user_saved_albums_add`
- `current_user_saved_albums_contains`
Expand All @@ -209,7 +210,7 @@ More in the [`examples` directory](https://github.com/ramsayleung/rspotify/tree/
- `user_playlist_replace_tracks`
- `user_unfollow_artists`
- `user_unfollow_users`
+ The endpoints which changes parameter from `String` to `&Id<Type>`:
+ The endpoints which changes parameter from `String` to `&Id`:
- `get_a_show`
- `get_an_episode`
- `get_shows_episodes`
Expand Down Expand Up @@ -244,7 +245,7 @@ More in the [`examples` directory](https://github.com/ramsayleung/rspotify/tree/
+ Change `{FullArtist, FullPlaylist, PublicUser, PrivateUser}::followers` from `HashMap<String, Option<Value>>` to struct `Followers`
+ Replace `Actions::disallows` with a `Vec<DisallowKey>` by removing all entires whose value is false, which will result in a simpler API
+ Replace `{FullAlbum, SimplifiedEpisode, FullEpisode}::release_date_precision` from `String` to `DatePrecision` enum, makes it easier to use.
+ Id and URI parameters are type-safe now everywhere, `Id<Type>` and `IdBuf<Type>` types for ids/URIs added (non-owning and owning structs).
+ Id and URI parameters are type-safe now everywhere thanks to the `Id` trait and its implementations.
- ([#157](https://github.com/ramsayleung/rspotify/pull/157)) Keep polishing models to make it easier to use:
+ Constrain visibility of `FullArtists` struct with `pub (in crate)`, make `artists` and `artist_related_artists` endpoints return a `Vec<FullArtist>` instead.
+ Constrain visibility of `FullTracks` struct with `pub (in crate)`, make `tracks` and `artist_top_tracks` endpoints return a `Vec<FullTrack>` instead.
Expand Down
6 changes: 3 additions & 3 deletions examples/client_creds.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rspotify::{model::Id, prelude::*, ClientCredsSpotify, Credentials};
use rspotify::{model::AlbumId, prelude::*, ClientCredsSpotify, Credentials};

#[tokio::main]
async fn main() {
Expand Down Expand Up @@ -31,8 +31,8 @@ async fn main() {
spotify.request_token().await.unwrap();

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

println!("Response: {:#?}", albums);
}
14 changes: 7 additions & 7 deletions examples/with_refresh_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
//! tokens](https://github.com/felix-hilden/tekore/issues/86), so in the case of
//! Spotify it doesn't seem to revoke them at all.
use rspotify::{model::Id, prelude::*, scopes, AuthCodeSpotify, Credentials, OAuth};
use rspotify::{model::ArtistId, prelude::*, scopes, AuthCodeSpotify, Credentials, OAuth};

// Sample request that will follow some artists, print the user's
// followed artists, and then unfollow the artists.
async fn do_things(spotify: AuthCodeSpotify) {
let artists = vec![
Id::from_id("3RGLhK1IP9jnYFH4BRFJBS").unwrap(), // The Clash
Id::from_id("0yNLKJebCb8Aueb54LYya3").unwrap(), // New Order
Id::from_id("2jzc5TC5TVFLXQlBNiIUzE").unwrap(), // a-ha
let artists = [
&ArtistId::from_id("3RGLhK1IP9jnYFH4BRFJBS").unwrap(), // The Clash
&ArtistId::from_id("0yNLKJebCb8Aueb54LYya3").unwrap(), // New Order
&ArtistId::from_id("2jzc5TC5TVFLXQlBNiIUzE").unwrap(), // a-ha
];
spotify
.user_follow_artists(artists.clone())
.user_follow_artists(artists)
.await
.expect("couldn't follow artists");
println!("Followed {} artists successfully.", artists.len());
Expand All @@ -42,7 +42,7 @@ async fn do_things(spotify: AuthCodeSpotify) {
);

spotify
.user_unfollow_artists(artists.clone())
.user_unfollow_artists(artists)
.await
.expect("couldn't unfollow artists");
println!("Unfollowed {} artists successfully.", artists.len());
Expand Down
19 changes: 6 additions & 13 deletions rspotify-model/src/album.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use serde::{Deserialize, Serialize};

use std::collections::HashMap;

use super::artist::SimplifiedArtist;
use super::image::Image;
use super::page::Page;
use super::track::SimplifiedTrack;
use crate::{AlbumType, Copyright, DatePrecision, RestrictionReason, Type};
use crate::{
AlbumId, AlbumType, Copyright, DatePrecision, Image, Page, RestrictionReason, SimplifiedArtist,
SimplifiedTrack,
};

/// Simplified Album Object
///
Expand All @@ -24,7 +23,7 @@ pub struct SimplifiedAlbum {
pub available_markets: Vec<String>,
pub external_urls: HashMap<String, String>,
pub href: Option<String>,
pub id: Option<String>,
pub id: Option<AlbumId>,
pub images: Vec<Image>,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -33,9 +32,6 @@ pub struct SimplifiedAlbum {
pub release_date_precision: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub restrictions: Option<Restriction>,
#[serde(rename = "type")]
pub _type: Type,
pub uri: Option<String>,
}

/// Full Album Object
Expand All @@ -51,16 +47,13 @@ pub struct FullAlbum {
pub external_urls: HashMap<String, String>,
pub genres: Vec<String>,
pub href: String,
pub id: String,
pub id: AlbumId,
pub images: Vec<Image>,
pub name: String,
pub popularity: u32,
pub release_date: String,
pub release_date_precision: DatePrecision,
pub tracks: Page<SimplifiedTrack>,
#[serde(rename = "type")]
pub _type: Type,
pub uri: String,
}

/// Full Albums wrapped by Vec object
Expand Down
15 changes: 4 additions & 11 deletions rspotify-model/src/artist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@
use serde::{Deserialize, Serialize};

use super::image::Image;
use super::page::CursorBasedPage;
use crate::{Followers, Type};
use std::collections::HashMap;

use crate::{ArtistId, CursorBasedPage, Followers, Image};

/// Simplified Artist Object
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#object-simplifiedartistobject)
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct SimplifiedArtist {
pub external_urls: HashMap<String, String>,
pub href: Option<String>,
pub id: Option<String>,
pub id: Option<ArtistId>,
pub name: String,
#[serde(rename = "type")]
pub _type: Type,
pub uri: Option<String>,
}

/// Full Artist Object
Expand All @@ -30,13 +26,10 @@ pub struct FullArtist {
pub followers: Followers,
pub genres: Vec<String>,
pub href: String,
pub id: String,
pub id: ArtistId,
pub images: Vec<Image>,
pub name: String,
pub popularity: u32,
#[serde(rename = "type")]
pub _type: Type,
pub uri: String,
}

/// Full artist object wrapped by `Vec`
Expand Down
13 changes: 6 additions & 7 deletions rspotify-model/src/audio.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! All objects related to audio defined by Spotify API
use serde::{Deserialize, Serialize};

use std::time::Duration;

use crate::{
custom_serde::{duration_ms, modality},
enums::Modality,
Modality, TrackId,
};
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Audio Feature Object
///
Expand All @@ -18,7 +20,7 @@ pub struct AudioFeatures {
#[serde(with = "duration_ms", rename = "duration_ms")]
pub duration: Duration,
pub energy: f32,
pub id: String,
pub id: TrackId,
pub instrumentalness: f32,
pub key: i32,
pub liveness: f32,
Expand All @@ -29,9 +31,6 @@ pub struct AudioFeatures {
pub tempo: f32,
pub time_signature: i32,
pub track_href: String,
#[serde(rename = "type")]
pub _type: String,
pub uri: String,
pub valence: f32,
}

Expand Down
4 changes: 2 additions & 2 deletions rspotify-model/src/category.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! All object related to category
use super::image::Image;
use super::page::Page;
use serde::{Deserialize, Serialize};

use crate::{Image, Page};

/// Category object
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-categories)
Expand Down
13 changes: 7 additions & 6 deletions rspotify-model/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
//! All objects related to context
use super::device::Device;
use super::PlayableItem;
use crate::{
custom_serde::{millisecond_timestamp, option_duration_ms},
CurrentlyPlayingType, DisallowKey, RepeatState, Type,
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Deserializer, Serialize};

use std::collections::HashMap;
use std::time::Duration;

use crate::{
custom_serde::{millisecond_timestamp, option_duration_ms},
CurrentlyPlayingType, Device, DisallowKey, PlayableItem, RepeatState, Type,
};

/// Context object
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-recently-played)
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Context {
/// The URI may be of any type, so it's not parsed into a [`crate::Id`]
pub uri: String,
pub href: String,
pub external_urls: HashMap<String, String>,
Expand Down
2 changes: 1 addition & 1 deletion rspotify-model/src/custom_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub mod option_duration_ms {

/// Deserialize/Serialize `Modality` to integer(0, 1, -1).
pub mod modality {
use crate::enums::Modality;
use crate::Modality;
use serde::{de, Deserialize, Serializer};

pub fn deserialize<'de, D>(d: D) -> Result<Modality, D::Error>
Expand Down
Loading

0 comments on commit 89b3721

Please sign in to comment.