async_wechat/
lib.rs

1pub mod constants;
2pub mod official_account;
3
4use deadpool_redis::{Pool, Runtime};
5use reqwest::Client;
6use serde::{Deserialize, Serialize};
7use std::sync::Arc;
8
9pub struct OfficialAccount {
10    config: Config,
11    rdb_pool: Arc<Pool>,
12    client: Client,
13}
14
15pub struct Config {
16    pub appid: String,
17    pub app_secret: String,
18    pub token: String,
19    pub encoding_aes_key: Option<String>,
20}
21
22#[derive(Serialize, Deserialize, Debug)]
23pub struct TokenResponse {
24    pub access_token: String,
25    expires_in: u64,
26}
27
28impl OfficialAccount {
29    /// Creates a new instance of the OfficialAccount struct.
30    ///
31    /// # Arguments
32    ///
33    /// * `appid` - The appid of the WeChat application.
34    /// * `app_secret` - The app secret of the WeChat application.
35    /// * `cfg` - The URL of the Redis database connection string.
36    ///
37    /// # Returns
38    ///
39    /// A new instance of the OfficialAccount struct.
40    pub fn new(conf: Config, redis_url: String) -> Self {
41        let pool_config = deadpool_redis::Config::from_url(redis_url);
42
43        let rdb_pool = match pool_config.create_pool(Some(Runtime::Tokio1)) {
44            Ok(pool) => Arc::new(pool),
45            Err(err) => {
46                panic!("Failed to create Redis pool: {}", err);
47            }
48        };
49
50        OfficialAccount {
51            config: conf,
52            rdb_pool,
53            client: Client::new(),
54        }
55    }
56}