wechat_mp_sdk/api/
logistics.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 LogisticsRequest {
15 #[serde(flatten)]
16 pub payload: HashMap<String, Value>,
17}
18
19#[non_exhaustive]
20#[derive(Debug, Clone, Deserialize, Serialize)]
21pub struct LogisticsResponse {
22 #[serde(default)]
23 pub(crate) errcode: i32,
24 #[serde(default)]
25 pub(crate) errmsg: String,
26 #[serde(flatten)]
27 pub extra: HashMap<String, Value>,
28}
29
30pub struct LogisticsApi {
31 context: Arc<WechatContext>,
32}
33
34impl LogisticsApi {
35 pub fn new(context: Arc<WechatContext>) -> Self {
36 Self { context }
37 }
38
39 pub async fn bind_account(
40 &self,
41 request: &LogisticsRequest,
42 ) -> Result<LogisticsResponse, WechatError> {
43 self.post_json("/cgi-bin/express/business/account/bind", request)
44 .await
45 }
46
47 pub async fn get_all_account(
48 &self,
49 request: &LogisticsRequest,
50 ) -> Result<LogisticsResponse, WechatError> {
51 self.post_json("/cgi-bin/express/business/account/getall", request)
52 .await
53 }
54
55 pub async fn get_all_delivery(
56 &self,
57 request: &LogisticsRequest,
58 ) -> Result<LogisticsResponse, WechatError> {
59 self.post_json("/cgi-bin/express/business/delivery/getall", request)
60 .await
61 }
62
63 pub async fn get_order(
64 &self,
65 request: &LogisticsRequest,
66 ) -> Result<LogisticsResponse, WechatError> {
67 self.post_json("/cgi-bin/express/business/order/get", request)
68 .await
69 }
70
71 pub async fn add_order(
72 &self,
73 request: &LogisticsRequest,
74 ) -> Result<LogisticsResponse, WechatError> {
75 self.post_json("/cgi-bin/express/business/order/add", request)
76 .await
77 }
78
79 pub async fn get_path(
80 &self,
81 request: &LogisticsRequest,
82 ) -> Result<LogisticsResponse, WechatError> {
83 self.post_json("/cgi-bin/express/business/path/get", request)
84 .await
85 }
86
87 async fn post_json<B: Serialize>(
88 &self,
89 endpoint: &str,
90 body: &B,
91 ) -> Result<LogisticsResponse, WechatError> {
92 let response: LogisticsResponse = self.context.authed_post(endpoint, body).await?;
93 WechatError::check_api(response.errcode, &response.errmsg)?;
94 Ok(response)
95 }
96}
97
98impl WechatApi for LogisticsApi {
99 fn context(&self) -> &WechatContext {
100 &self.context
101 }
102
103 fn api_name(&self) -> &'static str {
104 "logistics"
105 }
106}
107
108#[cfg(test)]
109mod tests {
110 use super::*;
111
112 #[test]
113 fn logistics_response_deserializes() {
114 let json = r#"{"errcode":0,"errmsg":"ok","waybill_id":"x"}"#;
115 let response: LogisticsResponse = serde_json::from_str(json).unwrap();
116 assert_eq!(response.errcode, 0);
117 }
118}