Add asset download functionality with builder pattern and example

This commit is contained in:
Joakim Hulthe
2026-04-06 08:04:39 +00:00
parent d04b8b221b
commit 1e38963ff6
2 changed files with 84 additions and 0 deletions

View File

@@ -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<bytes::Bytes> {
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)
}
}