-
Notifications
You must be signed in to change notification settings - Fork 2
/
json.rs
55 lines (49 loc) · 1.38 KB
/
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
46
47
48
49
50
51
52
53
54
55
use anyhow::Result;
use reqwest_scraper::ScraperResponse;
use serde::Deserialize;
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
struct Owner {
login: String,
id: i64,
node_id: String,
avatar_url: String,
gravatar_id: String,
url: String,
html_url: String,
followers_url: String,
following_url: String,
gists_url: String,
starred_url: String,
subscriptions_url: String,
organizations_url: String,
repos_url: String,
events_url: String,
received_events_url: String,
#[serde(alias = "type")]
_type: String,
site_admin: bool,
}
#[tokio::main]
async fn main() {
request().await.expect("request error");
}
pub async fn request() -> Result<()> {
let json = reqwest::Client::new()
.get("https://api.github.com/search/repositories?q=rust")
.header("User-Agent", "Rust Reqwest")
.body("body")
.send()
.await?
.jsonpath()
.await?;
let total_count_str = json.select_as_str("$.total_count")?;
let total_count_int: i32 = json.select_one("$.total_count")?;
let names: Vec<String> = json.select("$.items[*].full_name")?;
let owners: Vec<Owner> = json.select("$.items[*].owner")?;
println!("{}", total_count_str);
println!("{}", total_count_int);
println!("{}", names.join("\t"));
owners.iter().for_each(|o| println!("{:#?}", o));
Ok(())
}