api grubbing mechanism is created

pull/3/head
prplV 2025-01-17 11:46:04 +03:00
parent a017cbb18b
commit c00fb883c9
1 changed files with 42 additions and 6 deletions

View File

@ -1,11 +1,24 @@
// module to handle unix-socket connection + pulling info from api // module to handle unix-socket connection + pulling info from api
use anyhow::Result; use anyhow::{Error, Result};
use integr_structs::api::ApiConfig; use integr_structs::api::ApiConfig;
use log::info; use log::{error, info};
use tokio::sync::mpsc::Receiver; use tokio::sync::mpsc::Receiver;
use tokio::time::{sleep, Duration}; 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> { struct ApiPoll<'a> {
config : &'a mut ApiConfig, config : &'a mut ApiConfig,
client : Client, client : Client,
@ -27,9 +40,32 @@ impl<'a> ApiPoll<'a> {
} }
pub async fn process_polling(&self) -> Result<Vec<String>> { pub async fn process_polling(&self) -> Result<Vec<String>> {
let mut buffer: Vec<String> = vec![]; let mut buffer: Vec<String> = vec![];
// TODO: rewrite nextly to async
for point in &self.config.endpoints {
Ok(buffer) // 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 { pub async fn get_delay(&self) -> u32 {
self.config.delay self.config.delay