-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: move reqwest to send_api_request function
- Loading branch information
Showing
9 changed files
with
93 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use reqwest::Method; | ||
|
||
use super::ImgurHandle; | ||
use std::collections::HashMap; | ||
|
||
pub async fn send_api_request( | ||
config: &ImgurHandle, | ||
method: Method, | ||
uri: String, | ||
form: Option<HashMap<&str, String>>, | ||
) -> Result<reqwest::Response, reqwest::Error> { | ||
let client = &config.client; | ||
|
||
let mut req = client.request(method, uri.as_str()); | ||
|
||
const VERSION: Option<&str> = option_env!("CARGO_PKG_VERSION"); | ||
|
||
req = req | ||
.header("Authorization", format!("Client-ID {}", config.client_id)) | ||
.header( | ||
"User-Agent", | ||
format!("Imgur/{:?}", VERSION.unwrap_or("unknown")), | ||
); | ||
|
||
if form != None { | ||
req = req.form(&form.unwrap()) | ||
} | ||
|
||
let req = req.build()?; | ||
|
||
client.execute(req).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,37 @@ | ||
use crate::api::configuration::{api_url, ImgurHandle}; | ||
use crate::api::ImageInfo; | ||
use super::send_api_request; | ||
use crate::api::{ | ||
configuration::{api_url, ImgurHandle}, | ||
ImageInfo, | ||
}; | ||
|
||
use std::collections::HashMap; | ||
use log::error; | ||
use reqwest::Method; | ||
use std::{collections::HashMap, process::exit}; | ||
|
||
pub async fn upload_image(c: ImgurHandle, image: &str) -> Result<ImageInfo, String> { | ||
const VERSION: Option<&str> = option_env!("CARGO_PKG_VERSION"); | ||
|
||
let mut form = HashMap::new(); | ||
|
||
form.insert("image", image.to_string()); | ||
|
||
let res = c | ||
.client | ||
.post(api_url!("image")) | ||
.header("Authorization", format!("Client-ID {}", c.client_id)) | ||
.header( | ||
"User-Agent", | ||
format!("Imgurs/{:?}", VERSION.unwrap_or("unknown")), | ||
) | ||
.form(&form) | ||
.send() | ||
let uri = api_url!("image"); | ||
let res = send_api_request(&c, Method::POST, uri, Some(form)) | ||
.await | ||
.map_err(|err| err.to_string())?; | ||
.unwrap_or_else(|e| { | ||
error!("send api request: {e}"); | ||
exit(1) | ||
}); | ||
|
||
let status = res.status(); | ||
|
||
if status.is_client_error() || status.is_server_error() { | ||
let body = res.text().await.map_err(|err| err.to_string())?; | ||
let body = res.text().await.map_err(|e| e.to_string())?; | ||
|
||
Err(format!( | ||
"server returned non-successful status code = {status}. body = {body}" | ||
)) | ||
} else { | ||
let content: ImageInfo = res.json().await.map_err(|err| err.to_string())?; | ||
let content: ImageInfo = res.json().await.map_err(|e| e.to_string())?; | ||
|
||
Ok(content) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters