diff --git a/src/service/core.rs b/src/service/core.rs index 839e38f..d9869aa 100644 --- a/src/service/core.rs +++ b/src/service/core.rs @@ -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 = 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. diff --git a/src/service/mod.rs b/src/service/mod.rs index d3bd740..7fcb09e 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -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) +} diff --git a/src/service/queue.rs b/src/service/queue.rs new file mode 100644 index 0000000..7321441 --- /dev/null +++ b/src/service/queue.rs @@ -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 { + 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 { + match serde_json::from_str::(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())), + } + } diff --git a/src/service/scheduler.rs b/src/service/scheduler.rs new file mode 100644 index 0000000..e69de29