From 8a02d3386d4ce0b58de943fcf42bd072af1e0b42 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 8 Sep 2022 21:08:48 +0200 Subject: refactor: move get access token to DeezerClient --- src/auth/mod.rs | 44 ++------------------------------------------ 1 file changed, 2 insertions(+), 42 deletions(-) (limited to 'src/auth') diff --git a/src/auth/mod.rs b/src/auth/mod.rs index fae0383..ce550e1 100644 --- a/src/auth/mod.rs +++ b/src/auth/mod.rs @@ -11,12 +11,11 @@ use tokio::task::JoinHandle; use tokio::{select, spawn}; use crate::auth::service::retrieve_auth_code; -use crate::errors::auth::{AccessTokenRequestError, AuthPromptHandlerError}; +use crate::errors::auth::AuthPromptHandlerError; mod service; const AUTH_URL: &str = "https://connect.deezer.com/oauth/auth.php"; -const ACCESS_TOKEN_URL: &str = "https://connect.deezer.com/oauth/access_token.php"; /// A Deezer authentication code. #[derive(Debug, Deserialize, Clone)] @@ -96,7 +95,7 @@ impl AuthPromptHandler } /// A Deezer access token. -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct AccessToken { /// The access token. @@ -105,42 +104,3 @@ pub struct AccessToken /// The duration until the access token expires. pub expires: Duration, } - -/// Sends a request for a access token. -/// -/// # Errors -/// Will return Err if either sending the request fails or parsing the response fails. -pub async fn request_access_token( - app_id: u32, - secret_key: String, - auth_code: AuthCode, -) -> Result -{ - let response = reqwest::get(format!( - "{}?app_id={}&secret={}&code={}&output=json", - ACCESS_TOKEN_URL, app_id, secret_key, auth_code - )) - .await? - .json::() - .await?; - - Ok(response.into()) -} - -#[derive(Debug, Deserialize)] -struct AccessTokenResponse -{ - pub access_token: String, - pub expires: u64, -} - -impl From for AccessToken -{ - fn from(response: AccessTokenResponse) -> Self - { - Self { - access_token: response.access_token, - expires: Duration::from_secs(response.expires), - } - } -} -- cgit v1.2.3-18-g5258