vkteams_bot/api/files/
get_info.rs

1#![allow(unused_parens)]
2//! File get info method `files/getInfo`
3//! [More info](https://teams.vk.com/botapi/#/files/get_files_getInfo)
4use crate::api::types::*;
5use crate::bot::net::*;
6use crate::error::{BotError, Result};
7use reqwest::Url;
8bot_api_method! {
9    method = "files/getInfo",
10    request = RequestFilesGetInfo {
11        required {
12            file_id: FileId,
13        },
14        optional {}
15    },
16    response = ResponseFilesGetInfo {
17        #[serde(rename = "type", default)]
18        file_type: String,
19        #[serde(rename = "size", default)]
20        file_size: u32,
21        #[serde(rename = "filename", default)]
22        file_name: String,
23        #[serde(default)]
24        url: String,
25    },
26}
27
28impl ResponseFilesGetInfo {
29    /// Download file data
30    /// ## Parameters
31    /// - `client`: [`reqwest::Client`] - reqwest client
32    pub async fn download(&self, client: reqwest::Client) -> Result<Vec<u8>> {
33        if self.url.is_empty() {
34            return Err(BotError::Validation("URL is empty".to_string()));
35        }
36        get_bytes_response(client, Url::parse(&self.url)?).await
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use reqwest::Client;
44    use tokio;
45
46    #[tokio::test]
47    async fn test_download_empty_url() {
48        let info = ResponseFilesGetInfo {
49            file_type: "".to_string(),
50            file_size: 0,
51            file_name: "".to_string(),
52            url: "".to_string(),
53        };
54        let client = Client::new();
55        let result = info.download(client).await;
56        assert!(matches!(result, Err(BotError::Validation(_))));
57    }
58
59    #[tokio::test]
60    async fn test_download_invalid_url() {
61        let info = ResponseFilesGetInfo {
62            file_type: "".to_string(),
63            file_size: 0,
64            file_name: "".to_string(),
65            url: "not a url".to_string(),
66        };
67        let client = Client::new();
68        let result = info.download(client).await;
69        assert!(matches!(result, Err(BotError::Url(_))));
70    }
71
72    #[tokio::test]
73    async fn test_download_network_error() {
74        let info = ResponseFilesGetInfo {
75            file_type: "".to_string(),
76            file_size: 0,
77            file_name: "".to_string(),
78            url: "http://localhost:0".to_string(),
79        };
80        let client = Client::new();
81        let result = info.download(client).await;
82        assert!(matches!(result, Err(BotError::Network(_))));
83    }
84
85    // Для успешного случая нужен mock, если есть возможность внедрить dependency injection или test server
86    // #[test]
87    // fn test_download_success() {
88    //     let url = "http://localhost:8000/testfile.txt";
89    //     let result = download_file(url);
90    //     assert!(result.is_ok());
91    // }
92}