Skip to main content

wechat_mp_sdk/api/
delivery.rs

1//! Instant Delivery 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 DeliveryRequest {
15    #[serde(flatten)]
16    pub payload: HashMap<String, Value>,
17}
18
19#[non_exhaustive]
20#[derive(Debug, Clone, Deserialize, Serialize)]
21pub struct DeliveryResponse {
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 DeliveryApi {
31    context: Arc<WechatContext>,
32}
33
34impl DeliveryApi {
35    pub fn new(context: Arc<WechatContext>) -> Self {
36        Self { context }
37    }
38
39    pub async fn get_all_imme_delivery(
40        &self,
41        request: &DeliveryRequest,
42    ) -> Result<DeliveryResponse, WechatError> {
43        self.post_json("/cgi-bin/express/local/business/delivery/getall", request)
44            .await
45    }
46
47    pub async fn pre_add_order(
48        &self,
49        request: &DeliveryRequest,
50    ) -> Result<DeliveryResponse, WechatError> {
51        self.post_json("/cgi-bin/express/local/business/order/pre_add", request)
52            .await
53    }
54
55    pub async fn pre_cancel_order(
56        &self,
57        request: &DeliveryRequest,
58    ) -> Result<DeliveryResponse, WechatError> {
59        self.post_json("/cgi-bin/express/local/business/order/precancel", request)
60            .await
61    }
62
63    pub async fn add_local_order(
64        &self,
65        request: &DeliveryRequest,
66    ) -> Result<DeliveryResponse, WechatError> {
67        self.post_json("/cgi-bin/express/local/business/order/add", request)
68            .await
69    }
70
71    pub async fn cancel_local_order(
72        &self,
73        request: &DeliveryRequest,
74    ) -> Result<DeliveryResponse, WechatError> {
75        self.post_json("/cgi-bin/express/local/business/order/cancel", request)
76            .await
77    }
78
79    async fn post_json<B: Serialize>(
80        &self,
81        endpoint: &str,
82        body: &B,
83    ) -> Result<DeliveryResponse, WechatError> {
84        let response: DeliveryResponse = self.context.authed_post(endpoint, body).await?;
85        WechatError::check_api(response.errcode, &response.errmsg)?;
86        Ok(response)
87    }
88}
89
90impl WechatApi for DeliveryApi {
91    fn context(&self) -> &WechatContext {
92        &self.context
93    }
94
95    fn api_name(&self) -> &'static str {
96        "delivery"
97    }
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103
104    #[test]
105    fn delivery_response_deserializes() {
106        let json = r#"{"errcode":0,"errmsg":"ok","order_id":"x"}"#;
107        let response: DeliveryResponse = serde_json::from_str(json).unwrap();
108        assert_eq!(response.errcode, 0);
109        assert!(response.extra.contains_key("order_id"));
110    }
111}