wechat_api_rs/
lib.rs

1//! # WeChat API Rust SDK
2//!
3//! A comprehensive Rust SDK for WeChat Official Account and Mini Program APIs.
4//!
5//! ## Quick Start
6//!
7//! ```rust
8//! # use wechat_api_rs::WeChat;
9//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
10//! let client = WeChat::builder()
11//!     .app_id("your_app_id")
12//!     .app_secret("your_app_secret")
13//!     .build()?;
14//!
15//! // Use the client
16//! # Ok(())
17//! # }
18//! ```
19
20// Core modules
21pub mod client;
22pub mod error;
23pub mod auth;
24pub mod crypto;
25pub mod types;
26
27// Feature-gated modules
28#[cfg(feature = "official")]
29pub mod official;
30
31#[cfg(feature = "miniapp")]
32pub mod miniapp;
33
34// Re-exports
35pub use client::{Client, ClientBuilder};
36pub use error::{WeChatError, Result};
37pub use auth::AccessToken;
38pub use types::*;
39
40/// WeChat API base URLs
41pub mod api {
42    pub const WECHAT_API_BASE: &str = "https://api.weixin.qq.com";
43    pub const WECHAT_FILE_API_BASE: &str = "https://file.api.weixin.qq.com";
44}
45
46/// Main WeChat SDK client
47pub struct WeChat {
48    core_client: Client,
49}
50
51impl WeChat {
52    /// Create a new WeChat client builder
53    pub fn builder() -> WeChatBuilder {
54        WeChatBuilder::new()
55    }
56
57    /// Get the core client
58    pub fn core(&self) -> &Client {
59        &self.core_client
60    }
61
62    /// Get the official account client
63    #[cfg(feature = "official")]
64    pub fn official(&self) -> official::OfficialClient {
65        official::OfficialClient::new(self.core_client.clone())
66    }
67
68    /// Get the mini program client
69    #[cfg(feature = "miniapp")]
70    pub fn miniapp(&self) -> miniapp::MiniAppClient {
71        miniapp::MiniAppClient::new(self.core_client.clone())
72    }
73}
74
75/// Builder for WeChat client
76pub struct WeChatBuilder {
77    client_builder: ClientBuilder,
78}
79
80impl WeChatBuilder {
81    pub fn new() -> Self {
82        Self {
83            client_builder: ClientBuilder::new(),
84        }
85    }
86
87    pub fn app_id<S: Into<String>>(mut self, app_id: S) -> Self {
88        self.client_builder = self.client_builder.app_id(app_id);
89        self
90    }
91
92    pub fn app_secret<S: Into<String>>(mut self, app_secret: S) -> Self {
93        self.client_builder = self.client_builder.app_secret(app_secret);
94        self
95    }
96
97    pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
98        self.client_builder = self.client_builder.timeout(timeout);
99        self
100    }
101
102    pub fn build(self) -> Result<WeChat> {
103        let core_client = self.client_builder.build()?;
104        Ok(WeChat { core_client })
105    }
106}
107
108impl Default for WeChatBuilder {
109    fn default() -> Self {
110        Self::new()
111    }
112}