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 54 55 56 57 58 59 60 61 62 63 64 65 66
extern crate reqwest; extern crate serde; extern crate failure; extern crate serde_json; #[macro_use] extern crate hyper; #[macro_use] extern crate serde_derive; use destiny2::Destiny2; use reqwest::{ Client, header::Authorization }; use serde::{ de::DeserializeOwned }; #[macro_use] mod macros; pub mod destiny2; header! { (XApiKey, "X-API-Key") => [String] } pub struct BungieClient { api_key: String, oauth_token: Option<String> } impl BungieClient { pub fn new(api_key: String) -> BungieClient { BungieClient { api_key, oauth_token: None } } pub fn with_authentication_token(mut self, oauth_token: String) -> BungieClient { self.oauth_token = Some(oauth_token); self } pub fn destiny2(&self) -> Destiny2 { Destiny2 { bungie: &self } } fn send_request<T: DeserializeOwned>(&self, path: &str, body: Option<String>) -> Result<T, failure::Error> { let client = Client::new(); let path = &("https://www.bungie.net/Platform".to_owned() + path); if body.is_some() { let mut request = client.post(path); if let Some(ref oauth_token) = self.oauth_token { request.header(Authorization(oauth_token.clone())); } request.header(XApiKey(self.api_key.clone())); request.body(body.unwrap()); let mut response = request.send()?; Ok(response.json()?) } else { let mut request = client.get(path); if let Some(ref oauth_token) = self.oauth_token { request.header(Authorization(oauth_token.clone())); } request.header(XApiKey(self.api_key.clone())); let mut response = request.send()?; Ok(response.json()?) } } }