Skip to content

Instantly share code, notes, and snippets.

@zeisss
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeisss/51616d1fa601baa9a88e to your computer and use it in GitHub Desktop.
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)
[package]
name = "swarm-rust-example"
version = "0.1.0"
authors = ["root"]
[dependencies]
hyper = "0.6.1"
#![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);
}
}
@zeisss
Copy link
Author

zeisss commented Jun 30, 2015

# cargo run
   Compiling swarmctl v0.1.0 (file:///source/swarmctl)
     Running `target/debug/swarmctl`
{"status_code":10000,"status_text":"success","data":{"environments":[{"name":"cramtests"},{"name":"denderello"},{"name":"dev"},{"name":"prod"},{"name":"production"},{"name":"teemow"},{"name":"testing"}]}}

Status: 10000 success
Env: cramtests
Env: denderello
Env: dev
Env: prod
Env: production
Env: teemow
Env: testing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment