tsk-41: Can fetch the token now
This commit is contained in:
10
src/api.rs
10
src/api.rs
@@ -133,3 +133,13 @@ pub mod get_coverart_queue {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod service_token {
|
||||
pub mod response {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<icarus_models::login_result::LoginResult>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
63
src/main.rs
63
src/main.rs
@@ -15,18 +15,29 @@ 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,
|
||||
token: auth::get_token().await,
|
||||
// ..Default::default()
|
||||
// token: auth::get_token(&app).await,
|
||||
..Default::default()
|
||||
};
|
||||
match auth::get_token(&app).await {
|
||||
Ok(login_result) => {
|
||||
app.token = login_result;
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
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 {
|
||||
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 {
|
||||
app.token = auth::get_refresh_token(&app.token).await;
|
||||
app.token = auth::get_refresh_token(&app, &app.token).await;
|
||||
println!("Token refreshed");
|
||||
println!("Refreshed token: {:?}", app.token);
|
||||
}
|
||||
@@ -94,8 +105,48 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
mod auth {
|
||||
// TODO: Implement function
|
||||
pub async fn get_token() -> icarus_models::login_result::LoginResult {
|
||||
icarus_models::login_result::LoginResult::default()
|
||||
pub async fn get_token(app: &crate::config::App) -> Result<icarus_models::login_result::LoginResult, std::io::Error> {
|
||||
/*
|
||||
*
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
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);
|
||||
|
||||
let payload = serde_json::json!({
|
||||
"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::<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()))
|
||||
}
|
||||
}
|
||||
|
||||
// icarus_models::login_result::LoginResult::default()
|
||||
}
|
||||
|
||||
// TODO: Implement function. Might want to put the functionality within icarus_models
|
||||
@@ -105,7 +156,7 @@ mod auth {
|
||||
}
|
||||
|
||||
// TODO: Implement function
|
||||
pub async fn get_refresh_token(login_result: &icarus_models::login_result::LoginResult) -> icarus_models::login_result::LoginResult {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user