gewe_http/contact/
info.rs

1use crate::client::GeweHttpClient;
2use gewe_core::{
3    CheckRelationRequest, CheckRelationResponse, FetchContactsListCacheRequest,
4    FetchContactsListRequest, FetchContactsListResponse, GetContactBriefInfoRequest,
5    GetContactBriefInfoResponse, GetContactDetailInfoRequest, GetContactDetailInfoResponse,
6    GetPhoneAddressListRequest, GetPhoneAddressListResponse, GeweError, SearchContactsRequest,
7    SearchContactsResponse,
8};
9use tracing::instrument;
10
11impl GeweHttpClient {
12    #[instrument(skip(self))]
13    pub async fn fetch_contacts_list(
14        &self,
15        req: FetchContactsListRequest<'_>,
16    ) -> Result<FetchContactsListResponse, GeweError> {
17        let env = self
18            .post_api::<_, FetchContactsListResponse>(
19                "gewe/v2/api/contacts/fetchContactsList",
20                &req,
21            )
22            .await?;
23        env.data.ok_or(GeweError::MissingData)
24    }
25
26    #[instrument(skip(self))]
27    pub async fn fetch_contacts_list_cache(
28        &self,
29        req: FetchContactsListCacheRequest<'_>,
30    ) -> Result<(), GeweError> {
31        let _ = self
32            .post_api::<_, ()>("gewe/v2/api/contacts/fetchContactsListCache", &req)
33            .await?;
34        Ok(())
35    }
36
37    #[instrument(skip(self))]
38    pub async fn search_contacts(
39        &self,
40        req: SearchContactsRequest<'_>,
41    ) -> Result<SearchContactsResponse, GeweError> {
42        let env = self
43            .post_api::<_, SearchContactsResponse>("gewe/v2/api/contacts/search", &req)
44            .await?;
45        env.data.ok_or(GeweError::MissingData)
46    }
47
48    #[instrument(skip(self))]
49    pub async fn get_contact_brief_info(
50        &self,
51        req: GetContactBriefInfoRequest<'_>,
52    ) -> Result<GetContactBriefInfoResponse, GeweError> {
53        let env = self
54            .post_api::<_, GetContactBriefInfoResponse>("gewe/v2/api/contacts/getBriefInfo", &req)
55            .await?;
56        env.data.ok_or(GeweError::MissingData)
57    }
58
59    #[instrument(skip(self))]
60    pub async fn get_contact_detail_info(
61        &self,
62        req: GetContactDetailInfoRequest<'_>,
63    ) -> Result<GetContactDetailInfoResponse, GeweError> {
64        let env = self
65            .post_api::<_, GetContactDetailInfoResponse>("gewe/v2/api/contacts/getDetailInfo", &req)
66            .await?;
67        env.data.ok_or(GeweError::MissingData)
68    }
69
70    #[instrument(skip(self))]
71    pub async fn get_phone_address_list(
72        &self,
73        req: GetPhoneAddressListRequest<'_>,
74    ) -> Result<GetPhoneAddressListResponse, GeweError> {
75        let env = self
76            .post_api::<_, GetPhoneAddressListResponse>(
77                "gewe/v2/api/contacts/getPhoneAddressList",
78                &req,
79            )
80            .await?;
81        env.data.ok_or(GeweError::MissingData)
82    }
83
84    #[instrument(skip(self))]
85    pub async fn check_contact_relation(
86        &self,
87        req: CheckRelationRequest<'_>,
88    ) -> Result<CheckRelationResponse, GeweError> {
89        let env = self
90            .post_api::<_, CheckRelationResponse>("gewe/v2/api/contacts/checkRelation", &req)
91            .await?;
92        env.data.ok_or(GeweError::MissingData)
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn test_fetch_contacts_list_request_serialization() {
102        let req = FetchContactsListRequest { app_id: "test_app" };
103        let json = serde_json::to_string(&req).expect("Failed to serialize");
104        assert!(json.contains("appId"));
105        assert!(json.contains("test_app"));
106    }
107
108    #[test]
109    fn test_fetch_contacts_list_cache_request_serialization() {
110        let req = FetchContactsListCacheRequest { app_id: "test_app" };
111        let json = serde_json::to_string(&req).expect("Failed to serialize");
112        assert!(json.contains("appId"));
113        assert!(json.contains("test_app"));
114    }
115
116    #[test]
117    fn test_search_contacts_request_serialization() {
118        let req = SearchContactsRequest {
119            app_id: "test_app",
120            contacts_info: "search_keyword",
121        };
122        let json = serde_json::to_string(&req).expect("Failed to serialize");
123        assert!(json.contains("appId"));
124        assert!(json.contains("test_app"));
125        assert!(json.contains("contactsInfo"));
126        assert!(json.contains("search_keyword"));
127    }
128
129    #[test]
130    fn test_get_contact_brief_info_request_serialization() {
131        let req = GetContactBriefInfoRequest {
132            app_id: "test_app",
133            wxids: vec!["wxid1", "wxid2", "wxid3"],
134        };
135        let json = serde_json::to_string(&req).expect("Failed to serialize");
136        assert!(json.contains("appId"));
137        assert!(json.contains("test_app"));
138        assert!(json.contains("wxids"));
139        assert!(json.contains("wxid1"));
140        assert!(json.contains("wxid2"));
141    }
142
143    #[test]
144    fn test_get_contact_brief_info_request_empty_wxids() {
145        let req = GetContactBriefInfoRequest {
146            app_id: "test_app",
147            wxids: vec![],
148        };
149        let json = serde_json::to_string(&req).expect("Failed to serialize");
150        assert!(json.contains("appId"));
151        assert!(json.contains("wxids"));
152        assert!(json.contains("[]"));
153    }
154
155    #[test]
156    fn test_get_contact_detail_info_request_serialization() {
157        let req = GetContactDetailInfoRequest {
158            app_id: "test_app",
159            wxids: vec!["target_wxid"],
160        };
161        let json = serde_json::to_string(&req).expect("Failed to serialize");
162        assert!(json.contains("appId"));
163        assert!(json.contains("test_app"));
164        assert!(json.contains("wxids"));
165        assert!(json.contains("target_wxid"));
166    }
167
168    #[test]
169    fn test_get_phone_address_list_request_serialization() {
170        let req = GetPhoneAddressListRequest {
171            app_id: "test_app",
172            phones: Some(vec!["+1234567890"]),
173        };
174        let json = serde_json::to_string(&req).expect("Failed to serialize");
175        assert!(json.contains("appId"));
176        assert!(json.contains("test_app"));
177    }
178
179    #[test]
180    fn test_check_relation_request_serialization() {
181        let req = CheckRelationRequest {
182            app_id: "test_app",
183            wxids: vec!["check_wxid"],
184        };
185        let json = serde_json::to_string(&req).expect("Failed to serialize");
186        assert!(json.contains("appId"));
187        assert!(json.contains("test_app"));
188        assert!(json.contains("wxids"));
189        assert!(json.contains("check_wxid"));
190    }
191
192    #[test]
193    fn test_search_contacts_with_unicode() {
194        let req = SearchContactsRequest {
195            app_id: "测试应用",
196            contacts_info: "搜索内容",
197        };
198        let json = serde_json::to_string(&req).expect("Failed to serialize");
199        assert!(json.contains("测试应用"));
200        assert!(json.contains("搜索内容"));
201    }
202
203    #[test]
204    fn test_get_contact_brief_info_with_special_chars() {
205        let req = GetContactBriefInfoRequest {
206            app_id: "app-123_test",
207            wxids: vec!["wxid_with_underscore", "wxid-with-dash"],
208        };
209        let json = serde_json::to_string(&req).expect("Failed to serialize");
210        assert!(json.contains("app-123_test"));
211        assert!(json.contains("wxid_with_underscore"));
212        assert!(json.contains("wxid-with-dash"));
213    }
214}