Skip to main content

runbot/onebot11_api/
download_file_to_cache.rs

1use crate::error::Result;
2use crate::prelude::BotContext;
3use serde_derive::{Deserialize, Serialize};
4use serde_json::json;
5use tokio::time::Duration;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct DownloadedFile {
9    pub file: String,
10}
11
12impl BotContext {
13    pub async fn download_file_to_cache_with_timeout(
14        &self,
15        url: &str,
16        thread_count: i32,
17        headers: Option<serde_json::Value>,
18        timeout: Duration,
19    ) -> Result<DownloadedFile> {
20        let mut params = json!({
21            "url": url,
22            "thread_count": thread_count,
23        });
24        if let Some(h) = headers {
25            params["headers"] = h;
26        }
27        let response = self
28            .websocket_send("download_file_to_cache", params)
29            .await?;
30        let data = response.data(timeout).await?;
31        let file: DownloadedFile = serde_json::from_value(data)?;
32        Ok(file)
33    }
34
35    pub async fn download_file_to_cache(
36        &self,
37        url: &str,
38        thread_count: i32,
39        headers: Option<serde_json::Value>,
40    ) -> Result<DownloadedFile> {
41        self.download_file_to_cache_with_timeout(url, thread_count, headers, Duration::from_secs(30))
42            .await
43    }
44}
45