slack_rust/reminders/
add.rs

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