From b7111994cd739e017c3e66a3f2e1bcee830d95e6 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 7 May 2022 18:31:10 -0400 Subject: [PATCH] Making API request but failing to decode the results. --- .gitignore | 4 ++++ Cargo.toml | 2 +- src/githubctl/githubctl.rs | 40 +++++++++++++++++++++++++++++++++++++- src/main.rs | 9 ++++++++- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..1db7a09 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ /target +.pat +.username +.org +.repo diff --git a/Cargo.toml b/Cargo.toml index 1b4a57b..51ea8f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/githubctl/githubctl.rs b/src/githubctl/githubctl.rs index cc871b1..3c70578 100644 --- a/src/githubctl/githubctl.rs +++ b/src/githubctl/githubctl.rs @@ -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> { + let http_client = reqwest::Client::new(); + Ok(GithubCtl { username, token, http_client }) + } + + pub async fn get_events( + &mut self, + owner: O, + repo: R, + ) -> Result<(), Box> + where + O: AsRef, + R: AsRef, + { + // /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::>().await?; + // let resp = reqwest::get(url) + // .await? + // .json::>() + // .await?; + println!("{:#?}", body); + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index 8785269..9b726f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { - println!("Hello, world!"); + let mut github = githubctl::GithubCtl::new(USERNAME, TOKEN)?; + github.get_events(ORG, REPO).await?; + println!("done."); Ok(()) }