Skip to main content

wx_rust_common/util/http/
media_download_request_executor.rs

1//! 媒体下载请求执行器。
2//!
3//! 对应 Java `me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor`
4//! (及四后端实现,均为 `PLATFORM_NA`)。Rust 以 reqwest 统一实现。
5
6use async_trait::async_trait;
7
8use crate::enums::WxType;
9use crate::error::WxErrorException;
10use crate::util::http::RequestExecutor;
11
12/// 媒体下载请求执行器。
13///
14/// 下载媒体文件到字节内容(对应 Java 的 `File` 下载;Rust 返回字节由调用方落盘)。
15#[derive(Debug, Clone)]
16pub struct MediaDownloadRequestExecutor {
17    /// reqwest 客户端
18    client: reqwest::Client,
19}
20
21impl MediaDownloadRequestExecutor {
22    /// 构建下载执行器。
23    pub fn new(client: reqwest::Client) -> Self {
24        Self { client }
25    }
26}
27
28#[async_trait]
29impl RequestExecutor<Vec<u8>, String> for MediaDownloadRequestExecutor {
30    async fn execute(
31        &self,
32        uri: &str,
33        _data: String,
34        _wx_type: WxType,
35    ) -> Result<Vec<u8>, WxErrorException> {
36        let resp = self.client.get(uri).send().await?;
37        let bytes = resp.bytes().await?.to_vec();
38        Ok(bytes)
39    }
40}