Get queue item (#9)
Reviewed-on: #9
This commit was merged in pull request #9.
This commit is contained in:
+1
-1
@@ -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
@@ -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
@@ -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
@@ -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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -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())),
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user