json_placeholder/
json_placeholder.rs

1use api_forge::ApiRequest;
2use serde::{Deserialize, Serialize};
3
4#[derive(Serialize, Deserialize, Debug, Clone)]
5pub struct GetPosts;
6
7impl ApiRequest<Vec<Post>> for GetPosts {
8    const ENDPOINT: &'static str = "/posts";
9}
10
11#[derive(Serialize, Deserialize, Debug, Clone, Default)]
12pub struct Post {
13    #[serde(rename = "userId")]
14    pub user_id: i32,
15    pub id: i32,
16    pub title: String,
17    pub body: String,
18}
19
20#[tokio::main]
21async fn main() {
22    // Initialize the request.
23    let request = GetPosts;
24
25    // Define the base URL (e.g., JSONPlaceholder API for testing).
26    let base_url = "https://jsonplaceholder.typicode.com";
27
28    // Send the request and await the response.
29    let result = request.send_and_parse(base_url, None, None).await;
30
31    match result {
32        Ok(post) => println!("Successfully fetched post: {:?}", post),
33        Err(e) => eprintln!("Error occurred: {:?}", e),
34    }
35}