2023-05-14 10:34:23 -04:00
|
|
|
use pretty_hex::PrettyHex;
|
2023-05-13 15:38:07 -04:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
2023-05-13 14:54:41 -04:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2023-05-14 10:34:23 -04:00
|
|
|
let image_bytes = get_cat_image_bytes().await.unwrap();
|
|
|
|
// only dump the first 200 bytes so our terminal survives the onslaught. This will panic if the
|
|
|
|
// image has fewer than 200 bytes.
|
|
|
|
println!("{:?}", &image_bytes[..200].hex_dump());
|
2023-05-13 16:24:05 -04:00
|
|
|
}
|
|
|
|
|
2023-05-14 10:34:23 -04:00
|
|
|
async fn get_cat_image_bytes() -> color_eyre::Result<Vec<u8>> {
|
2023-05-13 15:38:07 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct CatImage {
|
|
|
|
url: String,
|
|
|
|
}
|
|
|
|
|
2023-05-14 10:34:23 -04:00
|
|
|
let api_url = "https://api.thecatapi.com/v1/images/search";
|
|
|
|
let client = reqwest::Client::default();
|
|
|
|
|
|
|
|
let image = client
|
|
|
|
.get(api_url)
|
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.error_for_status()?
|
|
|
|
.json::<Vec<CatImage>>()
|
|
|
|
.await?
|
|
|
|
.pop()
|
|
|
|
.ok_or_else(|| color_eyre::eyre::eyre!("The Cat API returned no images"))?;
|
|
|
|
|
|
|
|
Ok(client
|
|
|
|
.get(image.url)
|
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.error_for_status()?
|
|
|
|
.bytes()
|
|
|
|
.await?
|
|
|
|
.to_vec())
|
2023-05-13 14:54:41 -04:00
|
|
|
}
|