use curl;
use auth::EdgeGridAuth;
use auth::RequestVerb::*;
use rustc_serialize::json;
use std::borrow::Borrow;
#[derive(RustcDecodable, RustcEncodable)]
pub struct PurgePostData {
objects: Vec<String>
}
#[derive(Debug, RustcDecodable)]
#[allow(non_snake_case)]
pub struct PurgeResponse {
estimatedSeconds: usize,
progressUri: String,
purgeId: String,
supportId: String,
httpStatus: usize,
detail: String,
pingAfterSeconds: usize,
}
#[derive(Debug, RustcDecodable)]
#[allow(non_snake_case)]
pub struct QueueLengthResponse {
httpStatus: usize,
queueLength: usize,
detail: String,
supportId: String,
}
impl QueueLengthResponse {
pub fn http_status(&self) -> usize {
self.httpStatus
}
}
#[derive(Debug, RustcDecodable)]
#[allow(non_snake_case)]
pub struct PurgeStatusResponse {
originalEstimatedSeconds: Option<usize>,
progressUri: Option<String>,
originalQueueLength: Option<usize>,
purgeId: Option<String>,
supportId: Option<String>,
httpStatus: Option<usize>,
completionTime: Option<String>,
submittedBy: Option<String>,
purgeStatus: Option<String>,
submissionTime: Option<String>,
pingAfterSeconds: Option<usize>,
}
pub fn purge(
egr: &EdgeGridAuth,
qname: &str,
urls: &Vec<String>
) -> Result<PurgeResponse, ::ApiError> {
let mut url = String::from(egr.baseurl());
let mut relurl = String::from("/ccu/v2/queues/");
relurl.push_str(qname);
url.push_str(&relurl[..]);
let objects = PurgePostData { objects: urls.clone() };
let post_json = try!(json::encode(&objects));
let header = try!(egr.auth_header(POST, &relurl[..], Some(&post_json[..])));
debug!("URL: {}", url);
let resp = try!(curl::http::handle()
.timeout(egr.timeout())
.post(&url[..], post_json.as_bytes())
.header("Content-Type", "application/json")
.header("Authorization", &header[..])
.exec()
);
let body = String::from_utf8_lossy(resp.get_body());
debug!("Status: {}", resp.get_code());
debug!("Body: {}", body);
match resp.get_code() {
201 => {
let jsonresp: PurgeResponse = try!(json::decode(&body));
Ok(jsonresp)
},
_ => {
let json_err: ::JSONError = try!(json::decode(&body));
Err(::ApiError{
json_err: json_err,
body: String::from(body.borrow())
})
}
}
}
pub fn queuelen(
egr: &EdgeGridAuth,
qname: &str
) -> Result<QueueLengthResponse, ::ApiError> {
let mut url = String::from(egr.baseurl());
let mut relurl = String::from("/ccu/v2/queues/");
relurl.push_str(qname);
url.push_str(&relurl[..]);
let header = try!(egr.auth_header(GET, &relurl[..], None));
debug!("URL: {}", url);
let resp = try!(curl::http::handle()
.timeout(egr.timeout())
.get(&url[..])
.header("Authorization", &header[..])
.exec()
);
let body = String::from_utf8_lossy(resp.get_body());
debug!("Status: {}", resp.get_code());
debug!("Body: {}", body);
match resp.get_code() {
200 => {
let jsonresp: QueueLengthResponse = try!(json::decode(&body));
Ok(jsonresp)
},
_ => {
let json_err: ::JSONError = try!(json::decode(&body));
Err(::ApiError{
json_err: json_err,
body: String::from(body.borrow())
})
}
}
}
pub fn purgestatus(
egr: &EdgeGridAuth,
purgeid: &str
) -> Result<PurgeStatusResponse, ::ApiError> {
let mut url = String::from(egr.baseurl());
let mut relurl = String::from("/ccu/v2/purges/");
relurl.push_str(purgeid);
url.push_str(&relurl[..]);
let header = try!(egr.auth_header(GET, &relurl[..], None));
debug!("URL: {}", url);
let resp = try!(curl::http::handle()
.timeout(egr.timeout())
.get(&url[..])
.header("Authorization", &header[..])
.exec()
);
let body = String::from_utf8_lossy(resp.get_body());
debug!("Status: {}", resp.get_code());
debug!("Body: {}", body);
match resp.get_code() {
200 => {
let jsonresp: PurgeStatusResponse = try!(json::decode(&body));
Ok(jsonresp)
},
_ => {
let json_err: ::JSONError = try!(json::decode(&body));
Err(::ApiError{
json_err: json_err,
body: String::from(body.borrow())
})
}
}
}