use beckon::beckon;
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Deserialize, Debug)]
#[allow(dead_code)]
pub struct User {
id: u32,
name: String,
}
#[derive(Serialize)]
pub struct UserPath {
id: u32,
}
beckon!(
ApiClient,
{
{
path: "/users/{id}",
method: GET,
path_params: UserPath,
res: User,
},
}
);
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ApiClient::new(
reqwest::Url::parse("https://api.example.com")?,
Duration::from_secs(5),
);
match client.get_users_by_id(&UserPath { id: 999 }).await {
Ok(user) => println!("got user: {user:?}"),
Err(ApiClientError::Http {
status,
reason,
body,
}) => {
eprintln!("request failed: HTTP {status} {reason}");
eprintln!("server said: {body}"); }
Err(other) => eprintln!("transport or decode error: {other}"),
}
Ok(())
}