diff --git a/src/main.rs b/src/main.rs index 83792e7..a70a0c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,6 @@ async fn main() -> Result<(), Box> { std::process::exit(-1); } }; - tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await; // let mut service_login = auth::get_token().await; loop { @@ -37,10 +36,15 @@ async fn main() -> Result<(), Box> { 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; + tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await; println!("Token refreshed"); println!("Refreshed token: {:?}", app.token); + } else { + println!("Token did not expire"); } + 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; @@ -104,19 +108,7 @@ async fn main() -> Result<(), Box> { mod auth { - // TODO: Implement function pub async fn get_token(app: &crate::config::App) -> Result { - /* - * - ) -> Result { - let client = reqwest::Client::new(); - let endpoint = String::from("api/v2/song/metadata/queue"); - let api_url = format!("{}/{endpoint}", app.uri); - client - .get(api_url) - .query(&[("song_queue_id", song_queue_id)]) - .send() - */ let client = reqwest::Client::new(); let endpoint = String::from("api/v2/service/login"); let api_url = format!("{}/{endpoint}", app.uri); @@ -125,8 +117,6 @@ mod auth { "passphrase": icarus_envy::environment::get_service_passphrase().await, }); - println!("Payload: {payload:?}"); - match client.post(api_url).json(&payload) .send().await { Ok(response) => match response.json::().await { @@ -145,14 +135,15 @@ mod auth { Err(std::io::Error::other(err.to_string())) } } - - // icarus_models::login_result::LoginResult::default() } // TODO: Implement function. 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 { - true + 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