zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
// fn main() {
//     // 初始化日志记录器
//     env_logger::init();

//     // 设置请求 URL
//     let url = "https://jsonplaceholder.typicode.com/todos/1";

//     // 构建 HTTP 客户端
//     let client = reqwest::blocking::Client::new();

//     // 发送 GET 请求
//     let response = client.get(url).send();

//     // 处理响应
//     match response {
//         Ok(resp) => {
//             // 打印 curl 指令
//             let curl_command = reqwest::blocking::RequestBuilder::new(reqwest::Method::GET, url)
//                 .build()
//                 .unwrap()
//                 .cancellable_curl_command();

//             println!("Curl Command: {:?}", curl_command);

//             // 处理其他响应逻辑...
//             println!("Response: {:?}", resp.text());
//         }
//         Err(err) => {
//             // 处理错误...
//             eprintln!("Error: {:?}", err);
//         }
//     }
// }

#[derive(Clone)]
pub struct HttpClient {
    pub client: reqwest::Client,
}

impl HttpClient {
    pub fn build(timeout_sec: usize) -> Self {
        // let mut tls_builder = rustls::ClientConfig::new();
        // tls_builder.danger_accept_invalid_certs(true);
        // let tls_connector = rustls::TlsConnector::from(std::sync::Arc::new(tls_builder));

        let http_client = reqwest::Client::builder()
            .use_rustls_tls()
            .danger_accept_invalid_certs(true)
            .timeout(std::time::Duration::from_secs(timeout_sec as u64))
            .build()
            .unwrap();

        Self {
            client: http_client,
        }
    }

    pub async fn do_get(&self, url: &str) -> Result<String, reqwest::Error> {
        match self.client.get(url).send().await {
            Ok(res) => match res.error_for_status() {
                Ok(o) => {
                    log::debug!(
                        "HttpRequest-Success: method=GET, url={}, StatusCode={}",
                        url,
                        o.status()
                    );

                    let text = o.text().await.unwrap();

                    Ok(text)
                }
                Err(e) => {
                    log::error!("HttpRequest-Failed: method=GET, url={}, error={:?}", url, e);
                    Err(e)
                }
            },
            Err(e) => {
                log::error!("HttpRequest-Error: method=GET, url={}, error={:?}", url, e);
                Err(e)
            }
        }
    }

    pub async fn post_with_json_data<T>(&self, url: &str, body: &T) -> Result<(), reqwest::Error>
    where
        T: serde::Serialize,
    {
        match self
            .client
            .post(url)
            .header("Content-Type", "application/json")
            .json(body)
            .send()
            .await
        {
            Ok(res) => match res.error_for_status() {
                Ok(o) => {
                    log::debug!(
                        "HttpRequest-Success: method=POST, url={}, StatusCode={}",
                        url,
                        o.status()
                    );
                    Ok(())
                }
                Err(e) => {
                    log::error!(
                        "HttpRequest-Failed: method=POST, url={}, error={:?}",
                        url,
                        e
                    );
                    Err(e)
                }
            },
            Err(e) => {
                log::error!("HttpRequest-Error: method=POST, url={}, error={:?}", url, e);
                Err(e)
            }
        }
    }

    pub async fn put_with_json_data<T>(&self, url: &str, body: &T) -> Result<(), reqwest::Error>
    where
        T: serde::Serialize,
    {
        match self
            .client
            .put(url)
            .header("Content-Type", "application/json")
            .json(body)
            .send()
            .await
        {
            Ok(res) => match res.error_for_status() {
                Ok(o) => {
                    log::debug!(
                        "HttpRequest-Success: method=PUT, url={}, StatusCode={}",
                        url,
                        o.status()
                    );
                    Ok(())
                }
                Err(e) => {
                    log::error!("HttpRequest-Failed: method=PUT, url={}, error={:?}", url, e);
                    Err(e)
                }
            },
            Err(e) => {
                log::error!("HttpRequest-Error: method=PUT, url={}, error={:?}", url, e);
                Err(e)
            }
        }
    }

    pub async fn put_with_json_text(&self, url: &str, body: String) -> Result<(), reqwest::Error> {
        match self
            .client
            .put(url)
            .header("Content-Type", "application/json")
            .body(body)
            .send()
            .await
        {
            Ok(res) => match res.error_for_status() {
                Ok(o) => {
                    log::debug!(
                        "HttpRequest-Success: method=PUT, url={}, StatusCode={}",
                        url,
                        o.status()
                    );
                    Ok(())
                }
                Err(e) => {
                    log::error!("HttpRequest-Failed: method=PUT, url={}, error={:?}", url, e);
                    Err(e)
                }
            },
            Err(e) => {
                log::error!("HttpRequest-Error: method=PUT, url={}, error={:?}", url, e);
                Err(e)
            }
        }
    }

    pub async fn post_with_json_data_string<T>(
        &self,
        url: &str,
        body: &T,
    ) -> Result<String, reqwest::Error>
    where
        T: serde::Serialize,
    {
        match self
            .client
            .post(url)
            .header("Content-Type", "application/json")
            .json(body)
            .send()
            .await
        {
            Ok(res) => match res.error_for_status() {
                Ok(o) => {
                    log::debug!(
                        "HttpRequest-Success: method=POST, url={}, StatusCode={}",
                        url,
                        o.status()
                    );

                    o.text().await
                }
                Err(e) => {
                    log::error!(
                        "HttpRequest-Failed: method=POST, url={}, error={:?}",
                        url,
                        e
                    );
                    Err(e)
                }
            },
            Err(e) => {
                log::error!("HttpRequest-Error: method=POST, url={}, error={:?}", url, e);
                Err(e)
            }
        }
    }
}