wx_rust_common/util/http/
media_upload_request_executor.rs1use async_trait::async_trait;
8use reqwest::multipart::{Form, Part};
9
10use crate::bean::CommonUploadParam;
11use crate::enums::WxType;
12use crate::error::WxErrorException;
13use crate::util::http::RequestExecutor;
14use crate::util::http::simple_get_request_executor::SimpleGetRequestExecutor;
15
16#[derive(Debug, Clone)]
20pub struct MediaUploadRequestExecutor {
21 client: reqwest::Client,
23}
24
25impl MediaUploadRequestExecutor {
26 pub fn new(client: reqwest::Client) -> Self {
28 Self { client }
29 }
30
31 pub async fn upload(
38 &self,
39 uri: &str,
40 param: CommonUploadParam,
41 wx_type: WxType,
42 ) -> Result<String, WxErrorException> {
43 let mut form = Form::new();
44 let file_name = param
45 .data
46 .file_name
47 .clone()
48 .unwrap_or_else(|| "file".to_string());
49 let part = Part::bytes(param.data.content).file_name(file_name);
50 form = form.part(param.name, part);
51 if let Some(fields) = param.form_fields {
52 for (k, v) in fields {
53 form = form.text(k, v);
54 }
55 }
56 let resp = self.client.post(uri).multipart(form).send().await?;
57 let body = resp.text().await?;
58 SimpleGetRequestExecutor::handle_response(wx_type, &body)
59 }
60}
61
62#[async_trait]
63impl RequestExecutor<String, CommonUploadParam> for MediaUploadRequestExecutor {
64 async fn execute(
65 &self,
66 uri: &str,
67 data: CommonUploadParam,
68 wx_type: WxType,
69 ) -> Result<String, WxErrorException> {
70 self.upload(uri, data, wx_type).await
71 }
72}