libedgegrid 0.1.0

This library implements an Authentication handler for the Akamai OPEN EdgeGrid Authentication scheme in Rust
use curl::http;
use serde_json;
use std::fmt;

use self::CCUResponseType::*;

pub enum CCUResponseType {
    Purge,
    PurgeStatus,
    QueueLength,
}

#[cfg(feature = "serde_macros")]
include!("resp.rs.in");

#[cfg(not(feature = "serde_macros"))]
include!(concat!(env!("OUT_DIR"), "/ccu/resp.rs"));

impl fmt::Display for CCUError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let detail = match self.detail {
            Some(ref s) => &s[..],
            None        => "",
        };
        write!(f, "{}", detail)
    }
}

fn parse_ccu_error(body: String) -> ::EdgeGridResult {
    debug!("{}", body);
    let ccue: CCUError = try!(serde_json::from_str(&body));
    debug!("{:?}", ccue);
    Err(::EdgeGridError{
        title: match ccue.title {
            Some(t) => t,
            None    => String::new(),
        },
        detail: match ccue.detail {
            Some(d) => d,
            None    => String::new(),
        },
    })
}

pub fn parse_purge(
    resp: http::Response
) -> ::EdgeGridResult  {
    let body_vec = Vec::from(resp.get_body());
    let body = try!(String::from_utf8(body_vec));
    ::response::check_code(
        resp,
        body,
        |b| {
            let pr: PurgeResponse = try!(serde_json::from_str(&b));
            debug!("{:?}", pr);
            Ok(format!("{}: {}", pr.detail, pr.purge_id))
        },
        parse_ccu_error
    )
}

pub fn parse_purge_status(
    resp: http::Response
) -> ::EdgeGridResult  {
    let body_vec = Vec::from(resp.get_body());
    let body = try!(String::from_utf8(body_vec));
    ::response::check_code(
        resp,
        body,
        |b| {
            let psr: PurgeStatusResponse = try!(serde_json::from_str(&b));
            debug!("{:?}", psr);
            let mut out = String::new();

            match psr.purge_status {
                Some(ps) => {
                    out.push_str(&format!("Status: {}", ps)[..]);
                },
                None     => {},
            }

            match psr.submission_time {
                Some(st) => {
                    out.push_str(&format!("\nSubmitted at {}", st)[..]);
                },
                None     => {},
            }

            match psr.completion_time {
                Some(ct) => {
                    out.push_str(&format!("\nCompleted at {}", ct)[..]);
                },
                None     => {},
            }

            Ok(out)
        },
        parse_ccu_error
    )
}

pub fn parse_queue_length(
    resp: http::Response
) -> ::EdgeGridResult  {
    let body_vec = Vec::from(resp.get_body());
    let body = try!(String::from_utf8(body_vec));
    ::response::check_code(
        resp,
        body,
        |b| {
            let qlen: QueueLengthResponse = try!(serde_json::from_str(&b));
            debug!("{:?}", qlen);
            Ok(format!("{:?}", qlen.queue_length))
        },
        parse_ccu_error
    )
}

pub fn parse(
    resp: http::Response,
    ccu_type: CCUResponseType
) -> ::EdgeGridResult {
    match ccu_type {
        Purge       => parse_purge(resp),
        PurgeStatus => parse_purge_status(resp),
        QueueLength => parse_queue_length(resp),
    }
}