diff options
author | HampusM <hampus@hampusmat.com> | 2022-09-08 21:08:48 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-09-08 21:08:48 +0200 |
commit | 8a02d3386d4ce0b58de943fcf42bd072af1e0b42 (patch) | |
tree | 6656b72ce309ae993029d8cdae26604098d92ba7 /src/auth | |
parent | b44463d533ba9b789e3423d670e2ddcc32c1112c (diff) |
refactor: move get access token to DeezerClient
Diffstat (limited to 'src/auth')
-rw-r--r-- | src/auth/mod.rs | 44 |
1 files changed, 2 insertions, 42 deletions
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<AccessToken, AccessTokenRequestError> -{ - let response = reqwest::get(format!( - "{}?app_id={}&secret={}&code={}&output=json", - ACCESS_TOKEN_URL, app_id, secret_key, auth_code - )) - .await? - .json::<AccessTokenResponse>() - .await?; - - Ok(response.into()) -} - -#[derive(Debug, Deserialize)] -struct AccessTokenResponse -{ - pub access_token: String, - pub expires: u64, -} - -impl From<AccessTokenResponse> for AccessToken -{ - fn from(response: AccessTokenResponse) -> Self - { - Self { - access_token: response.access_token, - expires: Duration::from_secs(response.expires), - } - } -} |