imgurs/src/api/get_image.rs

31 lines
834 B
Rust
Raw Normal View History

use std::io;
use anyhow::Error;
use reqwest::Method;
2022-01-23 12:05:32 +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> {
// get imgur api url
let uri = api_url!(format!("image/{image}"));
2022-01-23 12:05:32 +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();
// check if an error has occurred
2022-01-23 12:05:32 +00:00
if status.is_client_error() || status.is_server_error() {
let err = io::Error::new(
io::ErrorKind::Other,
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 {
let content: ImageInfo = res.json().await?;
2022-01-23 12:05:32 +00:00
Ok(content)
}
}