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
extern crate curl; use std::io; use std::error::Error; use curl::http; pub struct Cosmos { host: String } impl Cosmos { pub fn new(host: &str) -> Cosmos { let cosmos = Cosmos { host: host.to_string() }; return cosmos; } pub fn post_containers(&self, planet_name: &str, containers: &str) -> io::Result<String> { let url = format!("http://{}/v1/planets/{}/containers", self.host, planet_name); let response = try!(self.post(&url, containers)); return Ok(response); } fn post(&self, url: &str, body: &str) -> io::Result<String> { let res = match http::handle() .post(url, body) .header("Accept", "application/json") .header("Content-Type", "application/json") .follow_redirects(true) .exec() { Ok(res) => { res } Err(e) => { let err = io::Error::new(io::ErrorKind::Other, e.description()); return Err(err); } }; let res_body = std::str::from_utf8(res.get_body()).unwrap(); let code = res.get_code() / 100; match code { 2 => { return Ok(res_body.to_string()); } _ => { let err = io::Error::new(io::ErrorKind::Other, res_body); return Err(err); } } } } #[test] fn it_works() { Cosmos::new("localhost"); }