1use std::collections::HashMap;
2use reqwest::{Response, RequestBuilder, Client};
3use std::error::Error;
4use crate::model::{self, Dto};
5use crate::model::err::NacosError;
6use lazy_static::lazy_static;
7use std::time::Duration;
8
9lazy_static! {
10 pub static ref CLIENT: Client = Client::new();
11}
12
13pub(crate) async fn query<F>(map: &HashMap<String, String>, func: F)
14 -> Result<Response, Box<dyn Error>>
15 where F: Fn(&Client) -> RequestBuilder
16{
17 let resp = func(&CLIENT).query(map).timeout(Duration::from_secs(10)).send().await?;
18 Ok(resp)
19}
20
21pub(crate) async fn query_resp<T, F>(mut map: HashMap<String, String>, option: &Option<T>, func: F)
22 -> Result<Response, Box<dyn Error>>
23 where T: Dto,
24 F: Fn(&Client) -> RequestBuilder
25{
26 model::catch_mapping(&mut map, option);
27 let resp = query(&map, func).await?;
28 Ok(resp)
29}
30
31pub(crate) async fn query_and_ok<T, F>(map: HashMap<String, String>, option: &Option<T>, func: F)
32 -> Result<(), Box<dyn Error>>
33 where T: Dto,
34 F: Fn(&Client) -> RequestBuilder
35{
36 resp_assert(query_resp(map, option, func).await?, "ok").await
37}
38
39pub(crate) async fn resp_assert(resp: Response, assert: &str) -> Result<(), Box<dyn Error>> {
40 let result = resp.text().await?;
41 if result.ne(assert) { return Err(Box::from(NacosError::throw(&result))); }
42 Ok(())
43}