Get queue item #9

Merged
phoenix merged 2 commits from get_queue_item into v0.3.0 2026-06-24 15:33:47 -04:00
6 changed files with 107 additions and 6 deletions
+1 -1
View File
@@ -46,7 +46,7 @@ pub async fn load_app() -> Result<App, std::io::Error> {
auth_url: envs[1].value.clone(), auth_url: envs[1].value.clone(),
service_username: envs[2].value.clone(), service_username: envs[2].value.clone(),
service_passphrase: envs[3].value.clone(), service_passphrase: envs[3].value.clone(),
twilio_config: twilio_config, twilio_config,
}), }),
Err(err) => Err(err), Err(err) => Err(err),
} }
+1 -1
View File
@@ -30,7 +30,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut svc = catapult::service::core::Service { let mut svc = catapult::service::core::Service {
app: app.clone(), app: app.clone(),
token: token, token,
}; };
loop { loop {
+27 -3
View File
@@ -6,17 +6,41 @@ pub struct Service {
impl Service { impl Service {
pub async fn do_the_work(&mut self) { pub async fn do_the_work(&mut self) {
println!("Do some work"); println!("Do some work");
println!("Checking queue");
use textsender_models::message::scheduling::ScheduledMessage;
let queue = super::queue::Queue {
app: self.app.clone(),
token: self.token.clone(),
};
let queue_item: Option<ScheduledMessage> = match queue.get_queue().await {
Ok(item) => Some(item),
Err(err) => {
eprintln!("Error: {err:?}");
None
}
};
if queue_item.is_none() {
println!("No queue item found");
return;
}
let queue_item = queue_item.unwrap();
println!("Queue item Id: {:?}", queue_item.id);
todo!( todo!(
r#" r#"
Need to finish this Need to finish this
This function should fetch a token, check if the token is valid, and obtain a refresh token if [X] - This function should fetch a token, check if the token is valid, and obtain a refresh token if
it has expired. it has expired.
Then do the real work. Then do the real work.
1. Check the queue 1. Check the queue - Done
2. If there is an item, get the data, 2. If there is an item, get the data - Done
3. Evaluate if it is schedulable 3. Evaluate if it is schedulable
4. Get the events and iterate each one 4. Get the events and iterate each one
5. starting with the message 5. starting with the message
+10 -1
View File
@@ -1,4 +1,5 @@
pub mod core; pub mod core;
pub mod queue;
pub mod auth { pub mod auth {
pub struct Auth { pub struct Auth {
@@ -72,7 +73,7 @@ pub mod auth {
} }
async fn parse_token_response( async fn parse_token_response(
response: &String, response: &str,
) -> Result<textsender_models::token::LoginResult, std::io::Error> { ) -> Result<textsender_models::token::LoginResult, std::io::Error> {
match serde_json::from_str::<serde_json::Value>(response) { match serde_json::from_str::<serde_json::Value>(response) {
Ok(j) => { Ok(j) => {
@@ -118,3 +119,11 @@ pub mod auth {
now > expire now > expire
} }
} }
pub fn auth_header(
access_token: &str,
) -> (reqwest::header::HeaderName, reqwest::header::HeaderValue) {
let bearer = format!("Bearer {}", access_token);
let header_value = reqwest::header::HeaderValue::from_str(&bearer).unwrap();
(reqwest::header::AUTHORIZATION, header_value)
}
+68
View File
@@ -0,0 +1,68 @@
pub struct Queue {
pub app: crate::app::App,
pub token: textsender_models::token::LoginResult,
}
impl Queue {
pub async fn get_queue(
&self,
) -> Result<textsender_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<textsender_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: textsender_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())),
}
}
View File