2022-06-12 15:28:25 +00:00
|
|
|
mod client;
|
|
|
|
mod image_type;
|
2022-06-17 21:07:12 +00:00
|
|
|
mod requests;
|
2022-06-12 15:28:25 +00:00
|
|
|
mod send_api_request;
|
2022-01-22 22:10:11 +00:00
|
|
|
|
2022-06-12 15:41:50 +00:00
|
|
|
pub(crate) use client::api_url;
|
2022-04-03 19:01:58 +00:00
|
|
|
pub use client::ImgurClient;
|
2022-01-23 12:05:32 +00:00
|
|
|
pub use image_type::*;
|
2022-06-12 15:28:25 +00:00
|
|
|
pub use send_api_request::*;
|
2022-06-17 21:00:45 +00:00
|
|
|
|
2022-08-24 13:35:51 +00:00
|
|
|
use crate::{Error, Result};
|
|
|
|
|
2022-06-17 21:00:45 +00:00
|
|
|
impl ImgurClient {
|
|
|
|
/// Create a new Imgur Client
|
|
|
|
/// ```
|
|
|
|
/// use imgurs::ImgurClient;
|
|
|
|
///
|
|
|
|
/// let client = ImgurClient::new("3e3ce0d7ac14d56");
|
|
|
|
/// ```
|
|
|
|
pub fn new(client_id: &str) -> Self {
|
|
|
|
let client_id = client_id.to_string();
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
ImgurClient { client_id, client }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
if std::path::Path::new(path).exists() {
|
|
|
|
let bytes = std::fs::read(path)?;
|
|
|
|
image = base64::encode(bytes)
|
|
|
|
}
|
|
|
|
// validate url adress
|
|
|
|
else if !validator::validate_url(path) {
|
|
|
|
Err(Error::InvalidUrlOrFile(path.to_string()))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
requests::upload_image(self, image).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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<()> {
|
|
|
|
requests::delete_image(self, delete_hash).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get Rame Limit of this Imgur Client
|
|
|
|
/// ```
|
|
|
|
/// 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<requests::RateLimitInfo> {
|
|
|
|
requests::rate_limit(self).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get image info from a Imgur
|
|
|
|
/// ```
|
|
|
|
/// 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> {
|
|
|
|
requests::get_image(self, id).await
|
|
|
|
}
|
|
|
|
}
|