ApiPoll mechanism but without reqwest func
parent
d8fcf19322
commit
a017cbb18b
|
|
@ -3,34 +3,59 @@ use anyhow::Result;
|
||||||
use integr_structs::api::ApiConfig;
|
use integr_structs::api::ApiConfig;
|
||||||
use log::info;
|
use log::info;
|
||||||
use tokio::sync::mpsc::Receiver;
|
use tokio::sync::mpsc::Receiver;
|
||||||
// use reqwest::Client;
|
use tokio::time::{sleep, Duration};
|
||||||
|
use reqwest::Client;
|
||||||
|
|
||||||
struct ApiPoll<'a> {
|
struct ApiPoll<'a> {
|
||||||
config : &'a mut ApiConfig,
|
config : &'a mut ApiConfig,
|
||||||
// client : Client,
|
client : Client,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ApiPoll<'a> {
|
impl<'a> ApiPoll<'a> {
|
||||||
pub async fn new(poll_cfg : &'a mut ApiConfig) -> Self {
|
pub async fn new(poll_cfg : &'a mut ApiConfig) -> Self {
|
||||||
Self {
|
Self {
|
||||||
config : poll_cfg,
|
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
|
// for api info pulling
|
||||||
pub async fn init_api_grub_mechanism(config: ApiConfig, rx: &mut Receiver<ApiConfig>) -> Result<()> {
|
pub async fn init_api_grub_mechanism(config: ApiConfig, rx: &mut Receiver<ApiConfig>) -> Result<()> {
|
||||||
info!("Initializing API-info grubbing mechanism...");
|
info!("Initializing API-info grubbing mechanism...");
|
||||||
|
|
||||||
let mut config = config;
|
let mut config = config;
|
||||||
|
let mut poller = ApiPoll::new(&mut config).await;
|
||||||
loop {
|
loop {
|
||||||
match config.endpoints.len() {
|
if poller.is_default().await {
|
||||||
0 => todo!(),
|
sleep(Duration::from_secs(5)).await;
|
||||||
_ => todo!(),
|
} 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(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue