async_wechat/
lib.rs

1pub mod model;
2pub mod open_platform;
3
4use async_trait::async_trait;
5use model::{AccessTokenResponse, AuthResponse, UserInfoResponse};
6use std::error::Error;
7
8#[async_trait]
9pub trait WechatType {
10    fn get_redirect_url(&self, redirect_uri: String, state: Option<String>) -> String;
11    async fn get_access_token(&self, code: String) -> Result<AccessTokenResponse, Box<dyn Error>>;
12    async fn refresh_access_token(
13        &self,
14        appid: String,
15    ) -> Result<AccessTokenResponse, Box<dyn Error>>;
16    async fn check_access_token(&self, openid: String) -> Result<AuthResponse, Box<dyn Error>>;
17    async fn get_user_info(&self, openid: String) -> Result<UserInfoResponse, Box<dyn Error>>;
18}
19
20pub struct Wechat<T: WechatType> {
21    wechat_type: T,
22}
23
24impl<T: WechatType> Wechat<T> {
25    pub fn new(wechat_type: T) -> Self {
26        Wechat { wechat_type }
27    }
28
29    pub fn get_redirect_url(&self, redirect_uri: String, state: Option<String>) -> String {
30        self.wechat_type.get_redirect_url(redirect_uri, state)
31    }
32
33    pub async fn get_access_token(
34        &self,
35        code: String,
36    ) -> Result<AccessTokenResponse, Box<dyn Error>> {
37        self.wechat_type.get_access_token(code).await
38    }
39
40    pub async fn refresh_access_token(
41        &self,
42        appid: String,
43    ) -> Result<AccessTokenResponse, Box<dyn Error>> {
44        self.wechat_type.refresh_access_token(appid).await
45    }
46
47    pub async fn check_access_token(&self, openid: String) -> Result<AuthResponse, Box<dyn Error>> {
48        self.wechat_type.check_access_token(openid).await
49    }
50
51    pub async fn get_user_info(&self, openid: String) -> Result<UserInfoResponse, Box<dyn Error>> {
52        self.wechat_type.get_user_info(openid).await
53    }
54}