tsk-55: Utilize the expiration methods in icarus_models (#59)
All checks were successful
Rust Build / Check (push) Successful in 34s
Rust Build / Test Suite (push) Successful in 34s
Rust Build / Rustfmt (push) Successful in 30s
Rust Build / Clippy (push) Successful in 36s
Rust Build / build (push) Successful in 40s

Closes #55

Reviewed-on: #59
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
This commit was merged in pull request #59.
This commit is contained in:
2025-10-20 17:45:05 +00:00
committed by phoenix
parent 98f1c8bdf6
commit 3bb7fb1010
5 changed files with 203 additions and 212 deletions

57
src/auth/mod.rs Normal file
View File

@@ -0,0 +1,57 @@
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.value,
});
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())),
}
}
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())),
}
}