mirror of https://github.com/MedzikUser/imgurs
add missing doc
This commit is contained in:
parent
66551e77ad
commit
2d1f1a4001
|
@ -6,15 +6,10 @@ macro_rules! api_url (
|
||||||
|
|
||||||
pub(crate) use api_url;
|
pub(crate) use api_url;
|
||||||
|
|
||||||
use std::{fmt, fs, path::Path};
|
use std::fmt;
|
||||||
|
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
|
|
||||||
use crate::{
|
|
||||||
requests::{self, RateLimitInfo},
|
|
||||||
Error, ImageInfo, Result,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Imgur Client
|
/// Imgur Client
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ImgurClient {
|
pub struct ImgurClient {
|
||||||
|
@ -29,92 +24,3 @@ impl fmt::Debug for ImgurClient {
|
||||||
write!(f, "ImgurClient - client_id: {}", self.client_id)
|
write!(f, "ImgurClient - client_id: {}", self.client_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = 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 Path::new(path).exists() {
|
|
||||||
let bytes = 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<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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -12,27 +12,25 @@ pub enum Error {
|
||||||
/// Invalid file path or URL adress
|
/// Invalid file path or URL adress
|
||||||
#[error("{0} is not url or file path")]
|
#[error("{0} is not url or file path")]
|
||||||
InvalidUrlOrFile(String),
|
InvalidUrlOrFile(String),
|
||||||
/// Reqwest error
|
/// reqwest::Error
|
||||||
#[error("reqwest error - {0}")]
|
#[error("reqwest error - {0}")]
|
||||||
ReqwestError(reqwest::Error),
|
ReqwestError(reqwest::Error),
|
||||||
/// Io Error
|
/// std::io::Error
|
||||||
#[error("io error - {0}")]
|
#[error("io error - {0}")]
|
||||||
IoError(std::io::Error),
|
IoError(std::io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// reqwest::Error
|
|
||||||
impl From<reqwest::Error> for Error {
|
impl From<reqwest::Error> for Error {
|
||||||
fn from(err: reqwest::Error) -> Self {
|
fn from(err: reqwest::Error) -> Self {
|
||||||
Error::ReqwestError(err)
|
Error::ReqwestError(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// reqwest::Error
|
|
||||||
impl From<std::io::Error> for Error {
|
impl From<std::io::Error> for Error {
|
||||||
fn from(err: std::io::Error) -> Self {
|
fn from(err: std::io::Error) -> Self {
|
||||||
Error::IoError(err)
|
Error::IoError(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A `Result` alias where the `Err` case is `imgurs::Error`.
|
/// A `Result` alias where the `Err` case is `imgurs::Error`
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
|
@ -1,29 +1,48 @@
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// Image Info Response
|
/// Image Info Response
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
|
||||||
pub struct ImageInfo {
|
pub struct ImageInfo {
|
||||||
|
/// Image Data
|
||||||
pub data: ImageInfoData,
|
pub data: ImageInfoData,
|
||||||
|
/// Request processed success or not.
|
||||||
pub success: bool,
|
pub success: bool,
|
||||||
|
/// HTTP status code from API request.
|
||||||
pub status: i32,
|
pub status: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Image Info Reponse (`data` json)
|
/// Image Info Reponse (`data` json)
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
|
||||||
pub struct ImageInfoData {
|
pub struct ImageInfoData {
|
||||||
|
/// Image ID
|
||||||
|
/// e.g. `iDYNKJq`
|
||||||
pub id: String,
|
pub id: String,
|
||||||
|
/// Image title
|
||||||
pub title: Option<String>,
|
pub title: Option<String>,
|
||||||
|
/// Description of this image
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
|
/// Image uploaded time
|
||||||
pub datetime: i32,
|
pub datetime: i32,
|
||||||
|
/// Image type
|
||||||
|
/// e.g. `image/png`
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
pub img_type: String,
|
pub img_type: String,
|
||||||
|
/// If image if animated (gif, etc)
|
||||||
pub animated: bool,
|
pub animated: bool,
|
||||||
|
/// Width of this image
|
||||||
pub width: i32,
|
pub width: i32,
|
||||||
|
/// Height of this image
|
||||||
pub height: i32,
|
pub height: i32,
|
||||||
|
/// Image size in bytes
|
||||||
pub size: i32,
|
pub size: i32,
|
||||||
|
/// Unique image views
|
||||||
pub views: i32,
|
pub views: i32,
|
||||||
|
/// Bandwidth used by this image
|
||||||
pub bandwidth: i64,
|
pub bandwidth: i64,
|
||||||
|
/// If image is added to favorite
|
||||||
pub favorite: bool,
|
pub favorite: bool,
|
||||||
|
/// Delete hash (only show after image upload)
|
||||||
pub deletehash: Option<String>,
|
pub deletehash: Option<String>,
|
||||||
|
/// Link of this image
|
||||||
pub link: String,
|
pub link: String,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
pub mod requests;
|
mod requests;
|
||||||
|
|
||||||
mod client;
|
mod client;
|
||||||
mod error;
|
mod error;
|
||||||
mod image_type;
|
mod image_type;
|
||||||
|
@ -10,3 +9,92 @@ pub use client::ImgurClient;
|
||||||
pub use error::*;
|
pub use error::*;
|
||||||
pub use image_type::*;
|
pub use image_type::*;
|
||||||
pub use send_api_request::*;
|
pub use send_api_request::*;
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
52
src/lib.rs
52
src/lib.rs
|
@ -28,37 +28,69 @@
|
||||||
//! ```
|
//! ```
|
||||||
//! use imgurs::ImgurClient;
|
//! use imgurs::ImgurClient;
|
||||||
//!
|
//!
|
||||||
//! let client = ImgurClient::new("client id");
|
//! let client = ImgurClient::new("client_id");
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## Image Upload
|
//! ## Image Upload
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! // From URL
|
//! use imgurs::ImgurClient;
|
||||||
//! let info = client.upload_image("https://i.imgur.com/lFaGr1x.png").await?;
|
|
||||||
//!
|
//!
|
||||||
//! // From File
|
//! #[tokio::main]
|
||||||
//! let info = client.upload_image("path/to/image.png").await?;
|
//! async fn main() {
|
||||||
|
//! let client = ImgurClient::new("client_id");
|
||||||
|
//!
|
||||||
|
//! // From URL
|
||||||
|
//! let info = client.upload_image("https://i.imgur.com/lFaGr1x.png").await.unwrap();
|
||||||
|
//! println!("{:?}", info);
|
||||||
|
//!
|
||||||
|
//! // From File
|
||||||
|
//! let info = client.upload_image("path/to/file.png").await.unwrap();
|
||||||
|
//! println!("{:?}", info);
|
||||||
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## Delete Image
|
//! ## Delete Image
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! client.delete_image("Delete Hash").await?; // delete hash
|
//! use imgurs::ImgurClient;
|
||||||
|
//!
|
||||||
|
//! #[tokio::main]
|
||||||
|
//! async fn main() {
|
||||||
|
//! let client = ImgurClient::new("client_id");
|
||||||
|
//!
|
||||||
|
//! client.delete_image("delete_hash").await.unwrap(); // delete hash
|
||||||
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## Get Image Info
|
//! ## Get Image Info
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! let info = client.image_info("lFaGr1x").await?; // image id
|
//! use imgurs::ImgurClient;
|
||||||
//!
|
//!
|
||||||
//! println!("{:?}", info);
|
//! #[tokio::main]
|
||||||
|
//! async fn main() {
|
||||||
|
//! let client = ImgurClient::new("client_id");
|
||||||
|
//!
|
||||||
|
//! let info = client.image_info("lFaGr1x").await.unwrap(); // image id
|
||||||
|
//!
|
||||||
|
//! println!("{:?}", info);
|
||||||
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## Get Client RateLimit
|
//! ## Get Client RateLimit
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! let info = client.rate_limit.await?;
|
//! use imgurs::ImgurClient;
|
||||||
//!
|
//!
|
||||||
//! println!("{:?}", info);
|
//! #[tokio::main]
|
||||||
|
//! async fn main() {
|
||||||
|
//! let client = ImgurClient::new("client_id");
|
||||||
|
//!
|
||||||
|
//! let info = client.rate_limit().await.unwrap();
|
||||||
|
//!
|
||||||
|
//! println!("{:?}", info);
|
||||||
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
#![warn(missing_docs)]
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
|
|
||||||
pub use api::*;
|
pub use api::*;
|
||||||
|
|
Loading…
Reference in New Issue