slack_rust/reminders/
list.rs

1use crate::error::Error;
2use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
3use crate::reminders::reminder::Reminder;
4use serde::{Deserialize, Serialize};
5use serde_with::skip_serializing_none;
6
7#[skip_serializing_none]
8#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
9pub struct ListRequest {
10    pub team_id: Option<String>,
11}
12
13#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
14pub struct ListResponse {
15    pub ok: bool,
16    pub error: Option<String>,
17    pub response_metadata: Option<ResponseMetadata>,
18    pub reminders: Option<Vec<Reminder>>,
19}
20
21pub async fn list<T>(
22    client: &T,
23    param: &ListRequest,
24    bot_token: &str,
25) -> Result<ListResponse, Error>
26where
27    T: SlackWebAPIClient,
28{
29    let url = get_slack_url("reminders.list");
30    let json = serde_json::to_string(&param)?;
31
32    client
33        .post_json(&url, &json, bot_token)
34        .await
35        .and_then(|result| {
36            serde_json::from_str::<ListResponse>(&result).map_err(Error::SerdeJsonError)
37        })
38}
39
40#[cfg(test)]
41mod test {
42    use super::*;
43    use crate::http_client::MockSlackWebAPIClient;
44
45    #[test]
46    fn convert_request() {
47        let request = ListRequest {
48            team_id: Some("T1234567890".to_string()),
49        };
50        let json = r##"{
51  "team_id": "T1234567890"
52}"##;
53
54        let j = serde_json::to_string_pretty(&request).unwrap();
55        assert_eq!(json, j);
56
57        let s = serde_json::from_str::<ListRequest>(json).unwrap();
58        assert_eq!(request, s);
59    }
60
61    #[test]
62    fn convert_response() {
63        let response = ListResponse {
64            ok: true,
65            reminders: Some(vec![
66                Reminder {
67                    id: Some("Rm12345678".to_string()),
68                    creator: Some("U18888888".to_string()),
69                    user: Some("U18888888".to_string()),
70                    text: Some("eat a banana".to_string()),
71                    recurring: Some(false),
72                    time: Some(1602288000),
73                    complete_ts: Some(0),
74                },
75                Reminder {
76                    id: Some("Rm12345678".to_string()),
77                    creator: Some("U18888888".to_string()),
78                    user: Some("U18888888".to_string()),
79                    text: Some("eat a banana".to_string()),
80                    recurring: Some(false),
81                    time: Some(1602288000),
82                    complete_ts: Some(0),
83                },
84            ]),
85            ..Default::default()
86        };
87        let json = r##"{
88  "ok": true,
89  "error": null,
90  "response_metadata": null,
91  "reminders": [
92    {
93      "id": "Rm12345678",
94      "creator": "U18888888",
95      "user": "U18888888",
96      "text": "eat a banana",
97      "recurring": false,
98      "time": 1602288000,
99      "complete_ts": 0
100    },
101    {
102      "id": "Rm12345678",
103      "creator": "U18888888",
104      "user": "U18888888",
105      "text": "eat a banana",
106      "recurring": false,
107      "time": 1602288000,
108      "complete_ts": 0
109    }
110  ]
111}"##;
112
113        let j = serde_json::to_string_pretty(&response).unwrap();
114        assert_eq!(json, j);
115
116        let s = serde_json::from_str::<ListResponse>(json).unwrap();
117        assert_eq!(response, s);
118    }
119
120    #[async_std::test]
121    async fn test_list() {
122        let param = ListRequest {
123            team_id: Some("T1234567890".to_string()),
124        };
125        let mut mock = MockSlackWebAPIClient::new();
126        mock.expect_post_json().returning(|_, _, _| {
127            Ok(r##"{
128  "ok": true,
129  "error": null,
130  "response_metadata": null,
131  "reminders": [
132    {
133      "id": "Rm12345678",
134      "creator": "U18888888",
135      "user": "U18888888",
136      "text": "eat a banana",
137      "recurring": false,
138      "time": 1602288000,
139      "complete_ts": 0
140    },
141    {
142      "id": "Rm12345678",
143      "creator": "U18888888",
144      "user": "U18888888",
145      "text": "eat a banana",
146      "recurring": false,
147      "time": 1602288000,
148      "complete_ts": 0
149    }
150  ]
151}"##
152            .to_string())
153        });
154
155        let response = list(&mock, &param, &"test_token".to_string())
156            .await
157            .unwrap();
158        let expect = ListResponse {
159            ok: true,
160            reminders: Some(vec![
161                Reminder {
162                    id: Some("Rm12345678".to_string()),
163                    creator: Some("U18888888".to_string()),
164                    user: Some("U18888888".to_string()),
165                    text: Some("eat a banana".to_string()),
166                    recurring: Some(false),
167                    time: Some(1602288000),
168                    complete_ts: Some(0),
169                },
170                Reminder {
171                    id: Some("Rm12345678".to_string()),
172                    creator: Some("U18888888".to_string()),
173                    user: Some("U18888888".to_string()),
174                    text: Some("eat a banana".to_string()),
175                    recurring: Some(false),
176                    time: Some(1602288000),
177                    complete_ts: Some(0),
178                },
179            ]),
180            ..Default::default()
181        };
182
183        assert_eq!(expect, response);
184    }
185}