From b44463d533ba9b789e3423d670e2ddcc32c1112c Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 6 Sep 2022 21:40:01 +0200 Subject: feat: add getting user & playlists --- examples/access_token/main.rs | 52 ----------------------------------- examples/playlists/main.rs | 63 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 52 deletions(-) delete mode 100644 examples/access_token/main.rs create mode 100644 examples/playlists/main.rs (limited to 'examples') diff --git a/examples/access_token/main.rs b/examples/access_token/main.rs deleted file mode 100644 index d9b4d79..0000000 --- a/examples/access_token/main.rs +++ /dev/null @@ -1,52 +0,0 @@ -use std::error::Error; - -use anyhow::Context; -use config::Config; -use deez::auth::{request_access_token, AuthPromptHandler}; -use serde::Deserialize; - -#[derive(Deserialize)] -struct Settings -{ - app_id: u32, - secret_key: String, - address: String, - port: u16, - uri_scheme: String, -} - -#[tokio::main] -async fn main() -> Result<(), Box> -{ - let settings = Config::builder() - .add_source(config::File::with_name("examples/access_token/Settings")) - .build() - .with_context(|| "Failed to read settings")? - .try_deserialize::() - .with_context(|| "Invalid settings")?; - - let auth_code_prompt_info = AuthPromptHandler::run( - settings.app_id, - settings.address, - settings.port, - settings.uri_scheme, - ) - .await?; - - println!("{}", auth_code_prompt_info.auth_prompt_url); - - let auth_code = auth_code_prompt_info.handler.await??; - - println!("Retrieved authentication code '{}'", auth_code); - - let access_token = - request_access_token(settings.app_id, settings.secret_key, auth_code).await?; - - println!( - "Retrieved access token '{}' which expires in {} seconds", - access_token.access_token, - access_token.expires.as_secs() - ); - - Ok(()) -} diff --git a/examples/playlists/main.rs b/examples/playlists/main.rs new file mode 100644 index 0000000..fed5b73 --- /dev/null +++ b/examples/playlists/main.rs @@ -0,0 +1,63 @@ +use std::error::Error; + +use anyhow::Context; +use config::Config; +use deez::auth::{request_access_token, AuthPromptHandler}; +use deez::client::DeezerClient; +use serde::Deserialize; + +#[derive(Deserialize)] +struct Settings +{ + app_id: u32, + secret_key: String, + address: String, + port: u16, + uri_scheme: String, +} + +#[tokio::main] +async fn main() -> Result<(), Box> +{ + let settings = Config::builder() + .add_source(config::File::with_name("examples/playlists/Settings")) + .build() + .with_context(|| "Failed to read settings")? + .try_deserialize::() + .with_context(|| "Invalid settings")?; + + let auth_code_prompt_info = AuthPromptHandler::run( + settings.app_id, + settings.address, + settings.port, + settings.uri_scheme, + ) + .await?; + + println!("{}", auth_code_prompt_info.auth_prompt_url); + + let auth_code = auth_code_prompt_info.handler.await??; + + println!("Retrieved authentication code '{}'", auth_code); + + let access_token = + request_access_token(settings.app_id, settings.secret_key, auth_code).await?; + + println!( + "Retrieved access token '{}' which expires in {} seconds", + access_token.access_token, + access_token.expires.as_secs() + ); + + let client = DeezerClient::new(access_token); + + let me = client.get_me().await?; + + println!("{:#?}", me); + + let playlists = client.get_user_playlists(me.id).await?; + + println!("Playlists: {:#?}", playlists); + + Ok(()) +} -- cgit v1.2.3-18-g5258