-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathclient_json.rs
45 lines (35 loc) · 1.08 KB
/
client_json.rs
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
#![deny(warnings)]
#![warn(rust_2018_idioms)]
use hyper::body::Buf;
use hyper::Client;
use serde::Deserialize;
// A simple type alias so as to DRY.
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
#[tokio::main]
async fn main() -> Result<()> {
let url = "http://jsonplaceholder.typicode.com/users".parse().unwrap();
let users = fetch_json(url).await?;
// print users
println!("users: {:#?}", users);
// print the sum of ids
let sum = users.iter().fold(0, |acc, user| acc + user.id);
println!("sum of ids: {}", sum);
Ok(())
}
async fn fetch_json(url: hyper::Uri) -> Result<Vec<User>> {
let client = Client::new();
// Fetch the url...
let res = client.get(url).await?;
// asynchronously aggregate the chunks of the body
#[allow(deprecated)]
let body = hyper::body::aggregate(res).await?;
// try to parse as json with serde_json
let users = serde_json::from_reader(body.reader())?;
Ok(users)
}
#[derive(Deserialize, Debug)]
struct User {
id: i32,
#[allow(unused)]
name: String,
}