-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.rs
More file actions
58 lines (50 loc) · 1.35 KB
/
basic.rs
File metadata and controls
58 lines (50 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//! Basic example demonstrating minimal usage of the `api_client` macro.
//!
//! This example shows the simplest way to create an HTTP API client
//! with just the essential fields: `method`, `path`, and `res`.
use http_provider_macro::api_client;
use reqwest::Url;
use serde::Deserialize;
// Define your response types
#[derive(Deserialize, Debug)]
struct User {
id: u32,
name: String,
email: String,
}
#[derive(Deserialize, Debug)]
struct Post {
id: u32,
title: String,
content: String,
}
// Define your API client with minimal configuration
api_client!(
ApiClient,
{
{
path: "/users",
method: GET,
res: Vec<User>,
},
{
path: "/posts",
method: GET,
res: Post,
},
}
);
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let base_url = Url::parse("https://api.example.com")?;
let client = ApiClient::new(base_url, Some(5000));
// Use the auto-generated methods
let users = client.get_users().await?;
println!("Found {} users", users.len());
if let Some(user) = users.first() {
println!("First user: #{} - {} ({})", user.id, user.name, user.email);
}
let post = client.get_posts().await?;
println!("Post #{}: {} - {}", post.id, post.title, post.content);
Ok(())
}