From a017cbb18b465e5859c5e313be6eb3a50792aebf Mon Sep 17 00:00:00 2001 From: prplV Date: Thu, 16 Jan 2025 17:58:11 +0300 Subject: [PATCH] ApiPoll mechanism but without reqwest func --- crates/api-grub/src/net.rs | 41 ++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/crates/api-grub/src/net.rs b/crates/api-grub/src/net.rs index 094c182..cb9805f 100644 --- a/crates/api-grub/src/net.rs +++ b/crates/api-grub/src/net.rs @@ -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> { + let mut buffer: Vec = 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) -> 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"); + } + } + info!("Data from API: {:?}", poller.process_polling().await); + sleep(Duration::from_secs(poller.get_delay().await as u64)).await; } } - Ok(()) + // Ok(()) }