slack_rust/reminders/
info.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 InfoRequest {
10    pub reminder: String,
11    pub team_id: Option<String>,
12}
13
14#[skip_serializing_none]
15#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
16pub struct InfoResponse {
17    pub ok: bool,
18    pub error: Option<String>,
19    pub response_metadata: Option<ResponseMetadata>,
20    pub reminder: Option<Reminder>,
21}
22
23pub async fn info<T>(
24    client: &T,
25    param: &InfoRequest,
26    bot_token: &str,
27) -> Result<InfoResponse, Error>
28where
29    T: SlackWebAPIClient,
30{
31    let url = get_slack_url("reminders.info");
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::<InfoResponse>(&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 = InfoRequest {
50            reminder: "Rm12345678".to_string(),
51            team_id: Some("T1234567890".to_string()),
52        };
53        let json = r##"{
54  "reminder": "Rm12345678",
55  "team_id": "T1234567890"
56}"##;
57
58        let j = serde_json::to_string_pretty(&request).unwrap();
59        assert_eq!(json, j);
60
61        let s = serde_json::from_str::<InfoRequest>(json).unwrap();
62        assert_eq!(request, s);
63    }
64
65    #[test]
66    fn convert_response() {
67        let response = InfoResponse {
68            ok: true,
69            reminder: Some(Reminder {
70                id: Some("Rm12345678".to_string()),
71                creator: Some("U18888888".to_string()),
72                user: Some("U18888888".to_string()),
73                text: Some("eat a banana".to_string()),
74                recurring: Some(false),
75                time: Some(1602288000),
76                complete_ts: Some(0),
77            }),
78            ..Default::default()
79        };
80        let json = r##"{
81  "ok": true,
82  "reminder": {
83    "id": "Rm12345678",
84    "creator": "U18888888",
85    "user": "U18888888",
86    "text": "eat a banana",
87    "recurring": false,
88    "time": 1602288000,
89    "complete_ts": 0
90  }
91}"##;
92
93        let j = serde_json::to_string_pretty(&response).unwrap();
94        assert_eq!(json, j);
95
96        let s = serde_json::from_str::<InfoResponse>(json).unwrap();
97        assert_eq!(response, s);
98    }
99
100    #[async_std::test]
101    async fn test_info() {
102        let param = InfoRequest {
103            reminder: "Rm12345678".to_string(),
104            team_id: Some("T1234567890".to_string()),
105        };
106        let mut mock = MockSlackWebAPIClient::new();
107        mock.expect_post_json().returning(|_, _, _| {
108            Ok(r##"{
109  "ok": true,
110  "reminder": {
111    "id": "Rm12345678",
112    "creator": "U18888888",
113    "user": "U18888888",
114    "text": "eat a banana",
115    "recurring": false,
116    "time": 1602288000,
117    "complete_ts": 0
118  }
119}"##
120            .to_string())
121        });
122
123        let response = info(&mock, &param, &"test_token".to_string())
124            .await
125            .unwrap();
126        let expect = InfoResponse {
127            ok: true,
128            reminder: Some(Reminder {
129                id: Some("Rm12345678".to_string()),
130                creator: Some("U18888888".to_string()),
131                user: Some("U18888888".to_string()),
132                text: Some("eat a banana".to_string()),
133                recurring: Some(false),
134                time: Some(1602288000),
135                complete_ts: Some(0),
136            }),
137            ..Default::default()
138        };
139
140        assert_eq!(expect, response);
141    }
142}