rust_wechat_weapp/api/
open_api.rs

1use rust_wechat_codegen::ServerResponse;
2use rust_wechat_core::client::ClientTrait;
3use serde::{Deserialize, Serialize};
4
5const CLEAR_QUOTA_URL: &str = "https://api.weixin.qq.com/cgi-bin/clear_quota";
6const CLEAR_QUOTA_V2_URL: &str = "https://api.weixin.qq.com/cgi-bin/clear_quota/v2";
7const GET_QUOTA_URL: &str = "https://api.weixin.qq.com/cgi-bin/openapi/quota/get";
8const GET_RID_INFO_URL: &str = "https://api.weixin.qq.com/cgi-bin/openapi/rid/get";
9
10#[derive(Debug, Serialize, Deserialize, ServerResponse)]
11pub struct ClearQuota;
12
13#[derive(Debug, Serialize, Deserialize, ServerResponse)]
14#[sr(flatten)]
15pub struct ApiQuota {
16    pub quota: Quota,
17    pub rate_limit: Option<Limit>,
18    pub component_rate_limit: Option<Limit>,
19}
20
21#[derive(Debug, Serialize, Deserialize)]
22pub struct Quota {
23    pub daily_limit: u32,
24    pub used: u32,
25    pub remain: u32,
26}
27
28#[derive(Debug, Serialize, Deserialize)]
29pub struct Limit {
30    pub call_count: u32,
31    pub refresh_second: u32,
32}
33
34#[derive(Debug, Serialize, Deserialize, ServerResponse)]
35#[sr(name = "request")]
36pub struct RidInfo {
37    pub invoke_time: u32,
38    pub cost_in_ms: u32,
39    pub request_url: String,
40    pub request_body: String,
41    pub response_body: String,
42    pub client_ip: String,
43}
44
45impl crate::Client {
46    pub async fn clear_quota(&self) -> crate::Result<()> {
47        let url = self.authorized_url(CLEAR_QUOTA_URL).await?;
48        let response: ClearQuotaResponse = self.json(url, &[("appid", &self.appid)]).await?;
49
50        Ok(response.ignore()?)
51    }
52
53    pub async fn clear_quota_by_app_secret(&self) -> crate::Result<()> {
54        let response: ClearQuotaResponse = self
55            .post(
56                CLEAR_QUOTA_V2_URL,
57                &[("appid", &self.appid), ("appsecret", &self.secret)],
58            )
59            .await?;
60
61        Ok(response.ignore()?)
62    }
63
64    pub async fn get_api_quota(&self, api_path: &str) -> crate::Result<ApiQuota> {
65        let url = self.authorized_url(GET_QUOTA_URL).await?;
66        let response: ApiQuotaResponse = self.json(url, &[("cgi_path", api_path)]).await?;
67
68        Ok(response.data()?)
69    }
70
71    pub async fn get_rid_info(&self, rid: &str) -> crate::Result<RidInfo> {
72        let url = self.authorized_url(GET_RID_INFO_URL).await?;
73        let response: RidInfoResponse = self.json(url, &[("rid", rid)]).await?;
74
75        Ok(response.request()?)
76    }
77}