gewe_http/message/
forward.rs

1use crate::client::GeweHttpClient;
2use gewe_core::{
3    ForwardFileRequest, ForwardFileResponse, ForwardImageRequest, ForwardImageResponse,
4    ForwardMiniAppRequest, ForwardMiniAppResponse, ForwardUrlRequest, ForwardUrlResponse,
5    ForwardVideoRequest, ForwardVideoResponse, GeweError,
6};
7use tracing::instrument;
8
9impl GeweHttpClient {
10    #[instrument(skip(self))]
11    pub async fn forward_image(
12        &self,
13        app_id: &str,
14        to_wxid: &str,
15        xml: &str,
16    ) -> Result<ForwardImageResponse, GeweError> {
17        let body = ForwardImageRequest {
18            app_id,
19            to_wxid,
20            xml,
21        };
22        let env = self
23            .post_api::<_, ForwardImageResponse>("gewe/v2/api/message/forwardImage", &body)
24            .await?;
25        env.data.ok_or(GeweError::MissingData)
26    }
27
28    #[instrument(skip(self))]
29    pub async fn forward_video(
30        &self,
31        app_id: &str,
32        to_wxid: &str,
33        xml: &str,
34    ) -> Result<ForwardVideoResponse, GeweError> {
35        let body = ForwardVideoRequest {
36            app_id,
37            to_wxid,
38            xml,
39        };
40        let env = self
41            .post_api::<_, ForwardVideoResponse>("gewe/v2/api/message/forwardVideo", &body)
42            .await?;
43        env.data.ok_or(GeweError::MissingData)
44    }
45
46    #[instrument(skip(self))]
47    pub async fn forward_file(
48        &self,
49        app_id: &str,
50        to_wxid: &str,
51        xml: &str,
52    ) -> Result<ForwardFileResponse, GeweError> {
53        let body = ForwardFileRequest {
54            app_id,
55            to_wxid,
56            xml,
57        };
58        let env = self
59            .post_api::<_, ForwardFileResponse>("gewe/v2/api/message/forwardFile", &body)
60            .await?;
61        env.data.ok_or(GeweError::MissingData)
62    }
63
64    #[instrument(skip(self))]
65    pub async fn forward_mini_app(
66        &self,
67        app_id: &str,
68        to_wxid: &str,
69        xml: &str,
70        cover_img_url: &str,
71    ) -> Result<ForwardMiniAppResponse, GeweError> {
72        let body = ForwardMiniAppRequest {
73            app_id,
74            to_wxid,
75            xml,
76            cover_img_url,
77        };
78        let env = self
79            .post_api::<_, ForwardMiniAppResponse>("gewe/v2/api/message/forwardMiniApp", &body)
80            .await?;
81        env.data.ok_or(GeweError::MissingData)
82    }
83
84    #[instrument(skip(self))]
85    pub async fn forward_url(
86        &self,
87        app_id: &str,
88        to_wxid: &str,
89        xml: &str,
90    ) -> Result<ForwardUrlResponse, GeweError> {
91        let body = ForwardUrlRequest {
92            app_id,
93            to_wxid,
94            xml,
95        };
96        let env = self
97            .post_api::<_, ForwardUrlResponse>("gewe/v2/api/message/forwardUrl", &body)
98            .await?;
99        env.data.ok_or(GeweError::MissingData)
100    }
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    #[test]
108    fn test_forward_image_request_serialization() {
109        let req = ForwardImageRequest {
110            app_id: "test_app",
111            to_wxid: "recipient_wxid",
112            xml: "<xml>image xml</xml>",
113        };
114        let json = serde_json::to_string(&req).expect("Failed to serialize");
115        assert!(json.contains("appId"));
116        assert!(json.contains("toWxid"));
117        assert!(json.contains("<xml>image xml</xml>"));
118    }
119
120    #[test]
121    fn test_forward_video_request_serialization() {
122        let req = ForwardVideoRequest {
123            app_id: "test_app",
124            to_wxid: "recipient_wxid",
125            xml: "<xml>video xml</xml>",
126        };
127        let json = serde_json::to_string(&req).expect("Failed to serialize");
128        assert!(json.contains("appId"));
129        assert!(json.contains("toWxid"));
130        assert!(json.contains("<xml>video xml</xml>"));
131    }
132
133    #[test]
134    fn test_forward_file_request_serialization() {
135        let req = ForwardFileRequest {
136            app_id: "test_app",
137            to_wxid: "recipient_wxid",
138            xml: "<xml>file xml</xml>",
139        };
140        let json = serde_json::to_string(&req).expect("Failed to serialize");
141        assert!(json.contains("appId"));
142        assert!(json.contains("toWxid"));
143        assert!(json.contains("<xml>file xml</xml>"));
144    }
145
146    #[test]
147    fn test_forward_mini_app_request_serialization() {
148        let req = ForwardMiniAppRequest {
149            app_id: "test_app",
150            to_wxid: "recipient_wxid",
151            xml: "<xml>mini app xml</xml>",
152            cover_img_url: "https://example.com/cover.jpg",
153        };
154        let json = serde_json::to_string(&req).expect("Failed to serialize");
155        assert!(json.contains("appId"));
156        assert!(json.contains("coverImgUrl"));
157        assert!(json.contains("https://example.com/cover.jpg"));
158    }
159
160    #[test]
161    fn test_forward_url_request_serialization() {
162        let req = ForwardUrlRequest {
163            app_id: "test_app",
164            to_wxid: "recipient_wxid",
165            xml: "<xml>url xml</xml>",
166        };
167        let json = serde_json::to_string(&req).expect("Failed to serialize");
168        assert!(json.contains("appId"));
169        assert!(json.contains("toWxid"));
170        assert!(json.contains("<xml>url xml</xml>"));
171    }
172}