diff --git a/crates/api-grub/src/net.rs b/crates/api-grub/src/net.rs index cb9805f..6d4a29c 100644 --- a/crates/api-grub/src/net.rs +++ b/crates/api-grub/src/net.rs @@ -1,11 +1,24 @@ // module to handle unix-socket connection + pulling info from api -use anyhow::Result; +use anyhow::{Error, Result}; use integr_structs::api::ApiConfig; -use log::info; +use log::{error, info}; use tokio::sync::mpsc::Receiver; use tokio::time::{sleep, Duration}; -use reqwest::Client; +use reqwest::{Client, Method}; +struct RestMethod; + +impl RestMethod { + pub async fn from_str(method: &str) -> Method { + return match method.trim().to_lowercase().as_str() { + "post" => Method::POST, + "patch" => Method::PATCH, + "put" => Method::PUT, + "delete" => Method::DELETE, + "get" | _ => Method::GET + } + } +} struct ApiPoll<'a> { config : &'a mut ApiConfig, client : Client, @@ -27,9 +40,32 @@ impl<'a> ApiPoll<'a> { } pub async fn process_polling(&self) -> Result> { let mut buffer: Vec = vec![]; - - - Ok(buffer) + // TODO: rewrite nextly to async + for point in &self.config.endpoints { + // let a = self.client.get(&point.url).send().await.unwrap(); + // a.text().await.unwrap(); + match self.client.request(RestMethod::from_str(&point.method).await, &point.url).send().await { + Ok(resp) => { + if !resp.status().is_success() { + error!("ErrorCode in Response from API. Check configuration"); + continue; + } + if let Ok(text) = resp.text().await { + info!("{}: {} - Successfull grubbing info", &point.method.to_uppercase(), &point.url); + buffer.push(text); + } else { + error!("{}: {} - Error with extracting text field from Response", &point.method.to_uppercase(), &point.url); + } + }, + Err(er) => { + error!("{}: {} - Query crushed due to {}", &point.method.to_uppercase(), &point.url, er); + }, + } + } + match &buffer.len() { + 0 => Err(Error::msg("Error due to API grubbing. Check config" )), + _ => Ok(buffer), + } } pub async fn get_delay(&self) -> u32 { self.config.delay