Skip to main content

mimick/api_client/
suggestions.rs

1//! Search suggestion autocomplete from the Immich server.
2//!
3//! Wraps `GET /search/suggestions` which returns known values for
4//! country, state, city, camera make, model, and lens model fields.
5
6use std::time::Duration;
7
8use super::ImmichApiClient;
9
10impl ImmichApiClient {
11    /// Fetch autocomplete suggestions for a given metadata dimension.
12    ///
13    /// `suggestion_type` must be one of: `"country"`, `"state"`, `"city"`,
14    /// `"camera-make"`, `"camera-model"`, `"camera-lens-model"`.
15    pub async fn fetch_search_suggestions(
16        &self,
17        suggestion_type: &str,
18    ) -> Result<Vec<String>, String> {
19        let base_url = self
20            .get_active_url()
21            .await
22            .ok_or_else(|| "No active connection".to_string())?;
23        let settings = self.settings_snapshot();
24        let url = format!(
25            "{}/api/search/suggestions?type={}",
26            base_url, suggestion_type
27        );
28        match self
29            .client
30            .get(&url)
31            .header("x-api-key", &settings.api_key)
32            .header("Accept", "application/json")
33            .timeout(Duration::from_secs(10))
34            .send()
35            .await
36        {
37            Ok(resp) if resp.status().is_success() => {
38                let items: Vec<String> = resp.json().await.map_err(|e| e.to_string())?;
39                self.clear_issue().await;
40                Ok(items)
41            }
42            Ok(resp) => Err(format!("HTTP {}", resp.status())),
43            Err(err) => Err(err.to_string()),
44        }
45    }
46}