2022-01-23 12:05:32 +00:00
|
|
|
use crate::api::configuration::{api_url, ImgurHandle};
|
|
|
|
use crate::api::ImageInfo;
|
|
|
|
|
2022-01-26 18:55:08 +00:00
|
|
|
use super::send_api_request;
|
|
|
|
|
2022-01-25 18:47:08 +00:00
|
|
|
use log::error;
|
|
|
|
use reqwest::Method;
|
|
|
|
use std::process::exit;
|
2022-01-23 12:05:32 +00:00
|
|
|
|
2022-01-25 18:47:08 +00:00
|
|
|
pub async fn get_image(c: ImgurHandle, image: &str) -> Result<ImageInfo, String> {
|
|
|
|
let uri = api_url!(format!("image/{image}"));
|
|
|
|
let res = send_api_request(&c, Method::GET, uri, None)
|
2022-01-23 12:05:32 +00:00
|
|
|
.await
|
2022-01-25 18:47:08 +00:00
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
error!("send api request: {e}");
|
|
|
|
exit(1)
|
|
|
|
});
|
2022-01-23 12:05:32 +00:00
|
|
|
|
|
|
|
let status = res.status();
|
|
|
|
|
|
|
|
if status.is_client_error() || status.is_server_error() {
|
|
|
|
Err(format!(
|
|
|
|
"server returned non-successful status code = {status}."
|
|
|
|
))
|
|
|
|
} else {
|
2022-01-25 18:47:08 +00:00
|
|
|
let content: ImageInfo = res.json().await.map_err(|e| e.to_string())?;
|
2022-01-23 12:05:32 +00:00
|
|
|
Ok(content)
|
|
|
|
}
|
|
|
|
}
|