2022-06-12 15:28:25 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use reqwest::{Method, Response};
|
|
|
|
|
|
|
|
use crate::{ImgurClient, Result};
|
|
|
|
|
|
|
|
/// Send request to a Imgur API
|
|
|
|
pub async fn send_api_request(
|
|
|
|
config: &ImgurClient,
|
|
|
|
method: Method,
|
|
|
|
uri: String,
|
|
|
|
form: Option<HashMap<&str, String>>,
|
|
|
|
) -> Result<Response> {
|
|
|
|
// get http client
|
|
|
|
let client = &config.client;
|
|
|
|
|
|
|
|
// create Request buidler
|
|
|
|
let mut req = client.request(method, uri.as_str());
|
|
|
|
|
|
|
|
// add `Authorization` and `User-Agent` to Request
|
|
|
|
req = req
|
|
|
|
.header("Authorization", format!("Client-ID {}", config.client_id))
|
2022-08-24 13:35:51 +00:00
|
|
|
.header(
|
|
|
|
"User-Agent",
|
|
|
|
format!("Imgur/{:?}", env!("CARGO_PKG_VERSION")),
|
|
|
|
);
|
2022-06-12 15:28:25 +00:00
|
|
|
|
|
|
|
// if exists add HashMap to Request
|
|
|
|
if form != None {
|
|
|
|
req = req.form(&form.unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
// build Request
|
|
|
|
let req = req.build()?;
|
|
|
|
|
|
|
|
// send Request
|
|
|
|
Ok(client.execute(req).await?)
|
|
|
|
}
|