imgurs/src/api/client.rs

121 lines
3.2 KiB
Rust
Raw Normal View History

macro_rules! api_url (
($path: expr) => (
format!("{}{}", "https://api.imgur.com/3/", $path)
);
);
pub(crate) use api_url;
2022-06-12 15:28:25 +00:00
use std::{fmt, fs, path::Path};
use reqwest::Client;
2022-06-12 15:41:50 +00:00
use crate::{
requests::{self, RateLimitInfo},
Error, ImageInfo, Result,
};
2022-06-12 15:28:25 +00:00
/// Imgur Client
#[derive(Clone)]
pub struct ImgurClient {
2022-06-12 15:28:25 +00:00
/// Client id for a Imgur API
pub client_id: String,
2022-06-12 15:28:25 +00:00
/// HTTP Client
pub client: Client,
}
impl fmt::Debug for ImgurClient {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ImgurClient - client_id: {}", self.client_id)
}
}
impl ImgurClient {
2022-06-12 15:28:25 +00:00
/// Create a new Imgur Client
2022-05-18 17:48:39 +00:00
/// ```
/// use imgurs::ImgurClient;
///
/// let client = ImgurClient::new("3e3ce0d7ac14d56");
/// ```
pub fn new(client_id: &str) -> Self {
let client_id = client_id.to_string();
let client = Client::new();
ImgurClient { client_id, client }
}
2022-05-18 17:48:39 +00:00
/// Upload image to Imgur
/// ```
/// use imgurs::ImgurClient;
///
/// #[tokio::main]
/// async fn main() {
/// let client = ImgurClient::new("3e3ce0d7ac14d56");
///
/// client.upload_image("https://i.imgur.com/lFaGr1x.png").await.expect("upload image");
/// }
/// ```
pub async fn upload_image(&self, path: &str) -> Result<ImageInfo> {
let mut image = path.to_string();
// check if the specified file exists if not then check if it is a url
2022-06-12 15:28:25 +00:00
if Path::new(path).exists() {
let bytes = fs::read(path)?;
image = base64::encode(bytes)
2022-05-18 17:48:39 +00:00
}
2022-06-12 15:28:25 +00:00
// validate url adress
else if !validator::validate_url(path) {
Err(Error::InvalidUrlOrFile(path.to_string()))?;
}
2022-06-12 15:28:25 +00:00
requests::upload_image(self, image).await
}
2022-05-18 17:48:39 +00:00
/// Delete image from Imgur
/// ```
/// use imgurs::ImgurClient;
///
/// #[tokio::main]
/// async fn main() {
/// let client = ImgurClient::new("3e3ce0d7ac14d56");
///
/// let image = client.upload_image("https://i.imgur.com/lFaGr1x.png").await.expect("upload image");
/// let deletehash = image.data.deletehash.unwrap();
///
/// client.delete_image(&deletehash).await.expect("delete image");
/// }
/// ```
pub async fn delete_image(&self, delete_hash: &str) -> Result<()> {
2022-06-12 15:28:25 +00:00
requests::delete_image(self, delete_hash).await
}
2022-06-12 15:28:25 +00:00
/// Get Rame Limit of this Imgur Client
2022-05-18 17:48:39 +00:00
/// ```
/// use imgurs::ImgurClient;
///
/// #[tokio::main]
/// async fn main() {
/// let client = ImgurClient::new("3e3ce0d7ac14d56");
///
/// client.rate_limit().await.expect("get rate limit");
/// }
/// ```
pub async fn rate_limit(&self) -> Result<RateLimitInfo> {
2022-06-12 15:28:25 +00:00
requests::rate_limit(self).await
}
2022-06-12 15:28:25 +00:00
/// Get image info from a Imgur
2022-05-18 17:48:39 +00:00
/// ```
/// use imgurs::ImgurClient;
///
/// #[tokio::main]
/// async fn main() {
/// let client = ImgurClient::new("3e3ce0d7ac14d56");
///
/// client.image_info("lFaGr1x").await.expect("delete image");
/// }
/// ```
pub async fn image_info(&self, id: &str) -> Result<ImageInfo> {
2022-06-12 15:28:25 +00:00
requests::get_image(self, id).await
}
}