From 1e38963ff6b767c5331ba9a310cffc4a37f24d19 Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Mon, 6 Apr 2026 08:04:39 +0000 Subject: [PATCH] Add asset download functionality with builder pattern and example --- examples/download_asset.rs | 40 ++++++++++++++++++++++++++++++++++ src/apis/assets.rs | 44 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 examples/download_asset.rs diff --git a/examples/download_asset.rs b/examples/download_asset.rs new file mode 100644 index 0000000..9473765 --- /dev/null +++ b/examples/download_asset.rs @@ -0,0 +1,40 @@ +//! Example: Download an asset from Immich + +use immich_sdk::Client; +use std::fs; + +#[tokio::main] +async fn main() -> Result<(), Box> { + // Create a client + let client = Client::from_url("https://immich.example.com")?.with_api_key("your-api-key"); + + // Asset ID to download (replace with a real asset ID) + let asset_id = "your-asset-id-here".parse()?; + + // Download the asset + println!("Downloading asset..."); + let bytes = client.assets().download(asset_id).execute().await?; + + // Save to file + let output_path = "/path/to/output/photo.jpg"; + fs::write(output_path, &bytes)?; + println!("Saved {} bytes to {}", bytes.len(), output_path); + + // Or download the edited version if available + let edited_bytes = client + .assets() + .download(asset_id) + .edited() + .execute() + .await?; + + let edited_path = "/path/to/output/photo_edited.jpg"; + fs::write(edited_path, &edited_bytes)?; + println!( + "Saved edited version {} bytes to {}", + edited_bytes.len(), + edited_path + ); + + Ok(()) +} diff --git a/src/apis/assets.rs b/src/apis/assets.rs index ff2efad..ca2daa3 100644 --- a/src/apis/assets.rs +++ b/src/apis/assets.rs @@ -39,6 +39,11 @@ impl AssetsApi { pub fn delete(&self) -> DeleteAssetsBuilder { DeleteAssetsBuilder::new(self.client.clone()) } + + /// Download an asset + pub fn download(&self, id: AssetId) -> DownloadAssetBuilder { + DownloadAssetBuilder::new(self.client.clone(), id) + } } /// Builder for listing assets @@ -255,3 +260,42 @@ impl DeleteAssetsBuilder { Ok(()) } } + +/// Builder for downloading an asset +#[derive(Debug)] +pub struct DownloadAssetBuilder { + client: Client, + id: AssetId, + edited: bool, +} + +impl DownloadAssetBuilder { + /// Create a new download builder + const fn new(client: Client, id: AssetId) -> Self { + Self { + client, + id, + edited: false, + } + } + + /// Download the edited version if available + pub fn edited(mut self) -> Self { + self.edited = true; + self + } + + /// Execute the download + pub async fn execute(self) -> Result { + let path = format!("/assets/{}/original", self.id); + let mut req = self.client.get(&path); + + if self.edited { + req = req.query(&[("edited", "true")]); + } + + let response = self.client.execute(req.build()?).await?; + let bytes = response.bytes().await?; + Ok(bytes) + } +}