json_placeholder/
lib.rs

1pub mod posts {
2    const URL: &str = "https://jsonplaceholder.typicode.com";
3
4    #[derive(Debug, serde::Serialize, serde::Deserialize)]
5    pub struct Post {
6        pub userId: i32,
7        pub id: i32,
8        pub title: String,
9        pub body: String,
10    }
11
12    pub async fn get_all_posts() -> Result<Vec<Post>, Box<dyn std::error::Error>> {
13        let posts: Vec<Post> = reqwest::get(URL.to_owned() + "/posts")
14            .await?
15            .json::<Vec<Post>>()
16            .await?;
17        Ok(posts)
18    }
19
20    pub async fn get_post(id: i32) -> Result<Post, Box<dyn std::error::Error>> {
21        let post: Post = reqwest::get(URL.to_owned() + "/posts/" + &id.to_string())
22            .await?
23            .json::<Post>()
24            .await?;
25        Ok(post)
26    }
27}