More user friendly immich url handling

This commit is contained in:
Arnaud_Cayrol 2026-02-04 19:51:44 +01:00
parent 6dc406fddb
commit cc9ac97284
2 changed files with 65 additions and 3 deletions

View file

@ -9,8 +9,8 @@ output_dir = "output"
# Your Immich API key (get from Immich user settings)
api_key = "your-api-key-here"
# Immich server URL (include /api suffix)
base_url = "http://192.168.1.100:2283/api"
# Immich server URL (/api suffix is optional, will be added automatically)
base_url = "http://192.168.1.100:2283"
# Request timeout in seconds
timeout_secs = 30

View file

@ -102,11 +102,22 @@ impl ImmichClient {
Ok(Self {
client,
base_url: config.base_url.trim_end_matches('/').to_string(),
base_url: Self::sanitize_base_url(&config.base_url),
api_key: config.api_key.clone(),
})
}
/// Sanitize the base URL to ensure it ends with /api.
fn sanitize_base_url(url: &str) -> String {
let trimmed = url.trim_end_matches('/');
if trimmed.ends_with("/api") {
trimmed.to_string()
} else {
format!("{}/api", trimmed)
}
}
/// Validate the connection to Immich.
pub async fn validate_connection(&self) -> Result<ServerInfo> {
let url = format!("{}/server/about", self.base_url);
@ -272,6 +283,45 @@ impl ImmichClient {
mod tests {
use super::*;
#[test]
fn test_sanitize_base_url() {
// URL already ends with /api
assert_eq!(
ImmichClient::sanitize_base_url("http://localhost:2283/api"),
"http://localhost:2283/api"
);
// URL ends with /api/ (trailing slash)
assert_eq!(
ImmichClient::sanitize_base_url("http://localhost:2283/api/"),
"http://localhost:2283/api"
);
// URL without /api
assert_eq!(
ImmichClient::sanitize_base_url("http://localhost:2283"),
"http://localhost:2283/api"
);
// URL with trailing slash, no /api
assert_eq!(
ImmichClient::sanitize_base_url("http://localhost:2283/"),
"http://localhost:2283/api"
);
// URL with path but no /api
assert_eq!(
ImmichClient::sanitize_base_url("http://example.com/immich"),
"http://example.com/immich/api"
);
// URL with path and trailing slash
assert_eq!(
ImmichClient::sanitize_base_url("http://example.com/immich/"),
"http://example.com/immich/api"
);
}
#[test]
fn test_client_creation() {
let config = ApiConfig {
@ -283,6 +333,18 @@ mod tests {
assert!(client.is_ok());
}
#[test]
fn test_client_creation_without_api_suffix() {
// URL without /api should be sanitized automatically
let config = ApiConfig {
api_key: "test-key".to_string(),
base_url: "http://localhost:2283".to_string(),
timeout_secs: 30,
};
let client = ImmichClient::new(&config).unwrap();
assert_eq!(client.base_url, "http://localhost:2283/api");
}
/// Helper to create a client for the demo server
fn demo_client() -> ImmichClient {
let config = ApiConfig {