Skip to content

Commit

Permalink
Merge pull request #216 from ramsayleung/auth-rewrite-part4
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsayleung authored Jun 19, 2021
2 parents 4ab9300 + b526ea0 commit e1c6c44
Show file tree
Hide file tree
Showing 23 changed files with 511 additions and 879 deletions.
36 changes: 7 additions & 29 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ jobs:
- arm-unknown-linux-gnueabihf
- armv7-unknown-linux-gnueabihf
client:
- client-ureq,ureq-rustls-tls
- client-reqwest,reqwest-rustls-tls
- rspotify/cli,rspotify/env-file,rspotify/client-ureq,rspotify/ureq-rustls-tls,rspotify-http/client-ureq,rspotify-http/ureq-rustls-tls
- rspotify/cli,rspotify/env-file,rspotify/client-reqwest,rspotify/reqwest-rustls-tls,rspotify-http/client-reqwest,rspotify-http/reqwest-rustls-tls
steps:
- name: Checkout sources
uses: actions/checkout@v2
Expand All @@ -64,16 +64,16 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: build
args: --workspace --target ${{ matrix.target }} --no-default-features --features=cli,env-file,${{ matrix.client }}
args: -p rspotify -p rspotify-http -p rspotify-model -p rspotify-macros --no-default-features --features=${{ matrix.client }} --target ${{ matrix.target }}

test-client:
name: Test and Lint for each Client
runs-on: ubuntu-latest
strategy:
matrix:
client:
- client-ureq,ureq-rustls-tls
- client-reqwest,reqwest-rustls-tls
- rspotify/cli,rspotify/env-file,rspotify/client-ureq,rspotify/ureq-rustls-tls,rspotify-http/client-ureq,rspotify-http/ureq-rustls-tls
- rspotify/cli,rspotify/env-file,rspotify/client-reqwest,rspotify/reqwest-rustls-tls,rspotify-http/client-reqwest,rspotify-http/reqwest-rustls-tls
steps:
- name: Checkout sources
uses: actions/checkout@v2
Expand All @@ -90,32 +90,10 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
args: --workspace --no-default-features --features=cli,env-file,${{ matrix.client }} -- -D warnings
args: -p rspotify -p rspotify-http -p rspotify-model -p rspotify-macros --no-default-features --features=${{ matrix.client }} -- -D warnings

- name: Run cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features=env-file,${{ matrix.client }}

# The rest of the crates don't need to be tested with multiple feature
# combinations.
test-crates:
name: Simple Tests for Crates
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

- name: Run cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: -p rspotify-macros -p rspotify-model
args: -p rspotify -p rspotify-http -p rspotify-model -p rspotify-macros --no-default-features --features=${{ matrix.client }}
25 changes: 0 additions & 25 deletions .travis.yml

This file was deleted.

115 changes: 94 additions & 21 deletions CHANGELOG.md

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions examples/auth_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use rspotify::{
model::{AdditionalType, Country, Market},
prelude::*,
scopes, AuthCodeSpotify, Credentials, OAuth,
};

#[tokio::main]
async fn main() {
// You can use any logger for debugging.
env_logger::init();

// Set RSPOTIFY_CLIENT_ID, RSPOTIFY_CLIENT_SECRET and
// RSPOTIFY_REDIRECT_URI in an .env file or export them manually:
//
// export RSPOTIFY_CLIENT_ID="your client_id"
// export RSPOTIFY_CLIENT_SECRET="secret"
//
// These will then be read with `from_env`.
//
// Otherwise, set client_id and client_secret explictly:
//
// ```
// let creds = Credentials {
// id: "this-is-my-client-id".to_string(),
// secret: "this-is-my-client-secret".to_string()
// };
// ```
let creds = Credentials::from_env().unwrap();

// Or set the redirect_uri explictly:
//
// ```
// let oauth = OAuth {
// redirect_uri: "http://localhost:8888/callback".to_string(),
// scope: scopes!("user-read-recently-played"),
// ..Default::default(),
// };
// ```
let oauth = OAuth::from_env(scopes!("user-read-currently-playing")).unwrap();

let mut spotify = AuthCodeSpotify::new(creds, oauth);

// Obtaining the access token
let url = spotify.get_authorize_url(false).unwrap();
spotify.prompt_for_token(&url).await.unwrap();

// Running the requests
let market = Market::Country(Country::Spain);
let additional_types = [AdditionalType::Episode];
let artists = spotify
.current_playing(Some(&market), Some(&additional_types))
.await;

println!("Response: {:?}", artists);
}
47 changes: 47 additions & 0 deletions examples/auth_code_pkce.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use rspotify::{prelude::*, scopes, AuthCodePkceSpotify, Credentials, OAuth};

#[tokio::main]
async fn main() {
// You can use any logger for debugging.
env_logger::init();

// Set RSPOTIFY_CLIENT_ID, RSPOTIFY_CLIENT_SECRET and
// RSPOTIFY_REDIRECT_URI in an .env file or export them manually:
//
// export RSPOTIFY_CLIENT_ID="your client_id"
// export RSPOTIFY_CLIENT_SECRET="secret"
//
// These will then be read with `from_env`.
//
// Otherwise, set client_id and client_secret explictly:
//
// ```
// let creds = Credentials {
// id: "this-is-my-client-id".to_string(),
// secret: "this-is-my-client-secret".to_string()
// };
// ```
let creds = Credentials::from_env().unwrap();

// Or set the redirect_uri explictly:
//
// ```
// let oauth = OAuth {
// redirect_uri: "http://localhost:8888/callback".to_string(),
// scope: scopes!("user-read-recently-played"),
// ..Default::default(),
// };
// ```
let oauth = OAuth::from_env(scopes!("user-read-recently-played")).unwrap();

let mut spotify = AuthCodePkceSpotify::new(creds, oauth);

// Obtaining the access token
let url = spotify.get_authorize_url().unwrap();
spotify.prompt_for_token(&url).await.unwrap();

// Running the requests
let history = spotify.current_playback(None, None::<Vec<_>>).await;

println!("Response: {:?}", history);
}
24 changes: 10 additions & 14 deletions examples/album.rs → examples/client_creds.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use rspotify::client::SpotifyBuilder;
use rspotify::model::Id;
use rspotify::oauth2::CredentialsBuilder;
use rspotify::{model::Id, prelude::*, ClientCredsSpotify, Credentials};

