use reqwest::Client; use serde::{Deserialize, Serialize}; use std::fmt::{Display, Formatter}; #[derive(Debug)] pub enum ImgurError { ReqwestError(reqwest::Error), ImgurRequestError(String), } impl From for ImgurError { fn from(e: reqwest::Error) -> Self { Self::ReqwestError(e) } } impl Display for ImgurError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let msg = match self { ImgurError::ReqwestError(err) => format!("Reqwest error: {}", err), ImgurError::ImgurRequestError(msg) => format!("Imgur request error: {}", msg), }; write!(f, "{}", msg) } } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct AlbumData { images: Option>, error: Option, } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct AlbumResponse { data: AlbumData, success: bool, status: i32, } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Image { pub id: String, pub title: Option, pub description: Option, #[serde(rename = "type")] pub img_type: String, pub animated: bool, pub width: i32, pub height: i32, pub size: i32, pub link: String, } pub async fn get_album_images(client_id: &str, album_hash: &str) -> Result, ImgurError> { let client = Client::new(); let res = client .get(format!("https://api.imgur.com/3/album/{}", album_hash)) .header("Authorization", format!("Client-ID {}", client_id)) .send() .await?; let album_response: AlbumResponse = res.json().await?; if album_response.success { Ok(album_response.data.images.unwrap()) } else { Err(ImgurError::ImgurRequestError( album_response.data.error.unwrap(), )) } }