ApiPoll mechanism but without reqwest func

pull/3/head
prplV 2025-01-16 17:58:11 +03:00
parent d8fcf19322
commit a017cbb18b
1 changed files with 33 additions and 8 deletions

View File

@ -3,34 +3,59 @@ use anyhow::Result;
use integr_structs::api::ApiConfig;
use log::info;
use tokio::sync::mpsc::Receiver;
// use reqwest::Client;
use tokio::time::{sleep, Duration};
use reqwest::Client;
struct ApiPoll<'a> {
config : &'a mut ApiConfig,
// client : Client,
client : Client,
}
impl<'a> ApiPoll<'a> {
pub async fn new(poll_cfg : &'a mut ApiConfig) -> Self {
Self {
config : poll_cfg,
// client : Client::new(),
client : Client::new(),
}
}
// can be weak and with bug test needed
pub async fn change_config(&mut self, conf: ApiConfig) {
*self.config = conf;
}
pub async fn is_default(&self) -> bool {
self.config.endpoints.len() == 0
}
pub async fn process_polling(&self) -> Result<Vec<String>> {
let mut buffer: Vec<String> = vec![];
Ok(buffer)
}
pub async fn get_delay(&self) -> u32 {
self.config.delay
}
}
// for api info pulling
pub async fn init_api_grub_mechanism(config: ApiConfig, rx: &mut Receiver<ApiConfig>) -> Result<()> {
info!("Initializing API-info grubbing mechanism...");
let mut config = config;
let mut poller = ApiPoll::new(&mut config).await;
loop {
match config.endpoints.len() {
0 => todo!(),
_ => todo!(),
if poller.is_default().await {
sleep(Duration::from_secs(5)).await;
} else {
if rx.len() > 0 {
if let Some(conf) = rx.recv().await {
poller.change_config(conf).await;
info!("Config changed");
}
}
Ok(())
info!("Data from API: {:?}", poller.process_polling().await);
sleep(Duration::from_secs(poller.get_delay().await as u64)).await;
}
}
// Ok(())
}