tsk-41: Starting work to add auth to the requests
Some checks failed
Rust Build / Check (pull_request) Failing after 28s
Rust Build / Rustfmt (pull_request) Failing after 32s
Rust Build / Test Suite (pull_request) Failing after 37s
Rust Build / Clippy (pull_request) Failing after 35s
Rust Build / build (pull_request) Failing after 39s
Some checks failed
Rust Build / Check (pull_request) Failing after 28s
Rust Build / Rustfmt (pull_request) Failing after 32s
Rust Build / Test Suite (pull_request) Failing after 37s
Rust Build / Clippy (pull_request) Failing after 35s
Rust Build / build (pull_request) Failing after 39s
This commit is contained in:
49
src/main.rs
49
src/main.rs
@@ -33,11 +33,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
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<dyn std::error::Error>> {
|
||||
}
|
||||
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<icarus_models::login_result::LoginResult, std::io::Error> {
|
||||
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::<crate::api::refresh_token::response::Response>().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()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user