wechat_mp_sdk/api/
plugin.rs1use 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 ManagePluginApplicationRequest {
15 pub action: String,
16 #[serde(flatten)]
17 pub payload: HashMap<String, Value>,
18}
19
20#[non_exhaustive]
21#[derive(Debug, Clone, Serialize)]
22pub struct ManagePluginRequest {
23 pub action: String,
24 #[serde(flatten)]
25 pub payload: HashMap<String, Value>,
26}
27
28#[non_exhaustive]
29#[derive(Debug, Clone, Deserialize, Serialize)]
30pub struct PluginResponse {
31 #[serde(default)]
32 pub(crate) errcode: i32,
33 #[serde(default)]
34 pub(crate) errmsg: String,
35 #[serde(flatten)]
36 pub extra: HashMap<String, Value>,
37}
38
39pub struct PluginApi {
40 context: Arc<WechatContext>,
41}
42
43impl PluginApi {
44 pub fn new(context: Arc<WechatContext>) -> Self {
45 Self { context }
46 }
47
48 pub async fn manage_plugin_application(
49 &self,
50 request: &ManagePluginApplicationRequest,
51 ) -> Result<PluginResponse, WechatError> {
52 self.post_plugin(request).await
53 }
54
55 pub async fn manage_plugin(
56 &self,
57 request: &ManagePluginRequest,
58 ) -> Result<PluginResponse, WechatError> {
59 self.post_plugin(request).await
60 }
61
62 async fn post_plugin<B: Serialize>(&self, body: &B) -> Result<PluginResponse, WechatError> {
63 let response: PluginResponse = self.context.authed_post("/wxa/plugin", body).await?;
64 WechatError::check_api(response.errcode, &response.errmsg)?;
65 Ok(response)
66 }
67}
68
69impl WechatApi for PluginApi {
70 fn context(&self) -> &WechatContext {
71 &self.context
72 }
73
74 fn api_name(&self) -> &'static str {
75 "plugin"
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82
83 #[test]
84 fn plugin_response_deserializes() {
85 let json = r#"{"errcode":0,"errmsg":"ok","data":{}}"#;
86 let response: PluginResponse = serde_json::from_str(json).unwrap();
87 assert_eq!(response.errcode, 0);
88 }
89}