2022-04-03 19:01:58 +00:00
|
|
|
mod delete_image;
|
|
|
|
mod get_image;
|
2022-01-23 12:05:32 +00:00
|
|
|
mod image_type;
|
2022-04-03 19:01:58 +00:00
|
|
|
mod rate_limit;
|
|
|
|
mod upload_image;
|
2022-01-23 12:05:32 +00:00
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
pub mod client;
|
2022-01-22 22:10:11 +00:00
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
pub use client::ImgurClient;
|
|
|
|
pub use delete_image::*;
|
|
|
|
pub use get_image::*;
|
2022-01-23 12:05:32 +00:00
|
|
|
pub use image_type::*;
|
2022-04-03 19:01:58 +00:00
|
|
|
pub use rate_limit::*;
|
|
|
|
pub use upload_image::*;
|
2022-03-02 21:16:44 +00:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2022-04-03 19:11:11 +00:00
|
|
|
use reqwest::{Method, Response};
|
2022-04-03 19:01:58 +00:00
|
|
|
|
|
|
|
// send request to imgur api
|
2022-03-02 21:16:44 +00:00
|
|
|
pub async fn send_api_request(
|
|
|
|
config: &ImgurClient,
|
|
|
|
method: Method,
|
|
|
|
uri: String,
|
|
|
|
form: Option<HashMap<&str, String>>,
|
2022-05-18 18:16:42 +00:00
|
|
|
) -> anyhow::Result<Response> {
|
2022-04-03 19:01:58 +00:00
|
|
|
// get request client
|
2022-03-02 21:16:44 +00:00
|
|
|
let client = &config.client;
|
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
// create request buidler
|
2022-03-02 21:16:44 +00:00
|
|
|
let mut req = client.request(method, uri.as_str());
|
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
// get program version
|
|
|
|
let version: Option<&str> = option_env!("CARGO_PKG_VERSION");
|
|
|
|
let version = version.unwrap_or("unknown");
|
2022-03-02 21:16:44 +00:00
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
// add `Authorization` and `User-Agent` to request
|
2022-03-02 21:16:44 +00:00
|
|
|
req = req
|
|
|
|
.header("Authorization", format!("Client-ID {}", config.client_id))
|
2022-04-03 19:11:11 +00:00
|
|
|
.header("User-Agent", format!("Imgur/{:?}", version));
|
2022-03-02 21:16:44 +00:00
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
// if exists add hashmap to request
|
2022-03-02 21:16:44 +00:00
|
|
|
if form != None {
|
|
|
|
req = req.form(&form.unwrap())
|
|
|
|
}
|
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
// build request
|
2022-03-02 21:16:44 +00:00
|
|
|
let req = req.build()?;
|
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
// send request
|
|
|
|
Ok(client.execute(req).await?)
|
2022-03-02 21:16:44 +00:00
|
|
|
}
|