feat: add get_album function (#76)

Co-authored-by: Andre Julius <noromoron@gmail.com>
This commit is contained in:
MedzikUser 2022-09-05 21:02:27 +02:00
parent 31c737689c
commit ecb5855e75
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
5 changed files with 105 additions and 2 deletions

62
src/imgur/album_type.rs Normal file
View File

@ -0,0 +1,62 @@
use serde::{Deserialize, Serialize};
use crate::ImageInfoData;
/// Album Info Response
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct AlbumInfo {
/// Image Data
pub data: AlbumInfoData,
/// Request processed success or not.
pub success: bool,
/// HTTP status code from API request.
pub status: i32,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct AlbumInfoData {
/// Album ID
pub id: String,
/// Title of the album
pub title: String,
/// Description of the album
pub description: Option<String>,
pub datetime: i64,
pub cover: String,
pub cover_edited: Option<String>,
pub cover_width: i64,
pub cover_height: i64,
pub account_url: Option<String>,
pub account_id: Option<String>,
pub privacy: String,
pub layout: String,
pub views: i64,
/// Album link
pub link: String,
pub favorite: bool,
pub nsfw: bool,
pub section: Option<String>,
pub images_count: i64,
pub in_gallery: bool,
pub is_ad: bool,
pub include_album_ads: bool,
pub is_album: bool,
pub images: Vec<ImageInfoData>,
pub ad_config: AdConfig,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct AdConfig {
#[serde(rename = "safeFlags")]
pub safe_flags: Vec<String>,
#[serde(rename = "highRiskFlags")]
pub high_risk_flags: Vec<String>,
#[serde(rename = "unsafeFlags")]
pub unsafe_flags: Vec<String>,
#[serde(rename = "wallUnsafeFlags")]
pub wall_unsafe_flags: Vec<String>,
#[serde(rename = "showsAds")]
pub shows_ads: bool,
#[serde(rename = "showAdLevel")]
pub show_ad_level: i64,
}

View File

@ -1,8 +1,10 @@
mod album_type;
mod client;
mod image_type;
mod requests;
mod send_api_request;
pub use album_type::*;
pub(crate) use client::api_url;
pub use client::ImgurClient;
pub use image_type::*;
@ -97,4 +99,19 @@ impl ImgurClient {
pub async fn image_info(&self, id: &str) -> Result<ImageInfo> {
requests::get_image(self, id).await
}
/// Get album info from a Imgur
/// ```
/// use imgurs::ImgurClient;
///
/// #[tokio::main]
/// async fn main() {
/// let client = ImgurClient::new("3e3ce0d7ac14d56");
///
/// client.album_info("lFaGr1x").await.expect("delete album");
/// }
/// ```
pub async fn album_info(&self, id: &str) -> Result<AlbumInfo> {
requests::get_album(self, id).await
}
}

View File

@ -0,0 +1,24 @@
use reqwest::Method;
use crate::{api_url, send_api_request, AlbumInfo, Error, ImgurClient, Result};
pub async fn get_album(client: &ImgurClient, album: &str) -> Result<AlbumInfo> {
// get imgur api url
let uri = api_url!(format!("album/{album}"));
// send request to imgur api
let res = send_api_request(client, Method::GET, uri, None).await?;
// get response http code
let status = res.status();
// check if an error has occurred
if status.is_client_error() || status.is_server_error() {
let body = res.text().await?;
return Err(Error::ApiError(status.as_u16(), body));
}
// return `ImageInfo`
Ok(res.json().await?)
}

View File

@ -1,9 +1,11 @@
mod delete_image;
mod get_album;
mod get_image;
mod rate_limit;
mod upload_image;
pub use delete_image::*;
pub use get_album::*;
pub use get_image::*;
pub use rate_limit::*;
pub use upload_image::*;

View File

@ -89,8 +89,6 @@
//! }
//! ```
#![warn(missing_docs)]
mod error;
pub use error::*;