#![doc(html_root_url="https://cosmos-io.github.io/rust-cosmos/doc")]
extern crate request;
use std::io::Result;
use std::collections::HashMap;
pub struct Cosmos {
host: String
}
pub struct Response {
pub http_version: String,
pub status_code: u16,
pub status_message: String,
pub headers: HashMap<String, String>,
pub body: 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) -> Result<Response> {
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) -> Result<Response> {
let mut headers: HashMap<String, String> = HashMap::new();
headers.insert("Accept".to_string(), "application/json".to_string());
headers.insert("Content-Type".to_string(), "application/json".to_string());
headers.insert("Connection".to_string(), "close".to_string());
let response = match request::post(url, &mut headers, body.as_bytes()) {
Ok(response) => response,
Err(e) => { return Err(e); }
};
let cosmos_res = Response {
http_version: response.http_version.clone(),
status_code: response.status_code,
status_message: response.status_message.clone(),
headers: response.headers.clone(),
body: response.body.clone()
};
return Ok(cosmos_res);
}
}
#[test]
fn it_works() {
}