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
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:
13
src/api.rs
13
src/api.rs
@@ -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 client = reqwest::Client::new();
|
||||||
let fetch_endpoint = String::from("api/v2/song/queue/next");
|
let fetch_endpoint = String::from("api/v2/song/queue/next");
|
||||||
let api_url = format!("{}/{fetch_endpoint}", app.uri);
|
let api_url = format!("{}/{fetch_endpoint}", app.uri);
|
||||||
let (key, header) = auth_header(app).await;
|
let (key, header) = auth_header(app).await;
|
||||||
|
|
||||||
client.get(api_url).header(key, header)
|
client.get(api_url).header(key, header).send().await
|
||||||
.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 bearer = format!("Bearer {}", app.token.token);
|
||||||
let header_value = reqwest::header::HeaderValue::from_str(&bearer).unwrap();
|
let header_value = reqwest::header::HeaderValue::from_str(&bearer).unwrap();
|
||||||
(reqwest::header::AUTHORIZATION, header_value)
|
(reqwest::header::AUTHORIZATION, header_value)
|
||||||
|
@@ -2,5 +2,5 @@
|
|||||||
pub struct App {
|
pub struct App {
|
||||||
pub uri: String,
|
pub uri: String,
|
||||||
pub auth_uri: String,
|
pub auth_uri: String,
|
||||||
pub token: icarus_models::login_result::LoginResult
|
pub token: icarus_models::login_result::LoginResult,
|
||||||
}
|
}
|
||||||
|
165
src/main.rs
165
src/main.rs
@@ -9,14 +9,11 @@ use std::io::Write;
|
|||||||
|
|
||||||
pub const SECONDS_TO_SLEEP: u64 = 5;
|
pub const SECONDS_TO_SLEEP: u64 = 5;
|
||||||
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
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 {
|
let mut app = config::App {
|
||||||
uri: icarus_envy::environment::get_icarus_base_api_url().await,
|
uri: icarus_envy::environment::get_icarus_base_api_url().await,
|
||||||
auth_uri: icarus_envy::environment::get_icarus_auth_base_api_url().await,
|
auth_uri: icarus_envy::environment::get_icarus_auth_base_api_url().await,
|
||||||
// token: auth::get_token(&app).await,
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
match auth::get_token(&app).await {
|
match auth::get_token(&app).await {
|
||||||
@@ -28,32 +25,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
std::process::exit(-1);
|
std::process::exit(-1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// let mut service_login = auth::get_token().await;
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
println!("Base URL: {:?}", app.uri);
|
println!("Base URL: {:?}", app.uri);
|
||||||
|
println!("Auth URL: {:?}", app.auth_uri);
|
||||||
println!("Token: {:?}", app.token);
|
println!("Token: {:?}", app.token);
|
||||||
|
|
||||||
if auth::did_token_expire(&app.token).await {
|
if auth::did_token_expire(&app.token).await {
|
||||||
println!("Token did expire");
|
println!("Token expired");
|
||||||
app.token = match auth::get_refresh_token(&app, &app.token).await {
|
app.token = match auth::get_refresh_token(&app).await {
|
||||||
Ok(login_result) => {
|
Ok(login_result) => login_result,
|
||||||
login_result
|
|
||||||
}
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
|
|
||||||
println!("Token refreshed");
|
println!("Token refreshed");
|
||||||
println!("Refreshed token: {:?}", app.token);
|
println!("Refreshed token: {:?}", app.token);
|
||||||
} else {
|
} else {
|
||||||
println!("Token did not expire");
|
println!("Token did not expire");
|
||||||
}
|
}
|
||||||
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
|
|
||||||
|
|
||||||
|
|
||||||
match is_queue_empty(&app).await {
|
match is_queue_empty(&app).await {
|
||||||
Ok((empty, song_queue_item)) => {
|
Ok((empty, song_queue_item)) => {
|
||||||
@@ -72,12 +64,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
(song_queue_id, song_queue_path),
|
(song_queue_id, song_queue_path),
|
||||||
(coverart_queue_id, coverart_queue_path),
|
(coverart_queue_id, coverart_queue_path),
|
||||||
)) => {
|
)) => {
|
||||||
match wipe_data_from_queues(
|
match wipe_data_from_queues(&app, &song_queue_id, &coverart_queue_id)
|
||||||
&app,
|
.await
|
||||||
&song_queue_id,
|
|
||||||
&coverart_queue_id,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
{
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
match cleanup(&song_queue_path, &coverart_queue_path).await {
|
match cleanup(&song_queue_path, &coverart_queue_path).await {
|
||||||
@@ -112,56 +100,23 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
mod auth {
|
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 client = reqwest::Client::new();
|
||||||
let endpoint = String::from("api/v2/service/login");
|
let endpoint = String::from("api/v2/service/login");
|
||||||
let api_url = format!("{}/{endpoint}", app.auth_uri);
|
let api_url = format!("{}/{endpoint}", app.auth_uri);
|
||||||
|
|
||||||
let payload = serde_json::json!({
|
let payload = serde_json::json!({
|
||||||
"passphrase": icarus_envy::environment::get_service_passphrase().await,
|
"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 {
|
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) => {
|
Ok(resp) => {
|
||||||
if resp.data.is_empty() {
|
if resp.data.is_empty() {
|
||||||
Err(std::io::Error::other(String::from("No token returned")))
|
Err(std::io::Error::other(String::from("No token returned")))
|
||||||
@@ -169,13 +124,46 @@ mod auth {
|
|||||||
Ok(resp.data[0].clone())
|
Ok(resp.data[0].clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
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>()
|
.json::<the_rest::wipe_data::song_queue::response::Response>()
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(_resp) => match the_rest::wipe_data::coverart_queue::wipe_data(
|
Ok(_resp) => {
|
||||||
// app_base_url,
|
match the_rest::wipe_data::coverart_queue::wipe_data(app, coverart_queue_id).await {
|
||||||
app,
|
Ok(inner_response) => match inner_response
|
||||||
coverart_queue_id,
|
.json::<the_rest::wipe_data::coverart_queue::response::Response>()
|
||||||
)
|
.await
|
||||||
.await
|
{
|
||||||
{
|
Ok(_inner_resp) => {
|
||||||
Ok(inner_response) => match inner_response
|
println!("Wiped data from CoverArt queue");
|
||||||
.json::<the_rest::wipe_data::coverart_queue::response::Response>()
|
println!("Resp: {_inner_resp:?}");
|
||||||
.await
|
Ok(())
|
||||||
{
|
}
|
||||||
Ok(_inner_resp) => {
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
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())),
|
||||||
},
|
},
|
||||||
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(
|
async fn is_queue_empty(
|
||||||
// api_url: &String,
|
|
||||||
app: &config::App,
|
app: &config::App,
|
||||||
) -> Result<(bool, responses::fetch_next_queue_item::SongQueueItem), reqwest::Error> {
|
) -> Result<(bool, responses::fetch_next_queue_item::SongQueueItem), reqwest::Error> {
|
||||||
match api::fetch_next_queue_item(app).await {
|
match api::fetch_next_queue_item(app).await {
|
||||||
@@ -258,7 +241,6 @@ async fn is_queue_empty(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn some_work(
|
async fn some_work(
|
||||||
// app_base_url: &String,
|
|
||||||
app: &crate::config::App,
|
app: &crate::config::App,
|
||||||
song_queue_id: &uuid::Uuid,
|
song_queue_id: &uuid::Uuid,
|
||||||
user_id: &uuid::Uuid,
|
user_id: &uuid::Uuid,
|
||||||
@@ -294,10 +276,7 @@ async fn some_work(
|
|||||||
let song_type = String::from("flac");
|
let song_type = String::from("flac");
|
||||||
|
|
||||||
match the_rest::create_song::create(
|
match the_rest::create_song::create(
|
||||||
app,
|
app, &metadata, user_id, &song_type,
|
||||||
&metadata,
|
|
||||||
user_id,
|
|
||||||
&song_type,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
@@ -345,7 +324,6 @@ async fn some_work(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn prep_song(
|
async fn prep_song(
|
||||||
// api_url: &String,
|
|
||||||
app: &crate::config::App,
|
app: &crate::config::App,
|
||||||
song_queue_id: &uuid::Uuid,
|
song_queue_id: &uuid::Uuid,
|
||||||
) -> Result<
|
) -> Result<
|
||||||
@@ -382,8 +360,7 @@ async fn prep_song(
|
|||||||
println!("Created at: {created_at:?}");
|
println!("Created at: {created_at:?}");
|
||||||
|
|
||||||
println!("Getting coverart queue");
|
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) => {
|
Ok(response) => {
|
||||||
match response.json::<api::get_coverart_queue::response::Response>().await {
|
match response.json::<api::get_coverart_queue::response::Response>().await {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
|
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
pub mod create_song {
|
pub mod create_song {
|
||||||
pub async fn create(
|
pub async fn create(
|
||||||
// base_url: &String,
|
|
||||||
app: &crate::config::App,
|
app: &crate::config::App,
|
||||||
metadata_queue: &crate::api::get_metadata_queue::response::Metadata,
|
metadata_queue: &crate::api::get_metadata_queue::response::Metadata,
|
||||||
user_id: &uuid::Uuid,
|
user_id: &uuid::Uuid,
|
||||||
@@ -48,7 +47,6 @@ pub mod create_song {
|
|||||||
pub mod create_coverart {
|
pub mod create_coverart {
|
||||||
|
|
||||||
pub async fn create(
|
pub async fn create(
|
||||||
// base_url: &String,
|
|
||||||
app: &crate::config::App,
|
app: &crate::config::App,
|
||||||
song_id: &uuid::Uuid,
|
song_id: &uuid::Uuid,
|
||||||
coverart_queue_id: &uuid::Uuid,
|
coverart_queue_id: &uuid::Uuid,
|
||||||
@@ -81,7 +79,6 @@ pub mod create_coverart {
|
|||||||
pub mod wipe_data {
|
pub mod wipe_data {
|
||||||
pub mod song_queue {
|
pub mod song_queue {
|
||||||
pub async fn wipe_data(
|
pub async fn wipe_data(
|
||||||
// base_url: &String,
|
|
||||||
app: &crate::config::App,
|
app: &crate::config::App,
|
||||||
song_queue_id: &uuid::Uuid,
|
song_queue_id: &uuid::Uuid,
|
||||||
) -> Result<reqwest::Response, reqwest::Error> {
|
) -> Result<reqwest::Response, reqwest::Error> {
|
||||||
@@ -106,7 +103,6 @@ pub mod wipe_data {
|
|||||||
}
|
}
|
||||||
pub mod coverart_queue {
|
pub mod coverart_queue {
|
||||||
pub async fn wipe_data(
|
pub async fn wipe_data(
|
||||||
// base_url: &String,
|
|
||||||
app: &crate::config::App,
|
app: &crate::config::App,
|
||||||
coverart_queue_id: &uuid::Uuid,
|
coverart_queue_id: &uuid::Uuid,
|
||||||
) -> Result<reqwest::Response, reqwest::Error> {
|
) -> Result<reqwest::Response, reqwest::Error> {
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
pub async fn update_queued_song(
|
pub async fn update_queued_song(
|
||||||
// base_url: &String,
|
|
||||||
app: &crate::config::App,
|
app: &crate::config::App,
|
||||||
song_path: &String,
|
song_path: &String,
|
||||||
song_queue_id: &uuid::Uuid,
|
song_queue_id: &uuid::Uuid,
|
||||||
|
Reference in New Issue
Block a user