#[tokio::main]
async fn main() {
Expand All @@ -17,22 +15,20 @@ async fn main() {
//
// Otherwise, set client_id and client_secret explictly:
//
// let creds = CredentialsBuilder::default()
// .client_id("this-is-my-client-id")
// .client_secret("this-is-my-client-secret")
// .build()
// .unwrap();
let creds = CredentialsBuilder::from_env().build().unwrap();
// ```
// let creds = Credentials {
// id: "this-is-my-client-id".to_string(),
// secret: "this-is-my-client-secret".to_string()
// };
// ```
let creds = Credentials::from_env().unwrap();

let mut spotify = SpotifyBuilder::default()
.credentials(creds)
.build()
.unwrap();
let mut spotify = ClientCredsSpotify::new(creds);

// Obtaining the access token. Requires to be mutable because the internal
// token will be modified. We don't need OAuth for this specific endpoint,
// so `...` is used instead of `prompt_for_user_token`.
spotify.request_client_token().await.unwrap();
spotify.request_token().await.unwrap();

// Running the requests
let birdy_uri = Id::from_uri("spotify:album:0sNOF9WDwhWunNAHPD3Baj").unwrap();
Expand Down
51 changes: 0 additions & 51 deletions examples/current_user_recently_played.rs

This file was deleted.

19 changes: 7 additions & 12 deletions examples/oauth_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
//! an .env file or export them manually as environmental variables for this to
//! work.
use rspotify::client::SpotifyBuilder;
use rspotify::oauth2::{CredentialsBuilder, OAuthBuilder};
use rspotify::scopes;
use rspotify::{prelude::*, scopes, AuthCodeSpotify, Credentials, OAuth};

#[tokio::main]
async fn main() {
Expand All @@ -16,10 +14,10 @@ async fn main() {

// The credentials must be available in the environment. Enable
// `env-file` in order to read them from an `.env` file.
let creds = CredentialsBuilder::from_env().build().unwrap();
let creds = Credentials::from_env().unwrap();

// Using every possible scope
let scope = scopes!(
let scopes = scopes!(
"user-read-email",
"user-read-private",
"user-top-read",
Expand All @@ -38,15 +36,12 @@ async fn main() {
"playlist-modify-private",
"ugc-image-upload"
);
let oauth = OAuthBuilder::from_env().scope(scope).build().unwrap();
let oauth = OAuth::from_env(scopes).unwrap();

let mut spotify = SpotifyBuilder::default()
.credentials(creds)
.oauth(oauth)
.build()
.unwrap();
let mut spotify = AuthCodeSpotify::new(creds, oauth);

spotify.prompt_for_user_token().await.unwrap();
let url = spotify.get_authorize_url(false).unwrap();
spotify.prompt_for_token(&url).await.unwrap();

let token = spotify.token.as_ref().unwrap();
println!("Access token: {}", &token.access_token);
Expand Down
42 changes: 6 additions & 36 deletions examples/pagination_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,21 @@
use futures::stream::TryStreamExt;
use futures_util::pin_mut;
use rspotify::client::SpotifyBuilder;
use rspotify::oauth2::{CredentialsBuilder, OAuthBuilder};
use rspotify::scopes;
use rspotify::{prelude::*, scopes, AuthCodeSpotify, Credentials, OAuth};

#[tokio::main]
async fn main() {
// You can use any logger for debugging.
env_logger::init();

// Set RSPOTIFY_CLIENT_ID, RSPOTIFY_CLIENT_SECRET and
// RSPOTIFY_REDIRECT_URI in an .env file or export them manually:
//
// export RSPOTIFY_CLIENT_ID="your client_id"
// export RSPOTIFY_CLIENT_SECRET="secret"
//
// These will then be read with `from_env`.
//
// Otherwise, set client_id and client_secret explictly:
//
// let creds = CredentialsBuilder::default()
// .client_id("this-is-my-client-id")
// .client_secret("this-is-my-client-secret")
// .build()
// .unwrap();
let creds = CredentialsBuilder::from_env().build().unwrap();
let creds = Credentials::from_env().unwrap();
let oauth = OAuth::from_env(scopes!("user-library-read")).unwrap();

// Or set the redirect_uri explictly:
//
// let oauth = OAuthBuilder::default()
// .redirect_uri("http://localhost:8888/callback")
// .build()
// .unwrap();
let oauth = OAuthBuilder::from_env()
.scope(scopes!("user-library-read"))
.build()
.unwrap();

let mut spotify = SpotifyBuilder::default()
.credentials(creds)
.oauth(oauth)
.build()
.unwrap();
let mut spotify = AuthCodeSpotify::new(creds, oauth);

// Obtaining the access token
spotify.prompt_for_user_token().await.unwrap();
let url = spotify.get_authorize_url(false).unwrap();
spotify.prompt_for_token(&url).await.unwrap();

// Executing the futures sequentially
let stream = spotify.current_user_saved_tracks();
Expand Down
Loading

0 comments on commit e1c6c44

Please sign in to comment.