1use crate::client::GeweHttpClient;
2use gewe_core::{
3 ChangeMacToIpadRequest, CheckLoginRequest, CheckLoginResponse, CheckOnlineRequest,
4 CheckOnlineResponse, DialogLoginRequest, DialogLoginResponse, GetLoginQrCodeRequest,
5 GetLoginQrCodeResponse, GeweError, LoginByAccountRequest, LoginByAccountResponse,
6 LogoutRequest, ReconnectionRequest, ReconnectionResponse, SetCallbackRequest,
7};
8use serde_json::Value;
9use tracing::instrument;
10
11impl GeweHttpClient {
12 #[instrument(skip(self))]
13 pub async fn get_login_qr_code(
14 &self,
15 req: GetLoginQrCodeRequest<'_>,
16 ) -> Result<GetLoginQrCodeResponse, GeweError> {
17 let env = self
18 .post_api::<_, GetLoginQrCodeResponse>("gewe/v2/api/login/getLoginQrCode", &req)
19 .await?;
20 env.data.ok_or(GeweError::MissingData)
21 }
22
23 #[instrument(skip(self))]
24 pub async fn check_login(
25 &self,
26 req: CheckLoginRequest<'_>,
27 ) -> Result<CheckLoginResponse, GeweError> {
28 let env = self
29 .post_api::<_, CheckLoginResponse>("gewe/v2/api/login/checkLogin", &req)
30 .await?;
31 env.data.ok_or(GeweError::MissingData)
32 }
33
34 #[instrument(skip(self))]
35 pub async fn dialog_login(
36 &self,
37 req: DialogLoginRequest<'_>,
38 ) -> Result<DialogLoginResponse, GeweError> {
39 let env = self
40 .post_api::<_, DialogLoginResponse>("gewe/v2/api/login/dialogLogin", &req)
41 .await?;
42 env.data.ok_or(GeweError::MissingData)
43 }
44
45 #[instrument(skip(self))]
46 pub async fn login_by_account(
47 &self,
48 req: LoginByAccountRequest<'_>,
49 ) -> Result<LoginByAccountResponse, GeweError> {
50 let env = self
51 .post_api::<_, LoginByAccountResponse>("gewe/v2/api/login/loginByAccount", &req)
52 .await?;
53 env.data.ok_or(GeweError::MissingData)
54 }
55
56 #[instrument(skip(self))]
57 pub async fn set_callback(&self, req: SetCallbackRequest<'_>) -> Result<(), GeweError> {
58 self.post_api::<_, Value>("gewe/v2/api/login/setCallback", &req)
59 .await?;
60 Ok(())
61 }
62
63 #[instrument(skip(self))]
64 pub async fn change_mac_to_ipad(
65 &self,
66 req: ChangeMacToIpadRequest<'_>,
67 ) -> Result<GetLoginQrCodeResponse, GeweError> {
68 let env = self
69 .post_api::<_, GetLoginQrCodeResponse>("gewe/v2/api/login/changeMacToIpad", &req)
70 .await?;
71 env.data.ok_or(GeweError::MissingData)
72 }
73
74 #[instrument(skip(self))]
75 pub async fn check_online(
76 &self,
77 req: CheckOnlineRequest<'_>,
78 ) -> Result<CheckOnlineResponse, GeweError> {
79 let env = self
80 .post_api::<_, CheckOnlineResponse>("gewe/v2/api/login/checkOnline", &req)
81 .await?;
82 env.data.ok_or(GeweError::MissingData)
83 }
84
85 #[instrument(skip(self))]
86 pub async fn reconnection(
87 &self,
88 req: ReconnectionRequest<'_>,
89 ) -> Result<Option<ReconnectionResponse>, GeweError> {
90 let env = self
91 .post_api::<_, ReconnectionResponse>("gewe/v2/api/login/reconnection", &req)
92 .await?;
93 Ok(env.data)
94 }
95
96 #[instrument(skip(self))]
97 pub async fn logout(&self, req: LogoutRequest<'_>) -> Result<(), GeweError> {
98 self.post_api::<_, Value>("gewe/v2/api/login/logout", &req)
99 .await?;
100 Ok(())
101 }
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 #[test]
109 fn test_get_login_qr_code_request_serialization() {
110 let req = GetLoginQrCodeRequest {
111 app_id: "test_app",
112 r#type: "ipad",
113 region_id: "cn",
114 proxy_ip: Some("127.0.0.1"),
115 ttuid: None,
116 aid: None,
117 };
118 let json = serde_json::to_string(&req).expect("Failed to serialize");
119 assert!(json.contains("appId"));
120 assert!(json.contains("test_app"));
121 assert!(json.contains("ipad"));
122 assert!(json.contains("cn"));
123 }
124
125 #[test]
126 fn test_check_login_request_serialization() {
127 let req = CheckLoginRequest {
128 app_id: "test_app",
129 uuid: "uuid-1234",
130 proxy_ip: None,
131 captch_code: None,
132 auto_sliding: Some(true),
133 };
134 let json = serde_json::to_string(&req).expect("Failed to serialize");
135 assert!(json.contains("appId"));
136 assert!(json.contains("uuid"));
137 assert!(json.contains("uuid-1234"));
138 }
139
140 #[test]
141 fn test_dialog_login_request_serialization() {
142 let req = DialogLoginRequest {
143 app_id: "test_app",
144 region_id: "cn",
145 proxy_ip: Some("127.0.0.1"),
146 aid: None,
147 };
148 let json = serde_json::to_string(&req).expect("Failed to serialize");
149 assert!(json.contains("appId"));
150 assert!(json.contains("regionId"));
151 }
152
153 #[test]
154 fn test_login_by_account_request_serialization() {
155 let req = LoginByAccountRequest {
156 app_id: "test_app",
157 proxy_ip: "127.0.0.1",
158 region_id: "cn",
159 account: "test_account",
160 password: "password123",
161 step: 1,
162 };
163 let json = serde_json::to_string(&req).expect("Failed to serialize");
164 assert!(json.contains("appId"));
165 assert!(json.contains("test_account"));
166 assert!(json.contains("password123"));
167 assert!(json.contains("proxyIp"));
168 }
169
170 #[test]
171 fn test_set_callback_request_serialization() {
172 let req = SetCallbackRequest {
173 token: "test_token",
174 callback_url: "https://example.com/callback",
175 };
176 let json = serde_json::to_string(&req).expect("Failed to serialize");
177 assert!(json.contains("token"));
178 assert!(json.contains("https://example.com/callback"));
179 }
180
181 #[test]
182 fn test_change_mac_to_ipad_request_serialization() {
183 let req = ChangeMacToIpadRequest { app_id: "test_app" };
184 let json = serde_json::to_string(&req).expect("Failed to serialize");
185 assert!(json.contains("appId"));
186 assert!(json.contains("test_app"));
187 }
188
189 #[test]
190 fn test_check_online_request_serialization() {
191 let req = CheckOnlineRequest { app_id: "test_app" };
192 let json = serde_json::to_string(&req).expect("Failed to serialize");
193 assert!(json.contains("appId"));
194 assert!(json.contains("test_app"));
195 }
196
197 #[test]
198 fn test_reconnection_request_serialization() {
199 let req = ReconnectionRequest { app_id: "test_app" };
200 let json = serde_json::to_string(&req).expect("Failed to serialize");
201 assert!(json.contains("appId"));
202 assert!(json.contains("test_app"));
203 }
204
205 #[test]
206 fn test_logout_request_serialization() {
207 let req = LogoutRequest { app_id: "test_app" };
208 let json = serde_json::to_string(&req).expect("Failed to serialize");
209 assert!(json.contains("appId"));
210 assert!(json.contains("test_app"));
211 }
212
213 #[test]
214 fn test_get_login_qr_code_without_optional_fields() {
215 let req = GetLoginQrCodeRequest {
216 app_id: "test_app",
217 r#type: "mac",
218 region_id: "us",
219 proxy_ip: None,
220 ttuid: None,
221 aid: None,
222 };
223 let json = serde_json::to_string(&req).expect("Failed to serialize");
224 assert!(json.contains("appId"));
225 assert!(json.contains("test_app"));
226 assert!(json.contains("mac"));
227 }
228}