Files
immich-sdk/examples/album_management.rs
Joakim Hulthe 2e7db3b35a Add integration testing infrastructure with Podman Compose
- Add docker/podman-compose.yml for Immich server (no ML)
- Add test-data/sample-photos/ with 3 public domain images
- Add scripts/start-immich.sh for container startup + admin/API key creation
- Add scripts/seed-data.sh for uploading test photos and creating albums
- Add scripts/stop-immich.sh for cleanup with volume destruction
- Add scripts/run-example.sh wrapper (fixed env var export)
- Update examples to use IMMICH_URL and IMMICH_API_KEY env vars
- Add .github/workflows/integration-test.yml for CI
- Update assets.list() to use /search/metadata endpoint
- Update AGENTS.md with example running instructions
- Add 5 new examples: search_metadata, album_management, timeline_browsing,
  delete_assets, server_info
2026-04-14 20:13:30 +00:00

134 lines
4.4 KiB
Rust

//! Example: Album management (CRUD operations)
//!
//! This example demonstrates creating, reading, updating, and deleting albums
//!
//! Environment variables:
//! - IMMICH_URL: The Immich server URL (defaults to http://localhost:2283)
//! - IMMICH_API_KEY: Your API key (required)
use immich_sdk::Client;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Configure the client from environment variables
let url = std::env::var("IMMICH_URL").unwrap_or_else(|_| "http://localhost:2283".to_string());
let api_key =
std::env::var("IMMICH_API_KEY").expect("IMMICH_API_KEY environment variable not set");
// Create a client
let client = Client::from_url(&url)?.with_api_key(api_key);
println!("=== Album Management Example ===\n");
// List all albums
println!("1. Listing all albums...");
let albums = client.albums().list().execute().await?;
println!(" Found {} albums", albums.len());
for album in &albums {
println!(
" - {} (ID: {}, {} assets)",
album.album_name, album.id, album.asset_count
);
}
println!();
// Get details of the first album if one exists
let existing_album = albums.first().cloned();
if let Some(ref album) = existing_album {
println!("2. Getting album details...");
let details = client.albums().get(album.id).execute().await?;
println!(" Album: {}", details.album_name);
println!(" Description: {}", details.description);
println!(" Asset count: {}", details.asset_count);
println!();
}
// Create a new album
println!("3. Creating a new album...");
let new_album = client
.albums()
.create()
.name("SDK Test Album")
.execute()
.await?;
println!(
" Created album: {} (ID: {})",
new_album.album_name, new_album.id
);
println!();
// If we have assets, add some to the album
let asset_ids: Vec<_> = existing_album
.as_ref()
.map(|a| a.assets.iter().map(|asset| asset.id).collect())
.unwrap_or_default();
if !asset_ids.is_empty() {
println!("4. Adding assets to the new album...");
let asset_ids_to_add: Vec<_> = asset_ids.iter().take(2).copied().collect();
let num_added = asset_ids_to_add.len();
client
.albums()
.add_assets(new_album.id)
.asset_ids(asset_ids_to_add.clone())
.execute()
.await?;
println!(" Added {} assets to the album", num_added);
// Get updated album details
let updated_details = client.albums().get(new_album.id).execute().await?;
println!(" Album now has {} assets", updated_details.asset_count);
println!();
// Remove assets from the album
println!("5. Removing assets from the album...");
client
.albums()
.remove_assets(new_album.id)
.asset_ids(asset_ids_to_add)
.execute()
.await?;
println!(" Removed {} assets from the album", num_added);
let after_removal = client.albums().get(new_album.id).execute().await?;
println!(" Album now has {} assets", after_removal.asset_count);
println!();
} else {
println!("4. Skipping asset operations (no existing assets found)");
println!();
}
// Update the album
println!("6. Updating album name and description...");
let updated = client
.albums()
.update(new_album.id)
.name("Updated SDK Album")
.description("This description was updated by the SDK example")
.execute()
.await?;
println!(" Updated album name: {}", updated.album_name);
println!(" Updated description: {}", updated.description);
println!();
// Delete the album
println!("7. Deleting the album...");
client.albums().delete(new_album.id).execute().await?;
println!(" Album deleted successfully");
println!();
// Verify deletion by listing albums again
println!("8. Verifying deletion...");
let albums_after = client.albums().list().execute().await?;
let still_exists = albums_after.iter().any(|a| a.id == new_album.id);
if still_exists {
println!(" Warning: Album still exists after deletion attempt");
} else {
println!(" Confirmed: Album no longer exists");
}
println!("\n=== Example completed successfully ===");
Ok(())
}