commit 62b40c4708ff290352f6eee2b8af65a6cc7fba31
parent 674db022036fc3b01b86cd84b98b31fe3e87faa9
Author: Sylvia Ivory <git@sivory.net>
Date: Thu, 19 Jun 2025 18:14:16 -0700
Add unsplash collection api
Diffstat:
2 files changed, 64 insertions(+), 6 deletions(-)
diff --git a/crates/unsplash/src/lib.rs b/crates/unsplash/src/lib.rs
@@ -96,7 +96,19 @@ impl UnsplashClient {
})
}
- pub async fn download_photo(&self, photo: &Photo, opts: Option<PhotoFetchOptions>) -> Result<Bytes> {
+ pub async fn collection(&self, id: &str) -> Result<Collection> {
+ let (collection, _) = self
+ .request(&format!("collections/{id}"), None::<()>)
+ .await?;
+
+ Ok(collection)
+ }
+
+ pub async fn download_photo(
+ &self,
+ photo: &Photo,
+ opts: Option<PhotoFetchOptions>,
+ ) -> Result<Bytes> {
let mut req = self.client.get(&photo.urls.raw);
if let Some(ref query) = opts {
@@ -133,4 +145,12 @@ mod tests {
assert_eq!(collection.per_page, 5);
}
+
+ #[tokio::test]
+ async fn collection() {
+ let client = UnsplashClient::new(&api_key()).unwrap();
+ let collection = client.collection("1053828").await.unwrap();
+
+ assert_eq!(collection.title, "Tabliss Official");
+ }
}
diff --git a/crates/unsplash/src/model.rs b/crates/unsplash/src/model.rs
@@ -6,8 +6,8 @@ use strum::Display;
#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub(crate) enum UnsplashResponse {
- Success(serde_json::Value),
Error { errors: Vec<String> },
+ Success(serde_json::Value),
}
#[derive(Serialize)]
@@ -144,7 +144,7 @@ pub enum Crop {
Faces,
FocalPoint,
Edges,
- Entropy
+ Entropy,
}
#[derive(Serialize)]
@@ -163,7 +163,7 @@ pub enum Format {
Png32,
Webm,
Webp,
- BlurHash
+ BlurHash,
}
#[derive(Serialize)]
@@ -173,7 +173,7 @@ pub enum Auto {
Enhance,
True,
Format,
- Redeye
+ Redeye,
}
#[derive(Serialize)]
@@ -187,7 +187,7 @@ pub enum Fit {
FillMax,
Max,
Min,
- Scale
+ Scale,
}
#[serde_with::skip_serializing_none]
@@ -217,3 +217,41 @@ fn csv<S: Serializer, T: Display>(list: &Option<Vec<T>>, serializer: S) -> Resul
serializer.serialize_none()
}
}
+
+#[derive(Deserialize, Debug, Clone)]
+pub struct CollectionLinks {
+ #[serde(rename = "self")]
+ pub this: String,
+ pub html: String,
+ pub photos: String,
+ pub related: String,
+}
+
+#[derive(Deserialize, Debug, Clone)]
+pub struct PreviewPhoto {
+ pub id: String,
+ pub slug: String,
+ pub created_at: String,
+ pub updated_at: String,
+ pub blur_hash: String,
+ pub asset_type: String,
+ pub urls: PhotoUrls,
+}
+
+#[derive(Deserialize, Debug, Clone)]
+pub struct Collection {
+ pub id: String,
+ pub title: String,
+ pub description: Option<String>,
+ pub published_at: String,
+ pub last_collected_at: String,
+ pub updated_at: String,
+ pub featured: bool,
+ pub total_photos: usize,
+ pub private: bool,
+ pub share_key: String,
+ pub links: CollectionLinks,
+ pub user: User,
+ pub cover_photo: Option<Photo>,
+ pub preview_photos: Vec<PreviewPhoto>,
+}