|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use reqwest::header; |
| 4 | +use reqwest::Error; |
| 5 | +use serde::Deserialize; |
| 6 | + |
| 7 | +#[derive(Deserialize, Debug)] |
| 8 | +struct User { |
| 9 | + login: String, |
| 10 | + id: u32, |
| 11 | +} |
| 12 | + |
| 13 | + |
| 14 | +// tokio let's us use "async" on our main function |
| 15 | +#[tokio::main] |
| 16 | +async fn main() -> Result<(), Error> { |
| 17 | + let client = reqwest::Client::new(); |
| 18 | + let url= "https://api.github.com/repos/telefonicaid/fiware-orion/stargazers"; |
| 19 | + let request_url = format!( |
| 20 | + "https://api.github.com/repos/{owner}/{repo}/stargazers", |
| 21 | + owner = "rust-lang-nursery", |
| 22 | + repo = "rust-cookbook" |
| 23 | + ); |
| 24 | + |
| 25 | + let mut headers = header::HeaderMap::new(); |
| 26 | + headers.insert(header::AUTHORIZATION, header::HeaderValue::from_static("Bearer <YOUR TOKEN HERE>")); |
| 27 | + headers.insert(header::ACCEPT, header::HeaderValue::from_static("application/vnd.github.v3+json")); |
| 28 | + headers.insert(header::USER_AGENT, header::HeaderValue::from_static("reqwest")); |
| 29 | + |
| 30 | + // Set up the query parameters for the request |
| 31 | + let mut params = HashMap::new(); |
| 32 | + params.insert("per_page", 100); |
| 33 | + params.insert("page", 1); |
| 34 | + |
| 35 | + |
| 36 | + let body = reqwest::get("https://www.rust-lang.org") |
| 37 | + .await? |
| 38 | + .text() |
| 39 | + .await?; |
| 40 | + |
| 41 | + println!("body = {body:?}"); |
| 42 | + |
| 43 | + let response = client |
| 44 | + .get(url) |
| 45 | + .headers(headers) |
| 46 | + .query(¶ms) |
| 47 | + .send() |
| 48 | + .await? |
| 49 | + .text() |
| 50 | + .await; |
| 51 | + |
| 52 | + // Handle the response |
| 53 | + println!("{:#?}", response); |
| 54 | + |
| 55 | + //let users: Vec<User> = response.json().await; |
| 56 | + //println!("{:?}", users); |
| 57 | + Ok(()) |
| 58 | +} |
| 59 | + |
0 commit comments