Get queue item #9
+25
-1
@@ -6,11 +6,35 @@ pub struct Service {
|
||||
impl Service {
|
||||
pub async fn do_the_work(&mut self) {
|
||||
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_some() {
|
||||
println!("No queue item found");
|
||||
return;
|
||||
}
|
||||
let queue_item = queue_item.unwrap();
|
||||
|
||||
println!("Queue item Id: {:?}", queue_item.id);
|
||||
|
||||
todo!(
|
||||
r#"
|
||||
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.
|
||||
|
||||
Then do the real work.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod core;
|
||||
pub mod queue;
|
||||
|
||||
pub mod auth {
|
||||
pub struct Auth {
|
||||
@@ -118,3 +119,11 @@ pub mod auth {
|
||||
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,66 @@
|
||||
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: &String,
|
||||
) -> 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