Making API request but failing to decode the results.

This commit is contained in:
Tom Alexander 2022-05-07 18:31:10 -04:00
parent 857c237319
commit b7111994cd
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 52 additions and 3 deletions

4
.gitignore vendored
View File

@ -1 +1,5 @@
/target
.pat
.username
.org
.repo

View File

@ -4,6 +4,6 @@ version = "0.1.0"
edition = "2021"
[dependencies]
reqwest = "0.11.10"
reqwest = { version = "0.11.10", features = ["json"] }
sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "sqlite", "migrate" ] }
tokio = { version = "1.16.1", features = ["full"] }

View File

@ -1 +1,39 @@
pub struct GithubCtl {}
use std::collections::HashMap;
pub struct GithubCtl<'a> {
username: &'a str,
token: &'a str,
http_client: reqwest::Client,
}
impl<'a> GithubCtl<'a> {
pub fn new(username: &'a str, token: &'a str) -> Result<Self, Box<dyn std::error::Error>> {
let http_client = reqwest::Client::new();
Ok(GithubCtl { username, token, http_client })
}
pub async fn get_events<O, R>(
&mut self,
owner: O,
repo: R,
) -> Result<(), Box<dyn std::error::Error>>
where
O: AsRef<str>,
R: AsRef<str>,
{
// /repos/{owner}/{repo}/events
// -H "Accept: application/vnd.github.v3+json"
// https://api.github.com/orgs/ORG/events
// -u username:token
// https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
let url = format!("https://api.github.com/repos/{owner}/{repo}/events", owner=owner.as_ref(), repo=repo.as_ref());
let resp = self.http_client.get(url).basic_auth(self.username, Some(self.token)).send().await?;
// let body = resp.json::<HashMap<String, String>>().await?;
// let resp = reqwest::get(url)
// .await?
// .json::<HashMap<String, String>>()
// .await?;
println!("{:#?}", body);
Ok(())
}
}

View File

@ -1,7 +1,14 @@
mod githubctl;
const USERNAME: &'static str = include_str!("../.username");
const TOKEN: &'static str = include_str!("../.pat");
const ORG: &'static str = include_str!("../.org");
const REPO: &'static str = include_str!("../.repo");
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Hello, world!");
let mut github = githubctl::GithubCtl::new(USERNAME, TOKEN)?;
github.get_events(ORG, REPO).await?;
println!("done.");
Ok(())
}