1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use hyper::method::Method::*;
use client;
use errors::*;

/// A service
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub struct Service {
    pub name: String,
    pub memo: String,
    pub roles: Vec<String>,
}

#[cfg(test)]
mod tests {
    use serde_json;
    use service::*;

    fn service_example() -> Service {
        Service {
            name: "FooService".to_string(),
            memo: "service memo".to_string(),
            roles: vec!["role0".to_string(), "role1".to_string(), "role2".to_string()],
        }
    }

    fn json_example() -> serde_json::Value {
        serde_json::from_str(r##"
            {
                "name": "FooService",
                "memo": "service memo",
                "roles": [
                    "role0",
                    "role1",
                    "role2"
                ]
            }
        "##)
            .unwrap()
    }

    #[test]
    fn serialize_service() {
        assert_eq!(json_example(),
                   serde_json::to_value(&service_example()).unwrap());
    }

    #[test]
    fn deserialize_service() {
        assert_eq!(service_example(),
                   serde_json::from_value(json_example()).unwrap());
    }

}

#[derive(Deserialize)]
struct ListServiceResponse {
    services: Vec<Service>,
}

#[derive(Deserialize)]
struct ListMetricNamesResponse {
    names: Vec<String>,
}

impl client::Client {
    /// Fetches all the services.
    ///
    /// See https://mackerel.io/api-docs/entry/services#list.
    pub fn list_services(&self) -> Result<Vec<Service>> {
        self.request(Get,
                     "/api/v0/services",
                     vec![],
                     client::empty_body(),
                     |res: ListServiceResponse| res.services)
    }

    /// Fetches the names of the service metrics.
    ///
    /// See https://mackerel.io/api-docs/entry/services#metric-names.
    pub fn list_service_metric_names(&self, service_name: &str) -> Result<Vec<String>> {
        self.request(Get,
                     format!("/api/v0/services/{}/metric-names", service_name),
                     vec![],
                     client::empty_body(),
                     |res: ListMetricNamesResponse| res.names)
    }
}