| use reqwest::{Client, Proxy}; | |
| use crate::modules::config::load_app_config; | |
| /// Create a unified HTTP client with global configuration | |
| pub fn create_client(timeout_secs: u64) -> Client { | |
| if let Ok(config) = load_app_config() { | |
| create_client_with_proxy(timeout_secs, Some(config.proxy.upstream_proxy)) | |
| } else { | |
| create_client_with_proxy(timeout_secs, None) | |
| } | |
| } | |
| /// Create HTTP client with specific proxy configuration | |
| pub fn create_client_with_proxy( | |
| timeout_secs: u64, | |
| proxy_config: Option<crate::proxy::config::UpstreamProxyConfig> | |
| ) -> Client { | |
| let mut builder = Client::builder() | |
| .timeout(std::time::Duration::from_secs(timeout_secs)); | |
| if let Some(config) = proxy_config { | |
| if config.enabled && !config.url.is_empty() { | |
| match Proxy::all(&config.url) { | |
| Ok(proxy) => { | |
| builder = builder.proxy(proxy); | |
| tracing::info!("HTTP client using upstream proxy: {}", config.url); | |
| } | |
| Err(e) => { | |
| tracing::error!("Invalid proxy address: {}, error: {}", config.url, e); | |
| } | |
| } | |
| } | |
| } | |
| builder.build().unwrap_or_else(|_| Client::new()) | |
| } | |