1use crate::types::AiLibError;
2use reqwest::Client;
3use std::time::Duration;
4
5pub async fn health_check(base_url: &str) -> Result<(), AiLibError> {
9 let client_builder = Client::builder().timeout(Duration::from_secs(5));
10
11 let client_builder = if let Ok(proxy_url) = std::env::var("AI_PROXY_URL") {
13 if let Ok(proxy) = reqwest::Proxy::all(&proxy_url) {
14 client_builder.proxy(proxy)
15 } else {
16 client_builder
17 }
18 } else {
19 client_builder
20 };
21
22 let client = client_builder.build().map_err(|e| AiLibError::NetworkError(format!("Failed to build HTTP client: {}", e)))?;
23
24 let models_url = if base_url.ends_with('/') {
26 format!("{}models", base_url)
27 } else {
28 format!("{}/models", base_url)
29 };
30
31 let resp = client.get(&models_url).send().await;
32 match resp {
33 Ok(r) if r.status().is_success() => return Ok(()),
34 _ => {
35 let resp2 = client.get(base_url).send().await;
37 match resp2 {
38 Ok(r2) if r2.status().is_success() => Ok(()),
39 Ok(r2) => Err(AiLibError::NetworkError(format!("Health check returned status {}", r2.status()))),
40 Err(e) => Err(AiLibError::NetworkError(format!("Health check request failed: {}", e))),
41 }
42 }
43 }
44}