sfr_types/response/
files_info.rs

1//! The type that represents a response of `files.info`.
2//!
3//! <https://api.slack.com/methods/files.info>
4
5use crate::generated::files_info::FilesInfoResponse as Generated;
6use crate::Error;
7use serde::Deserialize;
8
9/// The type that represents a response of `files.info`.
10///
11/// <https://api.slack.com/methods/files.info>
12#[derive(Debug, Deserialize)]
13pub struct FilesInfoResponse(Generated);
14
15impl FilesInfoResponse {
16    /// Converts to generated type.
17    pub fn into_inner(self) -> Generated {
18        self.0
19    }
20
21    /// Takes `url_private_download`.
22    pub fn url_private_download(&self) -> Option<&str> {
23        self.0
24            .file
25            .as_ref()
26            .and_then(|x| x.url_private_download.as_deref())
27    }
28}
29
30impl TryFrom<serde_json::Value> for FilesInfoResponse {
31    type Error = Error;
32
33    fn try_from(value: serde_json::Value) -> Result<Self, Self::Error> {
34        serde_json::from_value(value).map_err(Error::DeserializeJson)
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_de_files_info_response() -> anyhow::Result<()> {
44        let testdata = r#"{
45            "ok": true,
46            "file": {
47                "id": "F0S43PZDF",
48                "created": 1531763342,
49                "timestamp": 1531763342,
50                "name": "tedair.gif",
51                "title": "tedair.gif",
52                "mimetype": "image/gif",
53                "filetype": "gif",
54                "pretty_type": "GIF",
55                "user": "U061F7AUR",
56                "editable": false,
57                "size": 137531,
58                "mode": "hosted",
59                "is_external": false,
60                "external_type": "",
61                "is_public": true,
62                "public_url_shared": false,
63                "display_as_bot": false,
64                "username": "",
65                "url_private": "https://.../tedair.gif",
66                "url_private_download": "https://.../tedair.gif",
67                "thumb_64": "https://.../tedair_64.png",
68                "thumb_80": "https://.../tedair_80.png",
69                "thumb_360": "https://.../tedair_360.png",
70                "thumb_360_w": 176,
71                "thumb_360_h": 226,
72                "thumb_160": "https://.../tedair_=_160.png",
73                "thumb_360_gif": "https://.../tedair_360.gif",
74                "image_exif_rotation": 1,
75                "original_w": 176,
76                "original_h": 226,
77                "deanimate_gif": "https://.../tedair_deanimate_gif.png",
78                "pjpeg": "https://.../tedair_pjpeg.jpg",
79                "permalink": "https://.../tedair.gif",
80                "permalink_public": "https://.../...",
81                "comments_count": 0,
82                "is_starred": false,
83                "shares": {
84                    "public": {
85                        "C0T8SE4AU": [
86                            {
87                                "reply_users": [
88                                    "U061F7AUR"
89                                ],
90                                "reply_users_count": 1,
91                                "reply_count": 1,
92                                "ts": "1531763348.000001",
93                                "thread_ts": "1531763273.000015",
94                                "latest_reply": "1531763348.000001",
95                                "channel_name": "file-under",
96                                "team_id": "T061EG9R6"
97                            }
98                        ]
99                    }
100                },
101                "channels": [
102                    "C0T8SE4AU"
103                ],
104                "groups": [],
105                "ims": [],
106                "has_rich_preview": false,
107                "alt_txt": "tedair.gif"
108            },
109            "comments": [],
110            "response_metadata": {
111                "next_cursor": "dGVhbTpDMUg5UkVTR0w="
112            }
113        }"#;
114        let deserialized: FilesInfoResponse = serde_json::from_str(testdata)?;
115
116        assert_eq!(
117            deserialized.url_private_download().unwrap(),
118            "https://.../tedair.gif",
119        );
120
121        Ok(())
122    }
123}