Last active
August 29, 2015 14:16
-
-
Save archaelus/76fc23da14f3e7054d07 to your computer and use it in GitHub Desktop.
Rust API Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate hyper; | |
use hyper::Client; | |
use hyper::header::{Header, Accept, Authorization}; | |
use hyper::net::NetworkConnector; | |
use hyper::client::{IntoUrl, RequestBuilder}; | |
struct API { | |
api_host: String, | |
} | |
fn ps(api: API, app_name: &str) -> hyper::status::StatusCode { | |
let mut client = Client::new(); | |
let url = format!("https://{api_host}/path/{app}/things", | |
api_host = api.api_host, | |
app=app_name); | |
let mut req = new_req(&mut client, &*url); | |
return req.send().unwrap().status; | |
} | |
fn new_req<U: IntoUrl, C: NetworkConnector>(client: &mut Client<C>, url: U) -> RequestBuilder<U,C> { | |
return client.get(url).header(accept_hdr()).header(authorization_hdr()); | |
} | |
fn accept_hdr() -> hyper::header::Accept { | |
return Header::parse_header([b"application/vnd.vendor+json; version=3".to_vec()].as_slice()).unwrap(); | |
} | |
fn authorization_hdr() -> hyper::header::Authorization<String> { | |
return Authorization("Secret".to_string()); | |
} | |
#[test] | |
fn herdverb_ps() { | |
let api = API { api_host: "api.example.com".to_string() }; | |
assert_eq!(ps(api, "app"), hyper::Ok); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment