69 lines
2.6 KiB
Rust
69 lines
2.6 KiB
Rust
pub struct Queue {
|
|
pub app: std::sync::Arc<crate::app::App>,
|
|
pub token: std::sync::Arc<schedtxt_models::token::LoginResult>,
|
|
}
|
|
|
|
impl Queue {
|
|
pub async fn get_queue(
|
|
&self,
|
|
) -> Result<schedtxt_models::message::scheduling::ScheduledMessage, std::io::Error> {
|
|
let client = reqwest::Client::new();
|
|
let endpoint = String::from("api/v1/schedule/message/fetch");
|
|
let api_url = format!("{}/{endpoint}", self.app.api_url);
|
|
|
|
println!("Url: {api_url:?}");
|
|
|
|
let (key, header) = super::auth_header(&self.token.access_token);
|
|
|
|
match client.get(api_url).header(key, header).send().await {
|
|
Ok(response) => match response.status() {
|
|
reqwest::StatusCode::OK => {
|
|
println!("Response: {response:?}");
|
|
match response.text().await {
|
|
Ok(val) => match parse_queue_response(&val).await {
|
|
Ok(login_result) => Ok(login_result),
|
|
Err(err) => Err(err),
|
|
},
|
|
Err(err) => Err(std::io::Error::other(err)),
|
|
}
|
|
}
|
|
_ => Err(std::io::Error::other("Error getting token")),
|
|
},
|
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn parse_queue_response(
|
|
response: &str,
|
|
) -> Result<schedtxt_models::message::scheduling::ScheduledMessage, std::io::Error> {
|
|
match serde_json::from_str::<serde_json::Value>(response) {
|
|
Ok(j) => {
|
|
let message = &j["message"];
|
|
let data = &j["data"];
|
|
|
|
println!("Message: {message:?}");
|
|
println!("Data: {data:?}");
|
|
match j.get("data") {
|
|
Some(serde_json::Value::Array(lrs)) => {
|
|
if lrs.is_empty() {
|
|
Err(std::io::Error::other("Error response is empty"))
|
|
} else {
|
|
let lr = lrs[0].clone();
|
|
let ll: schedtxt_models::message::scheduling::ScheduledMessage =
|
|
match serde_json::from_value(lr) {
|
|
Ok(lr) => lr,
|
|
Err(err) => {
|
|
return Err(std::io::Error::other(err.to_string()));
|
|
}
|
|
};
|
|
Ok(ll)
|
|
}
|
|
}
|
|
_ => Err(std::io::Error::other("Error parsing response")),
|
|
}
|
|
}
|
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
}
|
|
}
|