Skip to content

Commit

Permalink
Merge branch 'master' of github.com:samrayleung/rspotify
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsayleung committed Nov 10, 2018
2 parents afbebbe + 675a0a1 commit f8463a3
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 67 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- Hide warning on successful authentication
- Remove unneeded extern crate from examples/new_releases.rs
- Changes to Spotify.user_playlist and Spotify.playlist methods
- add new field `Unknown` for `DeviceType` enum
## 0.2.5 (2018/10/05)
- update reqwest to 0.9
## 0.2.4 (2018/08/19)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ console where your application is running. For example:
If you have a use case you are intertested in, you could check the
[examples](./examples), which has all kinds of detailed examples. For example,
If you want to get recently played history, you could check
[current_user_recently_played](./examples/current_user_recently_played). This is
[current_user_recently_played](./examples/current_user_recently_played.rs). This is
the example code:
``` rust
extern crate rspotify;
Expand Down
2 changes: 1 addition & 1 deletion examples/current_user_saved_tracks_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn main() {
Ok(_) => {
println!("saved tracks delete successful");
}
Err(_) => eprintln!("saved traks delete failed"),
Err(_) => eprintln!("saved tracks delete failed"),

}
}
Expand Down
43 changes: 43 additions & 0 deletions examples/playlist.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
extern crate rspotify;

use rspotify::spotify::client::Spotify;
use rspotify::spotify::util::get_token;
use rspotify::spotify::oauth2::{SpotifyClientCredentials, SpotifyOAuth};

fn main() {
// Set client_id and client_secret in .env file or
// export CLIENT_ID="your client_id"
// export CLIENT_SECRET="secret"
// export REDIRECT_URI=your-direct-uri

// Or set client_id, client_secret,redirect_uri explictly
// let oauth = SpotifyOAuth::default()
// .client_id("this-is-my-client-id")
// .client_secret("this-is-my-client-secret")
// .redirect_uri("http://localhost:8888/callback")
// .build();

let mut spotify_oauth = SpotifyOAuth::default().build();
match get_token(&mut spotify_oauth){
Some(token_info) => {
let client_credential = SpotifyClientCredentials::default()
.token_info(token_info)
.build();

// Or set client_id and client_secret explictly
// let client_credential = SpotifyClientCredentials::default()
// .client_id("this-is-my-client-id")
// .client_secret("this-is-my-client-secret")
// .build();
let spotify = Spotify::default()
.client_credentials_manager(client_credential)
.build();
let playlist_id = String::from("59ZbFPES4DQwEjBpWHzrtC");
let playlists = spotify.playlist(&playlist_id, None, None);
println!("{:?}", playlists);

}
None => println!("auth failed"),
};

}
2 changes: 1 addition & 1 deletion examples/user_playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() {
.build();
let user_id = "spotify";
let mut playlist_id = String::from("59ZbFPES4DQwEjBpWHzrtC");
let playlists = spotify.user_playlist(user_id, Some(&mut playlist_id), None);
let playlists = spotify.user_playlist(user_id, Some(&mut playlist_id), None, None);
println!("{:?}", playlists);

}
Expand Down
18 changes: 14 additions & 4 deletions src/spotify/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,19 +480,25 @@ impl Spotify {
self.convert_result::<PublicUser>(&result.unwrap_or_default())
}

//TODO: fields
///[get playlist](https://developer.spotify.com/documentation/web-api/reference/playlists/get-playlist/)
///Get full details about Spotify playlist
///Parameters:
///- playlist_id - the id of the playlist
///- market - an ISO 3166-1 alpha-2 country code.
pub fn playlist(&self, playlist_id: &str, market: Option<Country>) -> Result<FullPlaylist, failure::Error> {
pub fn playlist(&self,
playlist_id: &str,
fields: Option<&str>,
market: Option<Country>) -> Result<FullPlaylist, failure::Error> {
let mut params = HashMap::new();
if let Some(_fields) = fields {
params.insert("fields".to_owned(), _fields.to_string());
}
if let Some(_market) = market {
params.insert("market".to_owned(), _market.as_str().to_owned());
}

let url = format!("playlists/{}", playlist_id);
let plid = self.get_id(Type::Playlist, playlist_id);
let url = format!("playlists/{}", plid);
let result = self.get(&url, &mut params);
self.convert_result::<FullPlaylist>(&result.unwrap_or_default())
}
Expand Down Expand Up @@ -545,12 +551,16 @@ impl Spotify {
pub fn user_playlist(&self,
user_id: &str,
playlist_id: Option<&mut str>,
fields: Option<&str>)
fields: Option<&str>,
market: Option<Country>)
-> Result<FullPlaylist, failure::Error> {
let mut params = HashMap::new();
if let Some(_fields) = fields {
params.insert("fields".to_owned(), _fields.to_string());
}
if let Some(_market) = market {
params.insert("market".to_owned(), _market.as_str().to_owned());
}
match playlist_id {
Some(_playlist_id) => {
let plid = self.get_id(Type::Playlist, _playlist_id);
Expand Down
9 changes: 4 additions & 5 deletions src/spotify/oauth2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl SpotifyClientCredentials {
dotenv().ok();
let client_id = env::var("CLIENT_ID").unwrap_or_default();
let client_secret = env::var("CLIENT_SECRET").unwrap_or_default();
println!("SpotifyClientCredentials.default(): client_id:{:?}, client_secret:{:?}",client_id,client_secret);
SpotifyClientCredentials {
client_id: client_id,
client_secret: client_secret,
Expand All @@ -123,11 +124,9 @@ impl SpotifyClientCredentials {
CLIENT_SECRET='your-spotify-client-secret'
REDIRECT_URI='your-app-redirect-url'
Get your credentials at `https://developer.spotify.com/my-applications`";
let empty_flag = if self.client_id.is_empty() || self.client_secret.is_empty() {
true
} else {
self.token_info.is_none()
};
println!("SpotifyClientCredentials.default(): client_id:{:?}, client_secret:{:?} empty_flag:{:?}",self.client_id, self.client_secret, !(self.client_id.is_empty()||self.client_secret.is_empty())&&self.token_info.is_none());
let empty_flag = (self.client_id.is_empty() || self.client_secret.is_empty())
&& self.token_info.is_none();
if empty_flag {
eprintln!("{}", ERROR_MESSAGE);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/spotify/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn get_token(spotify_oauth: &mut SpotifyOAuth) -> Option<TokenInfo> {
let auth_url = spotify_oauth.get_authorize_url(Some(&state), None);
match webbrowser::open(&auth_url) {
Ok(_) => println!("Opened {} in your browser", auth_url),
Err(why) => eprintln!("Error {:?};Please naviage here [{:?}] ", why, auth_url),
Err(why) => eprintln!("Error {:?};Please navigate here [{:?}] ", why, auth_url),
}
println!("Enter the URL you were redirected to: ");
let mut input = String::new();
Expand Down
10 changes: 10 additions & 0 deletions tests/test_device.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extern crate reqwest;
extern crate rspotify;
use rspotify::spotify::client::Spotify;
use rspotify::spotify::oauth2::SpotifyClientCredentials;
use reqwest::Client;

//TODO waitting for mutable mockito test framework
#[test]#[ignore]
fn test_device(){
}
10 changes: 5 additions & 5 deletions tests/tests_with_credential.rs → tests/test_with_credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn test_artist() {
assert!(artist.is_ok());
}

#[test]
#[test]#[ignore]
fn test_artists_albums() {
let client_credential = SpotifyClientCredentials::default().build();
let spotify = Spotify::default()
Expand All @@ -96,7 +96,7 @@ fn test_artists() {
assert!(artists.is_ok());
}

#[test]
#[test]#[ignore]
fn test_artist_top_tracks() {
let client_credential = SpotifyClientCredentials::default().build();
let spotify = Spotify::default()
Expand All @@ -106,7 +106,7 @@ fn test_artist_top_tracks() {
let tracks = spotify.artist_top_tracks(birdy_uri, Country::UnitedStates);
assert!(tracks.is_ok());
}
#[test]
#[test]#[ignore]
fn test_audio_analysis() {
let client_credential = SpotifyClientCredentials::default().build();
let spotify = Spotify::default()
Expand Down Expand Up @@ -186,7 +186,7 @@ fn test_existing_playlist() {
.client_credentials_manager(client_credential)
.build();

let playlist = spotify.playlist("37i9dQZF1DZ06evO45P0Eo", None);
let playlist = spotify.playlist("37i9dQZF1DZ06evO45P0Eo", None, None);
assert!(playlist.is_ok());
}

Expand All @@ -198,6 +198,6 @@ fn test_fake_playlist() {
.client_credentials_manager(client_credential)
.build();

let playlist = spotify.playlist("fake_id", None);
let playlist = spotify.playlist("fake_id", None, None);
assert!(!playlist.is_ok());
}
Loading

0 comments on commit f8463a3

Please sign in to comment.