2022-01-27 13:14:46 +00:00
|
|
|
use crate::api::configuration::{api_url, ImgurClient};
|
2022-01-23 12:05:32 +00:00
|
|
|
use crate::api::ImageInfo;
|
|
|
|
|
2022-01-26 18:55:08 +00:00
|
|
|
use super::send_api_request;
|
|
|
|
|
2022-01-26 19:57:51 +00:00
|
|
|
use anyhow::Error as anyhow_err;
|
2022-01-25 18:47:08 +00:00
|
|
|
use reqwest::Method;
|
2022-01-26 19:57:51 +00:00
|
|
|
use std::io::{Error, ErrorKind};
|
2022-01-23 12:05:32 +00:00
|
|
|
|
2022-01-27 13:14:46 +00:00
|
|
|
pub async fn get_image(c: ImgurClient, image: &str) -> Result<ImageInfo, anyhow_err> {
|
2022-01-25 18:47:08 +00:00
|
|
|
let uri = api_url!(format!("image/{image}"));
|
2022-01-26 19:57:51 +00:00
|
|
|
let res = send_api_request(&c, Method::GET, uri, None).await?;
|
2022-01-23 12:05:32 +00:00
|
|
|
|
|
|
|
let status = res.status();
|
|
|
|
|
|
|
|
if status.is_client_error() || status.is_server_error() {
|
2022-01-26 19:57:51 +00:00
|
|
|
let err = Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
format!("server returned non-successful status code = {status}"),
|
|
|
|
);
|
|
|
|
|
|
|
|
Err(anyhow_err::from(err))
|
2022-01-23 12:05:32 +00:00
|
|
|
} else {
|
2022-01-26 19:57:51 +00:00
|
|
|
let content: ImageInfo = res.json().await.map_err(anyhow_err::new)?;
|
2022-01-23 12:05:32 +00:00
|
|
|
Ok(content)
|
|
|
|
}
|
|
|
|
}
|