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
#![feature(async_closure, map_into_keys_values, bool_to_option)] use reqwest::{header::*, Client, ClientBuilder}; static mut TOKEN: Option<String> = None; pub fn gh_client(token: Option<&str>) -> ClientBuilder { let mut default_headers = HeaderMap::new(); if let Some(token) = token.or(get_token()) { default_headers.insert( AUTHORIZATION, HeaderValue::from_str(&format!("Bearer {}", token)).unwrap(), ); } default_headers.insert( "Accept", HeaderValue::from_static("application/vnd.github.v3+json"), ); default_headers.insert(USER_AGENT, HeaderValue::from_str("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36").unwrap()); Client::builder().default_headers(default_headers) } pub fn get_token() -> Option<&'static str> { unsafe { TOKEN.as_ref().map(|token| &token[..]) } } pub fn set_token<T: ToString>(token: T) { unsafe { TOKEN = Some(token.to_string()) }; } #[macro_export] macro_rules! gh_api_get { ($fmt:literal$(, $($arg:tt)*)?) => { $crate::gh_api_get!($crate::gh_client(None).build().unwrap(), $fmt$(, $($arg)*)?) }; ($client:expr, $fmt:literal$(, $($arg:tt)*)?) => { $crate::gh_get!($client, "https://api.github.com/{}", format!($fmt$(, $($arg)*)?)) }; } #[macro_export] macro_rules! gh_get { ($fmt:literal$(, $($arg:tt)*)?) => { $crate::gh_get!($crate::gh_client(None).build().unwrap(), $fmt$(, $($arg)*)?) }; ($client:expr, $fmt:literal$(, $($arg:tt)*)?) => { $client.get(&format!($fmt$(, $($arg)*)?)) }; }