use curl::http;
pub fn check_code<S, F>(
resp: http::Response,
body: String,
on_success: S,
on_failure: F
) -> Result<String, ::EdgeGridError> where
S: Fn(String) -> ::EdgeGridResult,
F: Fn(String) -> ::EdgeGridResult
{
match resp.get_code() {
c @ 200...299 => {
debug!("Successful 2xx: {}", c);
debug!("Content-Length Check: {}", check_content_length(&resp));
debug!("Content-Type Check: {}", check_content_type_json(&resp));
on_success(body)
},
c @ 300...399 => {
debug!("Redirection 3xx: {}", c);
debug!("Content-Length Check: {}", check_content_length(&resp));
debug!(
"Content-Type Check: {}",
check_content_type_problem_json(&resp)
);
on_failure(body)
},
c @ 400...499 => {
debug!("Client Error 4xx: {}", c);
debug!("Content-Length Check: {}", check_content_length(&resp));
debug!(
"Content-Type Check: {}",
check_content_type_problem_json(&resp)
);
on_failure(body)
},
c @ 500...599 => {
debug!("Server Error 5xx: {}", c);
debug!("Content-Length Check: {}", check_content_length(&resp));
debug!(
"Content-Type Check: {}",
check_content_type_problem_json(&resp)
);
on_failure(body)
}
c @ _ => {
Err(::EdgeGridError {
title: format!("Unknown Error: {}", c),
detail: body
})
},
}
}
pub fn check_content_length(resp: &http::Response) -> bool {
let given: usize = match resp.get_header("content-length")[0].parse() {
Ok(g) => g,
Err(_) => return false,
};
given == resp.get_body().len()
}
pub fn check_content_type_json(resp: &http::Response) -> bool {
let ref given: String = resp.get_header("content-type")[0];
given == "application/json"
}
pub fn check_content_type_problem_json(resp: &http::Response) -> bool {
let ref given: String = resp.get_header("content-type")[0];
given == "application/problem+json"
}