Fetch models from an Anthropic-format endpoint. `use_x_api_key`: when `true`, send the key as `x-api-key` header (Anthropic direct). When `false`, send as `Authorization: Bearer` (gateways).
(
base_url: &str,
api_key: Option<&str>,
use_x_api_key: bool,
)
| 91 | /// `use_x_api_key`: when `true`, send the key as `x-api-key` header (Anthropic |
| 92 | /// direct). When `false`, send as `Authorization: Bearer` (gateways). |
| 93 | async fn fetch_anthropic_models( |
| 94 | base_url: &str, |
| 95 | api_key: Option<&str>, |
| 96 | use_x_api_key: bool, |
| 97 | ) -> AppResult<Vec<String>> { |
| 98 | let url = format!("{}/models", base_url.trim_end_matches('/')); |
| 99 | let client = http_client::request_client()?; |
| 100 | let mut req = client |
| 101 | .get(&url) |
| 102 | .header("anthropic-version", "2023-06-01"); |
| 103 | |
| 104 | if let Some(key) = api_key { |
| 105 | if !key.is_empty() { |
| 106 | if use_x_api_key { |
| 107 | req = req.header("x-api-key", key); |
| 108 | } else { |
| 109 | req = req.bearer_auth(key); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | let resp = req |
| 115 | .send() |
| 116 | .await |
| 117 | .map_err(|e| AppError::Internal(format!("Failed to fetch models: {e}")))?; |
| 118 | |
| 119 | if !resp.status().is_success() { |
| 120 | let status = resp.status(); |
| 121 | let body = resp.text().await.unwrap_or_default(); |
| 122 | return Err(AppError::Internal(format!( |
| 123 | "Models endpoint returned {status}: {body}" |
| 124 | ))); |
| 125 | } |
| 126 | |
| 127 | let list: ModelList = resp.json().await.map_err(|e| { |
| 128 | AppError::Internal(format!("Failed to parse models response: {e}")) |
| 129 | })?; |
| 130 | |
| 131 | let mut ids: Vec<String> = list.data.into_iter().map(|m| m.id).collect(); |
| 132 | ids.sort(); |
| 133 | Ok(ids) |
| 134 | } |
no test coverage detected