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
use hyper::method::Method::*;
use client;
use errors::*;

/// A user
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct User {
    pub id: String,
    pub screen_name: String,
    pub email: String,
}

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

    fn user_example() -> User {
        User {
            id: "abcde".to_string(),
            screen_name: "Example Mackerel".to_string(),
            email: "mackerel@example.com".to_string(),
        }
    }

    fn json_example() -> serde_json::Value {
        serde_json::from_str(r##"
            {
                "id": "abcde",
                "screenName": "Example Mackerel",
                "email": "mackerel@example.com"
            }
        "##)
            .unwrap()
    }

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

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

}

#[derive(Deserialize)]
struct ListUsers {
    pub users: Vec<User>,
}

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

    /// Delete the user from the organization.
    ///
    /// See https://mackerel.io/api-docs/entry/users#delete.
    pub fn delete_user(&self, user_name: &str) -> Result<User> {
        self.request(Delete,
                     format!("/api/v0/users/{}", user_name),
                     vec![],
                     client::empty_body(),
                     |user| user)
    }
}