chore: small changes

This commit is contained in:
MedzikUser 2022-05-18 19:48:39 +02:00
parent 20f0039d08
commit aabbea4182
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
11 changed files with 88 additions and 29 deletions

View File

@ -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:

5
.gitignore vendored
View File

@ -1,4 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
/debug
/target
# IDE configs
.vscode
.idea

View File

@ -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

View File

@ -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<ImageInfo, Error> {
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<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)
.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<RateLimitInfo, Error> {
/// 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<RateLimitInfo> {
rate_limit(self).await
}
pub async fn image_info(&self, id: String) -> Result<ImageInfo, Error> {
/// 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<ImageInfo> {
get_image(self, id).await
}
}

View File

@ -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}"));

View File

@ -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<ImageInfo, Error> {
pub async fn get_image(client: &ImgurClient, image: &str) -> Result<ImageInfo, Error> {
// get imgur api url
let uri = api_url!(format!("image/{image}"));

View File

@ -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");

View File

@ -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");

View File

@ -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()))

View File

@ -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);

View File

@ -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
}