2022-04-03 19:01:58 +00:00
|
|
|
use std::io;
|
2022-01-26 18:55:08 +00:00
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
use anyhow::Error;
|
2022-01-25 18:47:08 +00:00
|
|
|
use reqwest::Method;
|
2022-01-23 12:05:32 +00:00
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
use super::{client::api_url, send_api_request, ImageInfo, ImgurClient};
|
|
|
|
|
2022-05-18 17:48:39 +00:00
|
|
|
pub async fn get_image(client: &ImgurClient, image: &str) -> Result<ImageInfo, Error> {
|
2022-04-03 19:01:58 +00:00
|
|
|
// get imgur api url
|
2022-01-25 18:47:08 +00:00
|
|
|
let uri = api_url!(format!("image/{image}"));
|
2022-01-23 12:05:32 +00:00
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
// send request to imgur api
|
|
|
|
let res = send_api_request(client, Method::GET, uri, None).await?;
|
|
|
|
|
|
|
|
// get response http code
|
2022-01-23 12:05:32 +00:00
|
|
|
let status = res.status();
|
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
// check if an error has occurred
|
2022-01-23 12:05:32 +00:00
|
|
|
if status.is_client_error() || status.is_server_error() {
|
2022-04-03 19:01:58 +00:00
|
|
|
let err = io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
2022-01-26 19:57:51 +00:00
|
|
|
format!("server returned non-successful status code = {status}"),
|
|
|
|
);
|
|
|
|
|
2022-04-03 19:11:11 +00:00
|
|
|
Err(err.into())
|
2022-01-23 12:05:32 +00:00
|
|
|
} else {
|
2022-04-03 19:01:58 +00:00
|
|
|
let content: ImageInfo = res.json().await?;
|
2022-01-23 12:05:32 +00:00
|
|
|
Ok(content)
|
|
|
|
}
|
|
|
|
}
|