From b5f7943938a113f96a03b16bd8f27a6fbf65eaf2 Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 13 Aug 2025 17:55:30 -0400 Subject: [PATCH] tsk-41: Starting work to add auth to the requests --- src/api.rs | 21 ++++++++++++++++++++- src/main.rs | 49 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/api.rs b/src/api.rs index 9a380ec..51e888d 100644 --- a/src/api.rs +++ b/src/api.rs @@ -2,7 +2,16 @@ pub async fn fetch_next_queue_item(app: &crate::config::App) -> Result (reqwest::header::HeaderName, reqwest::header::HeaderValue) { + let bearer = format!("Bearer {}", app.token.token); + let header_value = reqwest::header::HeaderValue::from_str(&bearer).unwrap(); + (reqwest::header::AUTHORIZATION, header_value) } pub mod parsing { @@ -143,3 +152,13 @@ pub mod service_token { } } } + +pub mod refresh_token { + pub mod response { + #[derive(Debug, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } +} diff --git a/src/main.rs b/src/main.rs index a70a0c0..5644458 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,11 +33,18 @@ async fn main() -> Result<(), Box> { println!("Base URL: {:?}", app.uri); println!("Token: {:?}", app.token); - tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await; if auth::did_token_expire(&app.token).await { println!("Token did expire"); - app.token = auth::get_refresh_token(&app, &app.token).await; + app.token = match auth::get_refresh_token(&app, &app.token).await { + Ok(login_result) => { + login_result + } + Err(err) => { + eprintln!("Error: {err:?}"); + continue; + } + }; tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await; println!("Token refreshed"); println!("Refreshed token: {:?}", app.token); @@ -46,14 +53,14 @@ async fn main() -> Result<(), Box> { } tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await; - // TODO: For now, focus on making sure the token is valid before you get here - continue; match is_queue_empty(&app).await { Ok((empty, song_queue_item)) => { if !empty { println!("Queue is not empty"); println!("SongQueueItem: {song_queue_item:?}"); + // TODO: For now, focus on making sure the token is valid before you get here + continue; let song_queue_id = song_queue_item.data[0].id; let user_id = song_queue_item.data[0].user_id; @@ -137,18 +144,40 @@ mod auth { } } - // TODO: Implement function. Might want to put the functionality within icarus_models - // at some point + // TODO: Might want to put the functionality within icarus_models at some point pub async fn did_token_expire(login_result: &icarus_models::login_result::LoginResult) -> bool { let current_time = time::OffsetDateTime::now_utc(); let expire_time = time::OffsetDateTime::from_unix_timestamp(login_result.expiration).unwrap(); current_time > expire_time - // true } - // TODO: Implement function - pub async fn get_refresh_token(app: &crate::config::App, login_result: &icarus_models::login_result::LoginResult) -> icarus_models::login_result::LoginResult { - icarus_models::login_result::LoginResult::default() + // TODO: Refactor this to only have one function parameter + pub async fn get_refresh_token(app: &crate::config::App, login_result: &icarus_models::login_result::LoginResult) -> Result { + let client = reqwest::Client::new(); + let endpoint = String::from("api/v2/token/refresh"); + let api_url = format!("{}/{endpoint}", app.uri); + + let payload = serde_json::json!({ + "access_token": login_result.token + }); + + match client.post(api_url).json(&payload).send().await { + Ok(response) => match response.json::().await { + Ok(resp) => { + if resp.data.is_empty() { + Err(std::io::Error::other(String::from("No token returned"))) + } else { + Ok(resp.data[0].clone()) + } + } + Err(err) => { + Err(std::io::Error::other(err.to_string())) + } + } + Err(err) => { + Err(std::io::Error::other(err.to_string())) + } + } } }