Skip to main content

wechat_mp_sdk/api/
live.rs

1//! Live Streaming API
2
3use std::collections::HashMap;
4use std::sync::Arc;
5
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8
9use super::{WechatApi, WechatContext};
10use crate::error::WechatError;
11
12#[non_exhaustive]
13#[derive(Debug, Clone, Serialize)]
14pub struct LiveRequest {
15    #[serde(flatten)]
16    pub payload: HashMap<String, Value>,
17}
18
19#[non_exhaustive]
20#[derive(Debug, Clone, Serialize)]
21pub struct DeleteRoomRequest {
22    pub id: i32,
23}
24
25#[non_exhaustive]
26#[derive(Debug, Clone, Serialize)]
27pub struct GetLiveInfoRequest {
28    pub start: i32,
29    pub limit: i32,
30}
31
32#[non_exhaustive]
33#[derive(Debug, Clone, Deserialize, Serialize)]
34pub struct LiveResponse {
35    #[serde(default)]
36    pub(crate) errcode: i32,
37    #[serde(default)]
38    pub(crate) errmsg: String,
39    #[serde(flatten)]
40    pub extra: HashMap<String, Value>,
41}
42
43pub struct LiveApi {
44    context: Arc<WechatContext>,
45}
46
47impl LiveApi {
48    pub fn new(context: Arc<WechatContext>) -> Self {
49        Self { context }
50    }
51
52    pub async fn create_room(&self, request: &LiveRequest) -> Result<LiveResponse, WechatError> {
53        self.post_json("/wxaapi/broadcast/room/create", request)
54            .await
55    }
56
57    pub async fn delete_room(
58        &self,
59        request: &DeleteRoomRequest,
60    ) -> Result<LiveResponse, WechatError> {
61        self.post_json("/wxaapi/broadcast/room/deleteroom", request)
62            .await
63    }
64
65    pub async fn edit_room(&self, request: &LiveRequest) -> Result<LiveResponse, WechatError> {
66        self.post_json("/wxaapi/broadcast/room/editroom", request)
67            .await
68    }
69
70    pub async fn get_live_info(
71        &self,
72        request: &GetLiveInfoRequest,
73    ) -> Result<LiveResponse, WechatError> {
74        self.post_json("/wxa/business/getliveinfo", request).await
75    }
76
77    pub async fn add_goods(&self, request: &LiveRequest) -> Result<LiveResponse, WechatError> {
78        self.post_json("/wxaapi/broadcast/goods/add", request).await
79    }
80
81    pub async fn update_goods_info(
82        &self,
83        request: &LiveRequest,
84    ) -> Result<LiveResponse, WechatError> {
85        self.post_json("/wxaapi/broadcast/goods/update", request)
86            .await
87    }
88
89    pub async fn delete_goods_info(
90        &self,
91        request: &LiveRequest,
92    ) -> Result<LiveResponse, WechatError> {
93        self.post_json("/wxaapi/broadcast/goods/delete", request)
94            .await
95    }
96
97    pub async fn push_message(&self, request: &LiveRequest) -> Result<LiveResponse, WechatError> {
98        self.post_json("/wxaapi/broadcast/subscribe/send", request)
99            .await
100    }
101
102    pub async fn get_followers(&self, request: &LiveRequest) -> Result<LiveResponse, WechatError> {
103        self.post_json("/wxaapi/broadcast/subscribe/get", request)
104            .await
105    }
106
107    async fn post_json<B: Serialize>(
108        &self,
109        endpoint: &str,
110        body: &B,
111    ) -> Result<LiveResponse, WechatError> {
112        let response: LiveResponse = self.context.authed_post(endpoint, body).await?;
113        WechatError::check_api(response.errcode, &response.errmsg)?;
114        Ok(response)
115    }
116}
117
118impl WechatApi for LiveApi {
119    fn context(&self) -> &WechatContext {
120        &self.context
121    }
122
123    fn api_name(&self) -> &'static str {
124        "live"
125    }
126}
127
128#[cfg(test)]
129mod tests {
130    use super::*;
131
132    #[test]
133    fn live_response_deserializes() {
134        let json = r#"{"errcode":0,"errmsg":"ok","roomid":1}"#;
135        let response: LiveResponse = serde_json::from_str(json).unwrap();
136        assert_eq!(response.errcode, 0);
137        assert!(response.extra.contains_key("roomid"));
138    }
139}