Skip to main content

x_one/xhttp/
init.rs

1//! xhttp 初始化逻辑
2
3use crate::xconfig;
4use crate::xutil;
5
6use super::client::{HTTP_CLIENT, build_client};
7use super::config::{XHTTP_CONFIG_KEY, load_config};
8
9/// 初始化 HTTP 客户端
10pub fn init_xhttp() -> Result<(), crate::error::XOneError> {
11    if !xconfig::contain_key(XHTTP_CONFIG_KEY) {
12        xutil::info_if_enable_debug("XHttp config not found, skip init");
13        return Ok(());
14    }
15
16    let config = load_config();
17    let http_client = build_client(&config)?;
18
19    if HTTP_CLIENT.set(http_client).is_err() {
20        xutil::warn_if_enable_debug(
21            "XHttp client already initialized (accessed before init), config may not take effect",
22        );
23    }
24
25    xutil::info_if_enable_debug(&format!("XHttp init success, timeout=[{}]", config.timeout));
26    Ok(())
27}