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
88
89
90
91
92
93
94
use client;
use errors::*;
use reqwest::Method;

/// A channel
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub struct Channel {
    pub id: String,
    pub name: String,
    #[serde(rename = "type")]
    pub channel_type: String,
}

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

    fn channel_example1() -> Channel {
        Channel {
            id: "abcde1".to_string(),
            name: "Example Channel 1".to_string(),
            channel_type: "slack".to_string(),
        }
    }

    fn json_example1() -> serde_json::Value {
        json!({
            "id": "abcde1",
            "name": "Example Channel 1",
            "type": "slack"
        })
    }

    fn channel_example2() -> Channel {
        Channel {
            id: "abcde2".to_string(),
            name: "Example Channel 2".to_string(),
            channel_type: "email".to_string(),
        }
    }

    fn json_example2() -> serde_json::Value {
        json!({
            "id": "abcde2",
            "name": "Example Channel 2",
            "type": "email"
        })
    }

    #[test]
    fn serialize_channel() {
        assert_eq!(
            json_example1(),
            serde_json::to_value(&channel_example1()).unwrap()
        );
        assert_eq!(
            json_example2(),
            serde_json::to_value(&channel_example2()).unwrap()
        );
    }

    #[test]
    fn deserialize_channel() {
        assert_eq!(
            channel_example1(),
            serde_json::from_value(json_example1()).unwrap()
        );
        assert_eq!(
            channel_example2(),
            serde_json::from_value(json_example2()).unwrap()
        );
    }
}

#[derive(Deserialize)]
struct ListChannelResponse {
    channels: Vec<Channel>,
}

impl client::Client {
    /// Fetches all the channels.
    ///
    /// See https://mackerel.io/api-docs/entry/channels#get.
    pub fn list_channels(&self) -> Result<Vec<Channel>> {
        self.request(
            Method::GET,
            "/api/v0/channels",
            vec![],
            client::empty_body(),
            |res: ListChannelResponse| res.channels,
        )
    }
}