Skip to content

Commit

Permalink
Merge pull request #81 from Rigellute/ramsay-support-for-async-await-5
Browse files Browse the repository at this point in the history
Add async/await
  • Loading branch information
ramsayleung authored Feb 25, 2020
2 parents 9f2d991 + 497050b commit 911552c
Show file tree
Hide file tree
Showing 75 changed files with 4,508 additions and 1,998 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
.tern-port
cmake-build-debug
*/.test_token
Cargo.lock
Cargo.lock
.DS_Store
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ log = "0.4"
percent-encoding = "1.0.1"
rand = "0.6.5"
random = "0.12.2"
reqwest ={ version = "0.10.1", features=["json","blocking","socks"]}
reqwest = { version = "0.10", features = ["json", "socks"] }
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
Expand Down
9 changes: 5 additions & 4 deletions examples/album.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
extern crate rspotify;

use rspotify::spotify::client::Spotify;
use rspotify::spotify::oauth2::SpotifyClientCredentials;
use rspotify::client::Spotify;
use rspotify::oauth2::SpotifyClientCredentials;

fn main() {
#[tokio::main]
async fn main() {
// Set client_id and client_secret in .env file or
// export CLIENT_ID="your client_id"
// export CLIENT_SECRET="secret"
Expand All @@ -18,6 +19,6 @@ fn main() {
.client_credentials_manager(client_credential)
.build();
let birdy_uri = "spotify:album:0sNOF9WDwhWunNAHPD3Baj";
let albums = spotify.album(birdy_uri);
let albums = spotify.album(birdy_uri).await;
println!("{:?}", albums);
}
9 changes: 5 additions & 4 deletions examples/album_tracks.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
extern crate rspotify;

use rspotify::spotify::client::Spotify;
use rspotify::spotify::oauth2::SpotifyClientCredentials;
use rspotify::client::Spotify;
use rspotify::oauth2::SpotifyClientCredentials;

fn main() {
#[tokio::main]
async fn main() {
// Set client_id and client_secret in .env file or
// export CLIENT_ID="your client_id"
// export CLIENT_SECRET="secret"
Expand All @@ -18,6 +19,6 @@ fn main() {
.client_credentials_manager(client_credential)
.build();
let birdy_uri = "spotify:album:6akEvsycLGftJxYudPjmqK";
let tracks = spotify.album_track(birdy_uri, Some(2), None);
let tracks = spotify.album_track(birdy_uri, Some(2), None).await;
println!("{:?}", tracks);
}
9 changes: 5 additions & 4 deletions examples/albums.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
extern crate rspotify;

use rspotify::spotify::client::Spotify;
use rspotify::spotify::oauth2::SpotifyClientCredentials;
use rspotify::client::Spotify;
use rspotify::oauth2::SpotifyClientCredentials;

fn main() {
#[tokio::main]
async fn main() {
// Set client_id and client_secret in .env file or
// export CLIENT_ID="your client_id"
// export CLIENT_SECRET="secret"
Expand All @@ -21,6 +22,6 @@ fn main() {
let birdy_uri2 = String::from("spotify:album:6JWc4iAiJ9FjyK0B59ABb4");
let birdy_uri3 = String::from("spotify:album:6UXCm6bOO4gFlDQZV5yL37");
let track_uris = vec![birdy_uri1, birdy_uri2, birdy_uri3];
let albums = spotify.albums(track_uris);
let albums = spotify.albums(track_uris).await;
println!("{:?}", albums);
}
40 changes: 15 additions & 25 deletions examples/artist.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
extern crate rspotify;

use futures;
use rspotify::client::Spotify;
use rspotify::oauth2::SpotifyClientCredentials;
use tokio;

#[tokio::main]
async fn main() {
let mut handlers = vec![];
for _ in 0..20 {
let handler = tokio::spawn(async move {
// Set client_id and client_secret in .env file or
// export CLIENT_ID="your client_id"
// export CLIENT_SECRET="secret"
let client_credential = SpotifyClientCredentials::default().build();
// Set client_id and client_secret in .env file or
// export CLIENT_ID="your client_id"
// export CLIENT_SECRET="secret"
let client_credential = SpotifyClientCredentials::default().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 birdy_uri = "spotify:artist:2WX2uTcsvV5OnS0inACecP";
let artist = spotify.artist(birdy_uri).await;
println!("{:?}", artist);
return;
});
handlers.push(handler);
}
futures::future::join_all(handlers).await;
// 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 birdy_uri = "spotify:artist:2WX2uTcsvV5OnS0inACecP";
let artist = spotify.artist(birdy_uri).await;
println!("{:?}", artist);
}
9 changes: 5 additions & 4 deletions examples/artist_related_artists.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
extern crate rspotify;

use rspotify::spotify::client::Spotify;
use rspotify::spotify::oauth2::SpotifyClientCredentials;
use rspotify::client::Spotify;
use rspotify::oauth2::SpotifyClientCredentials;

fn main() {
#[tokio::main]
async fn main() {
// Set client_id and client_secret in .env file or
// export CLIENT_ID="your client_id"
// export CLIENT_SECRET="secret"
Expand All @@ -18,6 +19,6 @@ fn main() {
.client_credentials_manager(client_credential)
.build();
let birdy_uri = "spotify:artist:43ZHCT0cAZBISjO8DG9PnE";
let artist = spotify.artist_related_artists(birdy_uri);
let artist = spotify.artist_related_artists(birdy_uri).await;
println!("{:?}", artist);
}
33 changes: 33 additions & 0 deletions examples/artist_top_tracks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
extern crate rspotify;

use rspotify::client::Spotify;
use rspotify::oauth2::SpotifyClientCredentials;
use rspotify::senum::Country;

#[tokio::main]
async fn main() {
// Set client_id and client_secret in .env file or
// export CLIENT_ID="your client_id"
// export CLIENT_SECRET="secret"
let client_credential = SpotifyClientCredentials::default().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();
println!(
"{:?}:{:?}:{:?}",
&client_credential.client_id,
&client_credential.client_secret,
&client_credential.get_access_token().await
);
let spotify = Spotify::default()
.client_credentials_manager(client_credential)
.build();
let birdy_uri = "spotify:artist:2WX2uTcsvV5OnS0inACecP";
let tracks = spotify
.artist_top_tracks(birdy_uri, Country::UnitedStates)
.await;
println!("{:?}", tracks.unwrap());
}
9 changes: 5 additions & 4 deletions examples/artists.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
extern crate rspotify;

use rspotify::spotify::client::Spotify;
use rspotify::spotify::oauth2::SpotifyClientCredentials;
use rspotify::client::Spotify;
use rspotify::oauth2::SpotifyClientCredentials;

fn main() {
#[tokio::main]
async fn main() {
// Set client_id and client_secret in .env file or
// export CLIENT_ID="your client_id"
// export CLIENT_SECRET="secret"
Expand All @@ -20,6 +21,6 @@ fn main() {
let birdy_uri1 = String::from("spotify:artist:0oSGxfWSnnOXhD2fKuz2Gy");
let birdy_uri2 = String::from("spotify:artist:3dBVyJ7JuOMt4GE9607Qin");
let artist_uris = vec![birdy_uri1, birdy_uri2];
let artists = spotify.artists(artist_uris);
let artists = spotify.artists(artist_uris).await;
println!("{:?}", artists);
}
25 changes: 14 additions & 11 deletions examples/artists_albums.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
extern crate rspotify;

use rspotify::spotify::client::Spotify;
use rspotify::spotify::oauth2::SpotifyClientCredentials;
use rspotify::spotify::senum::{AlbumType, Country};
use rspotify::client::Spotify;
use rspotify::oauth2::SpotifyClientCredentials;
use rspotify::senum::{AlbumType, Country};

fn main() {
#[tokio::main]
async fn main() {
// Set client_id and client_secret in .env file or
// export CLIENT_ID="your client_id"
// export CLIENT_SECRET="secret"
Expand All @@ -19,12 +20,14 @@ fn main() {
.client_credentials_manager(client_credential)
.build();
let birdy_uri = "spotify:artist:2WX2uTcsvV5OnS0inACecP";
let albums = spotify.artist_albums(
birdy_uri,
Some(AlbumType::Album),
Some(Country::UnitedStates),
Some(10),
None,
);
let albums = spotify
.artist_albums(
birdy_uri,
Some(AlbumType::Album),
Some(Country::UnitedStates),
Some(10),
None,
)
.await;
println!("{:?}", albums);
}
9 changes: 5 additions & 4 deletions examples/audio_analysis.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
extern crate rspotify;

use rspotify::spotify::client::Spotify;
use rspotify::spotify::oauth2::SpotifyClientCredentials;
use rspotify::client::Spotify;
use rspotify::oauth2::SpotifyClientCredentials;

fn main() {
#[tokio::main]
async fn main() {
// Set client_id and client_secret in .env file or
// export CLIENT_ID="your client_id"
// export CLIENT_SECRET="secret"
Expand All @@ -18,6 +19,6 @@ fn main() {
.client_credentials_manager(client_credential)
.build();
let track = "06AKEBrKUckW0KREUWRnvT";
let analysis = spotify.audio_analysis(track);
let analysis = spotify.audio_analysis(track).await;
println!("{:?}", analysis);
}
9 changes: 5 additions & 4 deletions examples/audio_features.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
extern crate rspotify;

use rspotify::spotify::client::Spotify;
use rspotify::spotify::oauth2::SpotifyClientCredentials;
use rspotify::client::Spotify;
use rspotify::oauth2::SpotifyClientCredentials;

fn main() {
#[tokio::main]
async fn main() {
// Set client_id and client_secret in .env file or
// export CLIENT_ID="your client_id"
// export CLIENT_SECRET="secret"
Expand All @@ -18,6 +19,6 @@ fn main() {
.client_credentials_manager(client_credential)
.build();
let track = "spotify:track:06AKEBrKUckW0KREUWRnvT";
let features = spotify.audio_features(track);
let features = spotify.audio_features(track).await;
println!("{:?}", features);
}
9 changes: 5 additions & 4 deletions examples/audios_features.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
extern crate rspotify;

use rspotify::spotify::client::Spotify;
use rspotify::spotify::oauth2::SpotifyClientCredentials;
use rspotify::client::Spotify;
use rspotify::oauth2::SpotifyClientCredentials;

fn main() {
#[tokio::main]
async fn main() {
// Set client_id and client_secret in .env file or
// export CLIENT_ID="your client_id"
// export CLIENT_SECRET="secret"
Expand All @@ -22,6 +23,6 @@ fn main() {
tracks_ids.push(track_id1);
let track_id2 = String::from("spotify:track:24JygzOLM0EmRQeGtFcIcG");
tracks_ids.push(track_id2);
let features = spotify.audios_features(&tracks_ids);
let features = spotify.audios_features(&tracks_ids).await;
println!("{:?}", features);
}
45 changes: 45 additions & 0 deletions examples/categories.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
extern crate chrono;
extern crate rspotify;

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

#[tokio::main]
async 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 oauth = SpotifyOAuth::default().scope("user-follow-read").build();
match get_token(&mut oauth).await {
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 categories = spotify
.categories(None, Some(Country::UnitedStates), 10, 0)
.await;
println!("{:?}", categories);
}
None => println!("auth failed"),
};
}
Loading

0 comments on commit 911552c

Please sign in to comment.