tsk-41: Refactored code to prepare for auth implementation
This commit is contained in:
79
src/main.rs
79
src/main.rs
@@ -1,4 +1,5 @@
|
||||
pub mod api;
|
||||
pub mod config;
|
||||
pub mod responses;
|
||||
pub mod the_rest;
|
||||
pub mod update_queued_song;
|
||||
@@ -8,23 +9,42 @@ 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 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,
|
||||
token: auth::get_token().await,
|
||||
// ..Default::default()
|
||||
};
|
||||
// let mut service_login = auth::get_token().await;
|
||||
|
||||
loop {
|
||||
println!("Base URL: {app_base_url}");
|
||||
println!("Base URL: {:?}", app.uri);
|
||||
|
||||
match is_queue_empty(&app_base_url).await {
|
||||
println!("Token: {:?}", app.token);
|
||||
|
||||
if auth::did_token_expire(&app.token).await {
|
||||
app.token = auth::get_refresh_token(&app.token).await;
|
||||
println!("Token refreshed");
|
||||
println!("Refreshed token: {:?}", app.token);
|
||||
}
|
||||
|
||||
// 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:?}");
|
||||
|
||||
let song_queue_id = song_queue_item.data[0].id;
|
||||
let user_id = song_queue_item.data[0].user_id;
|
||||
|
||||
// TODO: Do something with the result later
|
||||
match some_work(&app_base_url, &song_queue_id, &user_id).await {
|
||||
match some_work(&app, &song_queue_id, &user_id).await {
|
||||
Ok((
|
||||
_song,
|
||||
_coverart,
|
||||
@@ -32,7 +52,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
(coverart_queue_id, coverart_queue_path),
|
||||
)) => {
|
||||
match wipe_data_from_queues(
|
||||
&app_base_url,
|
||||
&app,
|
||||
&song_queue_id,
|
||||
&coverart_queue_id,
|
||||
)
|
||||
@@ -71,18 +91,34 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
mod auth {
|
||||
pub async fn get_token() -> icarus_models::login_result::LoginResult {
|
||||
icarus_models::login_result::LoginResult::default()
|
||||
}
|
||||
|
||||
pub async fn did_token_expire(login_result: &icarus_models::login_result::LoginResult) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
pub async fn get_refresh_token(login_result: &icarus_models::login_result::LoginResult) -> icarus_models::login_result::LoginResult {
|
||||
icarus_models::login_result::LoginResult::default()
|
||||
}
|
||||
}
|
||||
|
||||
async fn wipe_data_from_queues(
|
||||
app_base_url: &String,
|
||||
app: &config::App,
|
||||
song_queue_id: &uuid::Uuid,
|
||||
coverart_queue_id: &uuid::Uuid,
|
||||
) -> Result<(), std::io::Error> {
|
||||
match the_rest::wipe_data::song_queue::wipe_data(app_base_url, song_queue_id).await {
|
||||
match the_rest::wipe_data::song_queue::wipe_data(app, song_queue_id).await {
|
||||
Ok(response) => match response
|
||||
.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_base_url,
|
||||
app,
|
||||
coverart_queue_id,
|
||||
)
|
||||
.await
|
||||
@@ -124,9 +160,10 @@ async fn cleanup(
|
||||
}
|
||||
|
||||
async fn is_queue_empty(
|
||||
api_url: &String,
|
||||
// api_url: &String,
|
||||
app: &config::App,
|
||||
) -> Result<(bool, responses::fetch_next_queue_item::SongQueueItem), reqwest::Error> {
|
||||
match api::fetch_next_queue_item(api_url).await {
|
||||
match api::fetch_next_queue_item(app).await {
|
||||
Ok(response) => {
|
||||
match response
|
||||
.json::<responses::fetch_next_queue_item::SongQueueItem>()
|
||||
@@ -147,7 +184,8 @@ async fn is_queue_empty(
|
||||
}
|
||||
|
||||
async fn some_work(
|
||||
app_base_url: &String,
|
||||
// app_base_url: &String,
|
||||
app: &crate::config::App,
|
||||
song_queue_id: &uuid::Uuid,
|
||||
user_id: &uuid::Uuid,
|
||||
) -> Result<
|
||||
@@ -159,12 +197,12 @@ async fn some_work(
|
||||
),
|
||||
std::io::Error,
|
||||
> {
|
||||
match prep_song(app_base_url, song_queue_id).await {
|
||||
match prep_song(app, song_queue_id).await {
|
||||
Ok((song_queue_path, coverart_queue_path, metadata, coverart_queue_id)) => {
|
||||
match apply_metadata(&song_queue_path, &coverart_queue_path, &metadata).await {
|
||||
Ok(_applied) => {
|
||||
match update_queued_song::update_queued_song(
|
||||
app_base_url,
|
||||
app,
|
||||
&song_queue_path,
|
||||
song_queue_id,
|
||||
)
|
||||
@@ -182,7 +220,7 @@ async fn some_work(
|
||||
let song_type = String::from("flac");
|
||||
|
||||
match the_rest::create_song::create(
|
||||
app_base_url,
|
||||
app,
|
||||
&metadata,
|
||||
user_id,
|
||||
&song_type,
|
||||
@@ -197,7 +235,7 @@ async fn some_work(
|
||||
println!("Response: {resp:?}");
|
||||
|
||||
let song = &resp.data[0];
|
||||
match the_rest::create_coverart::create(app_base_url, &song.id, &coverart_queue_id).await {
|
||||
match the_rest::create_coverart::create(app, &song.id, &coverart_queue_id).await {
|
||||
Ok(response) => match response.json::<the_rest::create_coverart::response::Response>().await {
|
||||
Ok(resp) => {
|
||||
println!("CoverArt sent and successfully parsed response");
|
||||
@@ -233,7 +271,8 @@ async fn some_work(
|
||||
}
|
||||
|
||||
async fn prep_song(
|
||||
api_url: &String,
|
||||
// api_url: &String,
|
||||
app: &crate::config::App,
|
||||
song_queue_id: &uuid::Uuid,
|
||||
) -> Result<
|
||||
(
|
||||
@@ -244,7 +283,7 @@ async fn prep_song(
|
||||
),
|
||||
reqwest::Error,
|
||||
> {
|
||||
match api::fetch_song_queue_data::get_data(api_url, song_queue_id).await {
|
||||
match api::fetch_song_queue_data::get_data(app, song_queue_id).await {
|
||||
Ok(response) => {
|
||||
// Process data here...
|
||||
match api::parsing::parse_response_into_bytes(response).await {
|
||||
@@ -254,7 +293,7 @@ async fn prep_song(
|
||||
|
||||
println!("Saved at: {song_queue_path:?}");
|
||||
|
||||
match api::get_metadata_queue::get(api_url, song_queue_id).await {
|
||||
match api::get_metadata_queue::get(app, song_queue_id).await {
|
||||
Ok(response) => {
|
||||
match response
|
||||
.json::<api::get_metadata_queue::response::Response>()
|
||||
@@ -269,7 +308,7 @@ async fn prep_song(
|
||||
println!("Created at: {created_at:?}");
|
||||
|
||||
println!("Getting coverart queue");
|
||||
match api::get_coverart_queue::get(api_url, 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 {
|
||||
@@ -277,7 +316,7 @@ async fn prep_song(
|
||||
let coverart_queue_id = &response.data[0].id;
|
||||
println!("Coverart queue Id: {coverart_queue_id:?}");
|
||||
|
||||
match api::get_coverart_queue::get_data(api_url, coverart_queue_id).await {
|
||||
match api::get_coverart_queue::get_data(app, coverart_queue_id).await {
|
||||
Ok(response) => match api::parsing::parse_response_into_bytes(response).await {
|
||||
Ok(coverart_queue_bytes) => {
|
||||
let (directory, filename) = generate_coverart_queue_dir_and_filename().await;
|
||||
|
Reference in New Issue
Block a user