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-13 15:15:06 -04:00
|
|
|
let res = reqwest::get("https://api.thecatapi.com/v1/images/search")
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-05-13 15:38:07 -04:00
|
|
|
if !res.status().is_success() {
|
|
|
|
panic!("Request failed with HTTP {}", res.status());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct CatImage {
|
|
|
|
url: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
let images: Vec<CatImage> = res.json().await.unwrap();
|
|
|
|
let image = images
|
|
|
|
.first()
|
|
|
|
.expect("the cat API should return at least one image");
|
|
|
|
println!("The image is at {}", image.url);
|
2023-05-13 14:54:41 -04:00
|
|
|
}
|