mirror of https://github.com/MedzikUser/imgurs
chore: small changes
This commit is contained in:
parent
20f0039d08
commit
aabbea4182
|
@ -41,6 +41,11 @@ jobs:
|
||||||
with:
|
with:
|
||||||
command: build
|
command: build
|
||||||
|
|
||||||
|
- name: cargo test
|
||||||
|
uses: actions-rs/cargo@v1
|
||||||
|
with:
|
||||||
|
command: test --all-features
|
||||||
|
|
||||||
- name: cargo clippy
|
- name: cargo clippy
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
with:
|
with:
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
# Generated by Cargo
|
# Generated by Cargo
|
||||||
# will have compiled files and executables
|
# will have compiled files and executables
|
||||||
/debug
|
|
||||||
/target
|
/target
|
||||||
|
|
||||||
|
# IDE configs
|
||||||
|
.vscode
|
||||||
|
.idea
|
||||||
|
|
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
## Screenshots
|
## 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
|
## Shell completions
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ macro_rules! api_url (
|
||||||
|
|
||||||
use std::{fmt, fs, io, path::Path};
|
use std::{fmt, fs, io, path::Path};
|
||||||
|
|
||||||
use anyhow::Error;
|
use anyhow::Result;
|
||||||
pub(crate) use api_url;
|
pub(crate) use api_url;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
|
|
||||||
|
@ -24,24 +24,39 @@ impl fmt::Debug for ImgurClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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();
|
let client = Client::new();
|
||||||
ImgurClient { client_id, client }
|
ImgurClient { client_id, client }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn upload_image(&self, path: String) -> Result<ImageInfo, Error> {
|
/// Upload image to Imgur
|
||||||
let mut image: String = path.clone();
|
/// ```
|
||||||
|
/// 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
|
// check if the specified file exists if not then check if it is a url
|
||||||
if Path::new(&path).exists() {
|
if Path::new(&path).exists() {
|
||||||
let bytes = fs::read(&path)
|
let bytes = fs::read(&path)?;
|
||||||
.map_err(|err| err.to_string())
|
|
||||||
.expect("read file");
|
|
||||||
|
|
||||||
image = base64::encode(bytes)
|
image = base64::encode(bytes)
|
||||||
|
}
|
||||||
// validate adress url
|
// validate adress url
|
||||||
} else if !validator::validate_url(&path) {
|
else if !validator::validate_url(&*path) {
|
||||||
let err = io::Error::new(
|
let err = io::Error::new(
|
||||||
io::ErrorKind::Other,
|
io::ErrorKind::Other,
|
||||||
format!("{path} is not url or file path"),
|
format!("{path} is not url or file path"),
|
||||||
|
@ -53,15 +68,51 @@ impl ImgurClient {
|
||||||
upload_image(self, image).await
|
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
|
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
|
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
|
get_image(self, id).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use reqwest::Method;
|
||||||
|
|
||||||
use super::{client::api_url, send_api_request, ImgurClient};
|
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
|
// get imgur api url
|
||||||
let uri = api_url!(format!("image/{delete_hash}"));
|
let uri = api_url!(format!("image/{delete_hash}"));
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ use reqwest::Method;
|
||||||
|
|
||||||
use super::{client::api_url, send_api_request, ImageInfo, ImgurClient};
|
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
|
// get imgur api url
|
||||||
let uri = api_url!(format!("image/{image}"));
|
let uri = api_url!(format!("image/{image}"));
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ use imgurs::ImgurClient;
|
||||||
pub async fn delete_image(client: ImgurClient, delete_hash: String) {
|
pub async fn delete_image(client: ImgurClient, delete_hash: String) {
|
||||||
// delete image from imgur
|
// delete image from imgur
|
||||||
client
|
client
|
||||||
.delete_image(delete_hash)
|
.delete_image(&delete_hash)
|
||||||
.await
|
.await
|
||||||
.expect("send api request");
|
.expect("send api request");
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ use super::print_image_info;
|
||||||
pub async fn image_info(client: ImgurClient, id: String) {
|
pub async fn image_info(client: ImgurClient, id: String) {
|
||||||
// get a image info from imgur
|
// get a image info from imgur
|
||||||
let info = client
|
let info = client
|
||||||
.image_info(id)
|
.image_info(&id)
|
||||||
.await
|
.await
|
||||||
.expect("send request to imfur api");
|
.expect("send request to imfur api");
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ pub async fn upload_image(client: ImgurClient, path: String) {
|
||||||
let config = toml::parse();
|
let config = toml::parse();
|
||||||
|
|
||||||
// upload a image to imgur
|
// 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()
|
notify!(Notification::new()
|
||||||
.summary("Error!")
|
.summary("Error!")
|
||||||
.body(&format!("Error: {}", &err.to_string()))
|
.body(&format!("Error: {}", &err.to_string()))
|
||||||
|
|
14
src/lib.rs
14
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
|
//! # Installation
|
||||||
//!
|
//!
|
||||||
|
@ -17,35 +17,35 @@
|
||||||
//! # Example Usage
|
//! # Example Usage
|
||||||
//!
|
//!
|
||||||
//! ## Create new ImgurClient
|
//! ## Create new ImgurClient
|
||||||
//! ```
|
//! ```ignore
|
||||||
//! use imgurs::ImgurClient;
|
//! use imgurs::ImgurClient;
|
||||||
//!
|
//!
|
||||||
//! let client = ImgurClient::new("client id");
|
//! let client = ImgurClient::new("client id");
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## Image Upload
|
//! ## Image Upload
|
||||||
//! ```
|
//! ```ignore
|
||||||
//! // From URL
|
//! // 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
|
//! // From File
|
||||||
//! let info = client.upload_image("path/to/image.png").await?;
|
//! let info = client.upload_image("path/to/image.png").await?;
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## Delete Image
|
//! ## Delete Image
|
||||||
//! ```
|
//! ```ignore
|
||||||
//! client.delete_image("SuPeRsEcReTDeLeTeHaSh").await?; // delete hash
|
//! client.delete_image("SuPeRsEcReTDeLeTeHaSh").await?; // delete hash
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## Get Image Info
|
//! ## Get Image Info
|
||||||
//! ```
|
//! ```ignore
|
||||||
//! let info = client.image_info("lFaGr1x").await?; // image id
|
//! let info = client.image_info("lFaGr1x").await?; // image id
|
||||||
//!
|
//!
|
||||||
//! println!("{:?}", info);
|
//! println!("{:?}", info);
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## Get Client RateLimit
|
//! ## Get Client RateLimit
|
||||||
//! ```
|
//! ```ignore
|
||||||
//! let info = client.rate_limit.await?;
|
//! let info = client.rate_limit.await?;
|
||||||
//!
|
//!
|
||||||
//! println!("{:?}", info);
|
//! println!("{:?}", info);
|
||||||
|
|
|
@ -13,7 +13,7 @@ async fn main() {
|
||||||
let config = config::toml::parse();
|
let config = config::toml::parse();
|
||||||
|
|
||||||
// create imgur client
|
// create imgur client
|
||||||
let client = ImgurClient::new(config.imgur.id);
|
let client = ImgurClient::new(&config.imgur.id);
|
||||||
|
|
||||||
cli::parse(client).await
|
cli::parse(client).await
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue