From aabbea4182df5e45abdffa5a94cb6d697f64baa9 Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Wed, 18 May 2022 19:48:39 +0200 Subject: [PATCH] chore: small changes --- .github/workflows/build.yml | 5 +++ .gitignore | 5 ++- README.md | 4 +- src/api/client.rs | 77 ++++++++++++++++++++++++++++++------- src/api/delete_image.rs | 2 +- src/api/get_image.rs | 2 +- src/cli/delete_image.rs | 2 +- src/cli/info_image.rs | 2 +- src/cli/upload_image.rs | 2 +- src/lib.rs | 14 +++---- src/main.rs | 2 +- 11 files changed, 88 insertions(+), 29 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1335a0d..a89681c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -41,6 +41,11 @@ jobs: with: command: build + - name: cargo test + uses: actions-rs/cargo@v1 + with: + command: test --all-features + - name: cargo clippy uses: actions-rs/cargo@v1 with: diff --git a/.gitignore b/.gitignore index 5a68c1f..80950d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ # Generated by Cargo # will have compiled files and executables -/debug /target + +# IDE configs +.vscode +.idea diff --git a/README.md b/README.md index e747fc0..32f7812 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ ## Screenshots -![upload](https://i.imgur.com/MG35kvf.png) +![](https://i.imgur.com/MG35kvf.png) -![delete](https://cdn.magicuser.cf/TSxBrhO.png) +![](https://i.imgur.com/TSxBrhO.png) ## Shell completions diff --git a/src/api/client.rs b/src/api/client.rs index cb7b54b..ba0bef1 100644 --- a/src/api/client.rs +++ b/src/api/client.rs @@ -6,7 +6,7 @@ macro_rules! api_url ( use std::{fmt, fs, io, path::Path}; -use anyhow::Error; +use anyhow::Result; pub(crate) use api_url; use reqwest::Client; @@ -24,24 +24,39 @@ impl fmt::Debug for ImgurClient { } impl ImgurClient { - pub fn new(client_id: String) -> Self { + /// Create 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 } } - pub async fn upload_image(&self, path: String) -> Result { - let mut image: String = path.clone(); + /// 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 { + 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) - .map_err(|err| err.to_string()) - .expect("read file"); - + let bytes = fs::read(&path)?; image = base64::encode(bytes) - + } // validate adress url - } else if !validator::validate_url(&path) { + else if !validator::validate_url(&*path) { let err = io::Error::new( io::ErrorKind::Other, format!("{path} is not url or file path"), @@ -53,15 +68,51 @@ impl ImgurClient { upload_image(self, image).await } - pub async fn delete_image(&self, delete_hash: String) -> Result<(), Error> { + /// 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<()> { delete_image(self, delete_hash).await } - pub async fn rate_limit(&self) -> Result { + /// Client Rate Limit + /// ``` + /// 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 { rate_limit(self).await } - pub async fn image_info(&self, id: String) -> Result { + /// Get Imgur image info + /// ``` + /// 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 { get_image(self, id).await } } diff --git a/src/api/delete_image.rs b/src/api/delete_image.rs index d207cc1..f04d2fd 100644 --- a/src/api/delete_image.rs +++ b/src/api/delete_image.rs @@ -5,7 +5,7 @@ use reqwest::Method; use super::{client::api_url, send_api_request, ImgurClient}; -pub async fn delete_image(client: &ImgurClient, delete_hash: String) -> Result<(), Error> { +pub async fn delete_image(client: &ImgurClient, delete_hash: &str) -> Result<(), Error> { // get imgur api url let uri = api_url!(format!("image/{delete_hash}")); diff --git a/src/api/get_image.rs b/src/api/get_image.rs index 542a91d..c78e0fe 100644 --- a/src/api/get_image.rs +++ b/src/api/get_image.rs @@ -5,7 +5,7 @@ use reqwest::Method; use super::{client::api_url, send_api_request, ImageInfo, ImgurClient}; -pub async fn get_image(client: &ImgurClient, image: String) -> Result { +pub async fn get_image(client: &ImgurClient, image: &str) -> Result { // get imgur api url let uri = api_url!(format!("image/{image}")); diff --git a/src/cli/delete_image.rs b/src/cli/delete_image.rs index 4617832..9fc5e45 100644 --- a/src/cli/delete_image.rs +++ b/src/cli/delete_image.rs @@ -4,7 +4,7 @@ use imgurs::ImgurClient; pub async fn delete_image(client: ImgurClient, delete_hash: String) { // delete image from imgur client - .delete_image(delete_hash) + .delete_image(&delete_hash) .await .expect("send api request"); diff --git a/src/cli/info_image.rs b/src/cli/info_image.rs index f0fc4e3..56d9317 100644 --- a/src/cli/info_image.rs +++ b/src/cli/info_image.rs @@ -5,7 +5,7 @@ use super::print_image_info; pub async fn image_info(client: ImgurClient, id: String) { // get a image info from imgur let info = client - .image_info(id) + .image_info(&id) .await .expect("send request to imfur api"); diff --git a/src/cli/upload_image.rs b/src/cli/upload_image.rs index 28b999e..99fc70e 100644 --- a/src/cli/upload_image.rs +++ b/src/cli/upload_image.rs @@ -20,7 +20,7 @@ pub async fn upload_image(client: ImgurClient, path: String) { let config = toml::parse(); // upload a image to imgur - let mut i = client.upload_image(path).await.unwrap_or_else(|err| { + let mut i = client.upload_image(&path).await.unwrap_or_else(|err| { notify!(Notification::new() .summary("Error!") .body(&format!("Error: {}", &err.to_string())) diff --git a/src/lib.rs b/src/lib.rs index 03744ce..31d5a02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -//! This crate is an unofficial implementation of the [Imgur](https://imgur.com) API in Rust. +//! This crate is an unofficial implementation of the [Imgur API](https://imgur.com) in Rust. //! //! # Installation //! @@ -17,35 +17,35 @@ //! # Example Usage //! //! ## Create new ImgurClient -//! ``` +//! ```ignore //! use imgurs::ImgurClient; //! //! let client = ImgurClient::new("client id"); //! ``` //! //! ## Image Upload -//! ``` +//! ```ignore //! // From URL -//! let info = client.upload_image("https://cdn.magicuser.cf/lFaGr1x.png").await?; +//! let info = client.upload_image("https://i.imgur.com/lFaGr1x.png").await?; //! //! // From File //! let info = client.upload_image("path/to/image.png").await?; //! ``` //! //! ## Delete Image -//! ``` +//! ```ignore //! client.delete_image("SuPeRsEcReTDeLeTeHaSh").await?; // delete hash //! ``` //! //! ## Get Image Info -//! ``` +//! ```ignore //! let info = client.image_info("lFaGr1x").await?; // image id //! //! println!("{:?}", info); //! ``` //! //! ## Get Client RateLimit -//! ``` +//! ```ignore //! let info = client.rate_limit.await?; //! //! println!("{:?}", info); diff --git a/src/main.rs b/src/main.rs index e1d7026..9ca73ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ async fn main() { let config = config::toml::parse(); // create imgur client - let client = ImgurClient::new(config.imgur.id); + let client = ImgurClient::new(&config.imgur.id); cli::parse(client).await }