tsk-41: Code cleanup
All checks were successful
Rust Build / Check (pull_request) Successful in 38s
Rust Build / Clippy (pull_request) Successful in 34s
Rust Build / Rustfmt (pull_request) Successful in 38s
Rust Build / Test Suite (pull_request) Successful in 38s
Rust Build / build (pull_request) Successful in 35s

This commit is contained in:
2025-08-14 19:04:34 -04:00
parent b8bbd66fd3
commit 67d2729c46
5 changed files with 80 additions and 105 deletions

View File

@@ -1,14 +1,17 @@
pub async fn fetch_next_queue_item(app: &crate::config::App) -> Result<reqwest::Response, reqwest::Error> {
pub async fn fetch_next_queue_item(
app: &crate::config::App,
) -> Result<reqwest::Response, reqwest::Error> {
let client = reqwest::Client::new();
let fetch_endpoint = String::from("api/v2/song/queue/next");
let api_url = format!("{}/{fetch_endpoint}", app.uri);
let (key, header) = auth_header(app).await;
client.get(api_url).header(key, header)
.send().await
client.get(api_url).header(key, header).send().await
}
pub async fn auth_header(app: &crate::config::App) -> (reqwest::header::HeaderName, reqwest::header::HeaderValue) {
pub async fn auth_header(
app: &crate::config::App,
) -> (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)

View File

@@ -2,5 +2,5 @@
pub struct App {
pub uri: String,
pub auth_uri: String,
pub token: icarus_models::login_result::LoginResult
pub token: icarus_models::login_result::LoginResult,
}

View File

@@ -9,14 +9,11 @@ use std::io::Write;
pub const SECONDS_TO_SLEEP: u64 = 5;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// let app_base_url = icarus_envy::environment::get_icarus_base_api_url().await;
let mut app = config::App {
uri: icarus_envy::environment::get_icarus_base_api_url().await,
auth_uri: icarus_envy::environment::get_icarus_auth_base_api_url().await,
// token: auth::get_token(&app).await,
..Default::default()
};
match auth::get_token(&app).await {
@@ -28,32 +25,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
std::process::exit(-1);
}
};
// let mut service_login = auth::get_token().await;
loop {
println!("Base URL: {:?}", app.uri);
println!("Auth URL: {:?}", app.auth_uri);
println!("Token: {:?}", app.token);
if auth::did_token_expire(&app.token).await {
println!("Token did expire");
app.token = match auth::get_refresh_token(&app, &app.token).await {
Ok(login_result) => {
login_result
}
println!("Token expired");
app.token = match auth::get_refresh_token(&app).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);
} else {
println!("Token did not expire");
}
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
match is_queue_empty(&app).await {
Ok((empty, song_queue_item)) => {
@@ -72,12 +64,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
(song_queue_id, song_queue_path),
(coverart_queue_id, coverart_queue_path),
)) => {
match wipe_data_from_queues(
&app,
&song_queue_id,
&coverart_queue_id,
)
.await
match wipe_data_from_queues(&app, &song_queue_id, &coverart_queue_id)
.await
{
Ok(_) => {
match cleanup(&song_queue_path, &coverart_queue_path).await {
@@ -112,56 +100,23 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}
mod auth {
pub async fn get_token(app: &crate::config::App) -> Result<icarus_models::login_result::LoginResult, std::io::Error> {
pub async fn get_token(
app: &crate::config::App,
) -> Result<icarus_models::login_result::LoginResult, std::io::Error> {
let client = reqwest::Client::new();
let endpoint = String::from("api/v2/service/login");
let api_url = format!("{}/{endpoint}", app.auth_uri);
let payload = serde_json::json!({
"passphrase": icarus_envy::environment::get_service_passphrase().await,
});
match client.post(api_url).json(&payload)
.send().await {
Ok(response) => match response.json::<crate::api::service_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()))
}
}
}
// 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
}
// 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.auth_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(response) => match response
.json::<crate::api::service_token::response::Response>()
.await
{
Ok(resp) => {
if resp.data.is_empty() {
Err(std::io::Error::other(String::from("No token returned")))
@@ -169,13 +124,46 @@ mod auth {
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())),
},
Err(err) => Err(std::io::Error::other(err.to_string())),
}
}
// 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
}
pub async fn get_refresh_token(
app: &crate::config::App,
) -> 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.auth_uri);
let payload = serde_json::json!({
"access_token": app.token.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())),
},
Err(err) => Err(std::io::Error::other(err.to_string())),
}
}
}
@@ -190,26 +178,22 @@ async fn wipe_data_from_queues(
.json::<the_rest::wipe_data::song_queue::response::Response>()
.await
{
Ok(_resp) => match the_rest::wipe_data::coverart_queue::wipe_data(
// app_base_url,
app,
coverart_queue_id,
)
.await
{
Ok(inner_response) => match inner_response
.json::<the_rest::wipe_data::coverart_queue::response::Response>()
.await
{
Ok(_inner_resp) => {
println!("Wiped data from CoverArt queue");
println!("Resp: {_inner_resp:?}");
Ok(())
}
Ok(_resp) => {
match the_rest::wipe_data::coverart_queue::wipe_data(app, coverart_queue_id).await {
Ok(inner_response) => match inner_response
.json::<the_rest::wipe_data::coverart_queue::response::Response>()
.await
{
Ok(_inner_resp) => {
println!("Wiped data from CoverArt queue");
println!("Resp: {_inner_resp:?}");
Ok(())
}
Err(err) => Err(std::io::Error::other(err.to_string())),
},
Err(err) => Err(std::io::Error::other(err.to_string())),
},
Err(err) => Err(std::io::Error::other(err.to_string())),
},
}
}
Err(err) => Err(std::io::Error::other(err.to_string())),
},
Err(err) => Err(std::io::Error::other(err.to_string())),
@@ -234,7 +218,6 @@ async fn cleanup(
}
async fn is_queue_empty(
// api_url: &String,
app: &config::App,
) -> Result<(bool, responses::fetch_next_queue_item::SongQueueItem), reqwest::Error> {
match api::fetch_next_queue_item(app).await {
@@ -258,7 +241,6 @@ async fn is_queue_empty(
}
async fn some_work(
// app_base_url: &String,
app: &crate::config::App,
song_queue_id: &uuid::Uuid,
user_id: &uuid::Uuid,
@@ -294,10 +276,7 @@ async fn some_work(
let song_type = String::from("flac");
match the_rest::create_song::create(
app,
&metadata,
user_id,
&song_type,
app, &metadata, user_id, &song_type,
)
.await
{
@@ -345,7 +324,6 @@ async fn some_work(
}
async fn prep_song(
// api_url: &String,
app: &crate::config::App,
song_queue_id: &uuid::Uuid,
) -> Result<
@@ -382,8 +360,7 @@ async fn prep_song(
println!("Created at: {created_at:?}");
println!("Getting coverart queue");
match api::get_coverart_queue::get(app, song_queue_id).await
{
match api::get_coverart_queue::get(app, song_queue_id).await {
Ok(response) => {
match response.json::<api::get_coverart_queue::response::Response>().await {
Ok(response) => {

View File

@@ -2,7 +2,6 @@
pub mod create_song {
pub async fn create(
// base_url: &String,
app: &crate::config::App,
metadata_queue: &crate::api::get_metadata_queue::response::Metadata,
user_id: &uuid::Uuid,
@@ -48,7 +47,6 @@ pub mod create_song {
pub mod create_coverart {
pub async fn create(
// base_url: &String,
app: &crate::config::App,
song_id: &uuid::Uuid,
coverart_queue_id: &uuid::Uuid,
@@ -81,7 +79,6 @@ pub mod create_coverart {
pub mod wipe_data {
pub mod song_queue {
pub async fn wipe_data(
// base_url: &String,
app: &crate::config::App,
song_queue_id: &uuid::Uuid,
) -> Result<reqwest::Response, reqwest::Error> {
@@ -106,7 +103,6 @@ pub mod wipe_data {
}
pub mod coverart_queue {
pub async fn wipe_data(
// base_url: &String,
app: &crate::config::App,
coverart_queue_id: &uuid::Uuid,
) -> Result<reqwest::Response, reqwest::Error> {

View File

@@ -1,5 +1,4 @@
pub async fn update_queued_song(
// base_url: &String,
app: &crate::config::App,
song_path: &String,
song_queue_id: &uuid::Uuid,