Skip to content

Instantly share code, notes, and snippets.

@fin-ger
Created April 4, 2020 08:00
Show Gist options
  • Save fin-ger/5441286805b70f3376c29aa4583ea53c to your computer and use it in GitHub Desktop.
Save fin-ger/5441286805b70f3376c29aa4583ea53c to your computer and use it in GitHub Desktop.
Short example on how to access the github API in rust. Usage: `cargo run <GITHUB_TOKEN>`
[package]
name = "github-api-example"
version = "0.1.0"
authors = ["Fin Christensen <christensen.fin@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-std = { version = "1.5.0", features = ["attributes"] }
surf = "1.0.3"
serde_json = "1.0.48"
const API_URL: &str = "https://api.github.com/repos/deinstapel/cursive-aligned-view/git/commits/8e33b22ea9147fcb04f5532179858d155a7dc931";
#[async_std::main]
async fn main() {
let github_token = std::env::args().nth(1)
.expect("First command line argument should be GitHub Token");
let commit = surf::get(API_URL)
.set_header("Authorization", format!("token {}", github_token))
.recv_json::<serde_json::Value>()
.await
.expect("Commit not found");
println!("{:#?}", commit);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment