Skip to main content

wx_rust_common/util/http/
simple_post_request_executor.rs

1//! 简单 POST 请求执行器。
2//!
3//! 对应 Java `me.chanjar.weixin.common.util.http.SimplePostRequestExecutor`。
4//! Java 提供 apache/okhttp/jodd/httpcomponents 四后端;Rust 以 reqwest 统一实现。
5
6use async_trait::async_trait;
7
8use crate::enums::WxType;
9use crate::error::WxErrorException;
10use crate::util::http::simple_get_request_executor::SimpleGetRequestExecutor;
11use crate::util::http::{RequestExecutor, ResponseHandler};
12
13/// 简单的 POST 请求执行器。
14///
15/// 请求参数是 `String`(JSON 或 form 内容),返回结果也是 `String`。
16#[derive(Debug, Clone)]
17pub struct SimplePostRequestExecutor {
18    /// reqwest 客户端
19    client: reqwest::Client,
20}
21
22impl SimplePostRequestExecutor {
23    /// 构建 POST 执行器。
24    ///
25    /// # 参数
26    /// - `client`:reqwest 客户端
27    pub fn new(client: reqwest::Client) -> Self {
28        Self { client }
29    }
30}
31
32#[async_trait]
33impl RequestExecutor<String, String> for SimplePostRequestExecutor {
34    async fn execute(
35        &self,
36        uri: &str,
37        data: String,
38        wx_type: WxType,
39    ) -> Result<String, WxErrorException> {
40        let resp = self.client.post(uri).body(data).send().await?;
41        let body = resp.text().await?;
42        SimpleGetRequestExecutor::handle_response(wx_type, &body)
43    }
44}
45
46#[async_trait]
47impl ResponseHandler<String> for SimplePostRequestExecutor {
48    async fn handle(&self, response: String) {
49        let _ = response;
50    }
51}