Last active
August 29, 2015 14:24
-
-
Save zeisss/51616d1fa601baa9a88e to your computer and use it in GitHub Desktop.
Reading GiantSwarm Environments with Rust 1.2.0-nightly (9ad0063a1 2015-06-20)
This file contains 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
[package] | |
name = "swarm-rust-example" | |
version = "0.1.0" | |
authors = ["root"] | |
[dependencies] | |
hyper = "0.6.1" |
This file contains 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
#![feature(result_expect)] | |
extern crate rustc_serialize; | |
extern crate hyper; | |
use hyper::client::{Client}; | |
use hyper::header::{Authorization}; | |
use rustc_serialize::json; | |
use std::io::Read; | |
#[derive(RustcDecodable, RustcEncodable)] | |
pub struct Env { | |
name: String, | |
} | |
#[derive(RustcDecodable, RustcEncodable)] | |
pub struct ListEnvData { | |
environments: Vec<Env>, | |
} | |
#[derive(RustcDecodable, RustcEncodable)] | |
pub struct ListEnvResponse { | |
status_code: u32, | |
status_text: String, | |
data: ListEnvData, | |
} | |
fn main() { | |
let token = "<token>"; | |
let org = "giantswarm"; | |
// Init | |
let client = Client::new(); | |
// GET | |
let url = &format!("https://api.giantswarm.io/v1/org/{}/env/", org); | |
let req = client.get(url).header(Authorization(format!("{} {}", "giantswarm", token))); | |
let mut res = req.send().unwrap(); | |
assert_eq!(res.status, hyper::Ok); | |
// Read body | |
let mut body = String::new(); | |
res.read_to_string(&mut body).expect("Request failed!"); | |
println!("{}", body); | |
let decoded: ListEnvResponse = json::decode(&body).unwrap(); | |
println!("Status: {} {}", decoded.status_code, decoded.status_text); | |
for env in decoded.data.environments.iter() { | |
println!("Env: {}", env.name); | |
} | |
} |
Author
zeisss
commented
Jun 30, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment