gewe_http/moments/
publish.rs1use crate::client::GeweHttpClient;
2use gewe_core::{
3 ForwardSnsRequest, GeweError, SendImgSnsRequest, SendSnsResponse, SendTextSnsRequest,
4 SendUrlSnsRequest, SendVideoSnsRequest,
5};
6use tracing::instrument;
7
8impl GeweHttpClient {
9 #[instrument(skip(self))]
10 pub async fn send_text_sns(
11 &self,
12 req: SendTextSnsRequest<'_>,
13 ) -> Result<SendSnsResponse, GeweError> {
14 let env = self
15 .post_api::<_, SendSnsResponse>("gewe/v2/api/sns/sendTextSns", &req)
16 .await?;
17 env.data.ok_or(GeweError::MissingData)
18 }
19
20 #[instrument(skip(self))]
21 pub async fn send_img_sns(
22 &self,
23 req: SendImgSnsRequest<'_>,
24 ) -> Result<SendSnsResponse, GeweError> {
25 let env = self
26 .post_api::<_, SendSnsResponse>("gewe/v2/api/sns/sendImgSns", &req)
27 .await?;
28 env.data.ok_or(GeweError::MissingData)
29 }
30
31 #[instrument(skip(self))]
32 pub async fn send_video_sns(
33 &self,
34 req: SendVideoSnsRequest<'_>,
35 ) -> Result<SendSnsResponse, GeweError> {
36 let env = self
37 .post_api::<_, SendSnsResponse>("gewe/v2/api/sns/sendVideoSns", &req)
38 .await?;
39 env.data.ok_or(GeweError::MissingData)
40 }
41
42 #[instrument(skip(self))]
43 pub async fn send_url_sns(
44 &self,
45 req: SendUrlSnsRequest<'_>,
46 ) -> Result<SendSnsResponse, GeweError> {
47 let env = self
48 .post_api::<_, SendSnsResponse>("gewe/v2/api/sns/sendUrlSns", &req)
49 .await?;
50 env.data.ok_or(GeweError::MissingData)
51 }
52
53 #[instrument(skip(self))]
54 pub async fn forward_sns(
55 &self,
56 req: ForwardSnsRequest<'_>,
57 ) -> Result<SendSnsResponse, GeweError> {
58 let env = self
59 .post_api::<_, SendSnsResponse>("gewe/v2/api/sns/forwardSns", &req)
60 .await?;
61 env.data.ok_or(GeweError::MissingData)
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68 use gewe_core::{SnsAudience, SnsImageInfo, SnsVideoInfo};
69
70 #[test]
71 fn test_send_text_sns_request() {
72 let req = SendTextSnsRequest {
73 app_id: "test_app",
74 audience: SnsAudience::default(),
75 content: "Hello moments!",
76 };
77 let json = serde_json::to_string(&req).expect("Failed to serialize");
78 assert!(json.contains("appId"));
79 assert!(json.contains("Hello moments!"));
80 }
81
82 #[test]
83 fn test_send_img_sns_request() {
84 let req = SendImgSnsRequest {
85 app_id: "test_app",
86 audience: SnsAudience::default(),
87 img_infos: vec![SnsImageInfo {
88 file_url: "https://example.com/image.jpg".to_string(),
89 thumb_url: "https://example.com/thumb.jpg".to_string(),
90 file_md5: "abc123".to_string(),
91 length: Some(1024),
92 width: 800,
93 height: 600,
94 }],
95 content: Some("Image caption"),
96 };
97 let json = serde_json::to_string(&req).expect("Failed to serialize");
98 assert!(json.contains("appId"));
99 assert!(json.contains("imgInfos"));
100 }
101
102 #[test]
103 fn test_send_video_sns_request() {
104 let req = SendVideoSnsRequest {
105 app_id: "test_app",
106 audience: SnsAudience::default(),
107 content: Some("Video caption"),
108 video_info: SnsVideoInfo {
109 file_url: "https://example.com/video.mp4".to_string(),
110 thumb_url: "https://example.com/thumb.jpg".to_string(),
111 file_md5: "abc123".to_string(),
112 length: Some(1024),
113 },
114 };
115 let json = serde_json::to_string(&req).expect("Failed to serialize");
116 assert!(json.contains("appId"));
117 assert!(json.contains("videoInfo"));
118 }
119}