#![cfg_attr(feature = "serde_macros", feature(plugin, custom_derive))]
#![cfg_attr(feature = "serde_macros", plugin(serde_macros))]
extern crate serde;
extern crate serde_json;
extern crate hyper;
#[macro_use]
extern crate log;
pub mod error;
use hyper::Client;
use hyper::header::{Authorization, ContentType, Bearer};
use hyper::mime::{Mime, TopLevel, SubLevel, Attr, Value};
use std::io::Read;
use error::Error;
#[cfg(feature = "serde_macros")]
include!("serde_types.in.rs");
#[cfg(feature = "serde_codegen")]
include!(concat!(env!("OUT_DIR"), "/serde_types.rs"));
const BASE_URL: &'static str = "https://ci.appveyor.com/api/";
const BASE_PROJECTS: &'static str = "projects";
#[allow(dead_code)]
const BASE_BUILDS: &'static str = "builds";
#[allow(dead_code)]
#[derive(Debug,PartialEq)]
pub struct AppVeyor {
token: String,
test_mode: bool,
}
impl AppVeyor {
pub fn new(token: &str) -> AppVeyor {
AppVeyor {
token: token.to_string(),
test_mode: false,
}
}
pub fn enable_test_mode(&mut self) {
self.test_mode = true;
}
pub fn get_projects(&self) -> Result<Vec<Project>, Error> {
let mut buffer: String;
if self.test_mode == true {
buffer = load_file("get_projects.json");
} else {
let client = Client::new();
let url = format!("{}{}", BASE_URL, BASE_PROJECTS);
debug!("url: {}", url);
let mut res = try!(client.get(&url)
.header(Authorization({
Bearer { token: self.token.to_owned() }
}))
.send());
if res.status != hyper::status::StatusCode::Ok {
return Err(Error::BadStatus(res.status));
}
buffer = String::new();
try!(res.read_to_string(&mut buffer));
debug!("buffer: {}", buffer);
}
let result = try!(serde_json::from_str(&buffer));
Ok(result)
}
pub fn add_project(&self,
repository_provider: String,
repository_name: String)
-> Result<Project, Error> {
let client = Client::new();
let url = format!("{}{}", BASE_URL, BASE_PROJECTS);
debug!("url: {}", url);
let project = AddProject {
repository_provider: repository_provider,
repository_name: repository_name,
};
let body_json = try!(serde_json::to_string(&project));
debug!("body_json: {:?}", body_json);
let mut res = try!(client.post(&url)
.header(Authorization({
Bearer { token: self.token.to_owned() }
}))
.header(ContentType(Mime(TopLevel::Application,
SubLevel::Json,
vec![(Attr::Charset, Value::Utf8)])))
.body(&body_json)
.send());
if res.status != hyper::status::StatusCode::Ok {
return Err(Error::BadStatus(res.status)); }
let mut buffer = String::new();
try!(res.read_to_string(&mut buffer));
debug!("buffer: {}", buffer);
let result = try!(serde_json::from_str(&buffer));
Ok(result)
}
pub fn delete_project(&self, account_name: String, project_slug: String) -> Result<(), Error> {
let client = Client::new();
let url = format!("{}{}/{}/{}",
BASE_URL,
BASE_PROJECTS,
account_name,
project_slug);
debug!("url: {}", url);
let res = try!(client.delete(&url)
.header(Authorization({
Bearer { token: self.token.to_owned() }
}))
.send());
debug!("status: {:?}", res.status);
if res.status != hyper::status::StatusCode::NoContent {
return Err(Error::BadStatus(res.status));
}
Ok(())
}
pub fn cancel_build(&self) -> String {
let client = Client::new();
let url = format!("{}{}", BASE_URL, "/post");
debug!("url: {}", url);
let mut res = client.delete(&url).send().unwrap();
if res.status != hyper::status::StatusCode::Ok {
panic!(res.status.to_string());
}
println!("cancel_build response status code: {}", res.status);
let mut buffer = String::new();
res.read_to_string(&mut buffer).expect("no body");
println!("buffer: {}", buffer);
buffer
}
}
fn load_file(file: &str) -> String {
println!("load_file: {}", &file);
use std::path::PathBuf;
let mut path = PathBuf::new();
path.push("tests");
path.push("fixtures");
path.push(&file);
let file_path = path.as_os_str();
use std::fs::File;
use std::io::Read;
let mut file = File::open(&file_path).unwrap();
let mut file_string = String::new();
file.read_to_string(&mut file_string).unwrap();
file_string
}
#[test]
fn should_return_project_list_in_testmode() {
let mut happv = AppVeyor::new("adsasd");
happv.enable_test_mode();
let result = happv.get_projects().unwrap();
assert_eq!(3, result.len());
println!("enumerate list:");
for i in result.into_iter() {
println!("\trepo: {}", i.slug);
}
}