1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use crate::{
client::WhmcsClient,
error::WhmcsError,
models::clients::{
GetClientDetailsParams, GetClientDetailsResponse, GetClientGroupsResponse, GetClientParams,
GetClientPasswordParams, GetClientPasswordResponse, GetClientsResponse,
},
resources::endpoint,
};
impl WhmcsClient {
endpoint!(
/// Obtain a vector of client groups
///
/// # Example
/// ```no_run
/// # use whmcs::{WhmcsBuilder, models::clients::GetClientGroupsResponse};
/// # async fn run() -> Result<(), whmcs::WhmcsError> {
/// let client = WhmcsBuilder::new()
/// .url("https://example.com/includes/api.php")
/// .api_identifier("id")
/// .api_secret("secret")
/// .build()
/// .expect("Failed to build client");
///
/// let response = client.get_client_groups().await;
/// # Ok(())
/// # }
/// ```
///
/// # Reference
/// <https://developers.whmcs.com/api-reference/getclientgroups/>
///
/// # Errors
/// Will return a [`WhmcsError`](`crate::WhmcsError`) if the API returns an error.
get_client_groups,
"GetClientGroups",
GetClientGroupsResponse
);
endpoint!(
#[deprecated(since = "0.1.0", note = "Deprecated in WHMCS 8.0.0")]
/// Obtain the encrypted client password
///
/// # Example
/// ```no_run
/// # use whmcs::{WhmcsBuilder, models::clients::GetClientPasswordParams};
/// # async fn run() -> Result<(), whmcs::WhmcsError> {
/// let client = WhmcsBuilder::new()
/// .url("https://example.com/includes/api.php")
/// .api_identifier("id")
/// .api_secret("secret")
/// .build()
/// .expect("Failed to build client");
///
/// let response = client.get_client_password(GetClientPasswordParams::default().client_id(1)).await;
/// # Ok(())
/// # }
/// ```
///
/// # Reference
/// <https://developers.whmcs.com/api-reference/getclientpassword/>
///
/// # Errors
/// Will return a [`WhmcsError`](`crate::WhmcsError`) if the API returns an error.
get_client_password,
"GetClientPassword",
GetClientPasswordParams,
GetClientPasswordResponse
);
endpoint!(
/// Obtain the Clients that match passed criteria
///
/// # Examples
/// ## Getting 25 clients
/// ```no_run
/// # use whmcs::{WhmcsBuilder, models::clients::GetClientParams};
/// # async fn run() -> Result<(), whmcs::WhmcsError> {
/// let client = WhmcsBuilder::new()
/// .url("https://example.com/includes/api.php")
/// .api_identifier("id")
/// .api_secret("secret")
/// .build()
/// .expect("Failed to build client");
///
/// let response = client.get_clients(None).await;
/// # Ok(())
/// # }
/// ```
///
/// ## Getting clients via a search term
/// ```no_run
/// # use whmcs::{WhmcsBuilder, models::clients::GetClientParams};
/// # async fn run() -> Result<(), whmcs::WhmcsError> {
/// let client = WhmcsBuilder::new()
/// .url("https://example.com/includes/api.php")
/// .api_identifier("id")
/// .api_secret("secret")
/// .build()
/// .expect("Failed to build client");
///
/// let response = client.get_clients(GetClientParams::default().search("user@example.com")).await;
/// # Ok(())
/// # }
/// ```
///
/// ## Filtering options
/// ```no_run
/// # use whmcs::{WhmcsSorting, WhmcsBuilder, models::clients::{GetClientParams, ClientStatus, ClientOrderBy}};
/// # async fn run() -> Result<(), whmcs::WhmcsError> {
/// let client = WhmcsBuilder::new()
/// .url("https://example.com/includes/api.php")
/// .api_identifier("id")
/// .api_secret("secret")
/// .build()
/// .expect("Failed to build client");
///
/// let response = client.get_clients(GetClientParams::default().search("user@example.com").limit_start(10).limit_num(25).sorting(WhmcsSorting::Descending).status(ClientStatus::Active).order_by(ClientOrderBy::Email)).await;
/// # Ok(())
/// # }
/// ```
///
/// # Reference
/// <https://developers.whmcs.com/api-reference/getclients/>
///
/// # Errors
/// Will return a [`WhmcsError`](`crate::WhmcsError`) if the API returns an error.
get_clients,
"GetClients",
GetClientParams,
GetClientsResponse
);
endpoint!(
/// Obtain the Clients Details for a specific client
///
/// # Examples
/// ## Getting client details
/// ```no_run
/// # use whmcs::{WhmcsBuilder, models::clients::GetClientDetailsParams};
/// # async fn run() -> Result<(), whmcs::WhmcsError> {
/// let client = WhmcsBuilder::new()
/// .url("https://example.com/includes/api.php")
/// .api_identifier("id")
/// .api_secret("secret")
/// .build()
/// .expect("Failed to build client");
///
/// let response = client.get_client_details(GetClientDetailsParams::default().email("user@example.com")).await;
/// # Ok(())
/// # }
/// ```
///
/// ## Getting client details and statistics
/// ```no_run
/// # use whmcs::{WhmcsBuilder, models::clients::GetClientDetailsParams};
/// # async fn run() -> Result<(), whmcs::WhmcsError> {
/// let client = WhmcsBuilder::new()
/// .url("https://example.com/includes/api.php")
/// .api_identifier("id")
/// .api_secret("secret")
/// .build()
/// .expect("Failed to build client");
///
/// let response = client.get_client_details(GetClientDetailsParams::default().client_id(1).include_stats(true)).await;
/// if let Some(stats) = response.unwrap().stats {
/// // ... do something with the stats
/// }
/// # Ok(())
/// # }
/// ```
///
///
/// # Reference
/// <https://developers.whmcs.com/api-reference/getclientsdetails/>
///
/// # Errors
/// Will return a [`WhmcsError`](`crate::WhmcsError`) if the API returns an error.
get_client_details,
"GetClientsDetails",
GetClientDetailsParams,
GetClientDetailsResponse
);
}
#[cfg(test)]
mod tests {
use crate::{
error::WhmcsError,
models::clients::{
ClientGroupId, ClientStatus, GetClientDetailsParams, GetClientParams,
GetClientPasswordParams,
},
test::get_test_client,
};
#[ignore = "Require WHMCS instance"]
#[tokio::test]
async fn get_client_groups() {
let client = get_test_client();
let response = client.get_client_groups().await.unwrap();
assert_eq!(response.total_results, 0);
assert_eq!(response.groups.len(), 0);
}
#[ignore = "Require WHMCS instance"]
#[tokio::test]
async fn invalid_get_client_password() {
let client = get_test_client();
#[allow(deprecated)]
let err = client
.get_client_password(None)
.await
.expect_err("should be an error");
assert!(matches!(err, WhmcsError::ApiError(_)));
}
#[ignore = "Require WHMCS instance"]
#[tokio::test]
async fn get_client_password() {
let client = get_test_client();
#[allow(deprecated)]
let response = client
.get_client_password(GetClientPasswordParams::default().client_id(1))
.await
.unwrap();
assert!(response.password.starts_with("$2y$"));
}
#[ignore = "Require WHMCS instance"]
#[tokio::test]
async fn get_clients() {
let client = get_test_client();
let response = client.get_clients(None).await.unwrap();
assert_ne!(response.total_results, 0);
assert_eq!(response.number_returned, 25);
assert_eq!(response.start_number, 0);
assert_eq!(response.clients.len(), 25);
assert_eq!(response.clients[0].group_id, ClientGroupId::new(0));
assert_eq!(response.clients[0].status, ClientStatus::Inactive);
}
#[ignore = "Require WHMCS instance"]
#[tokio::test]
async fn get_no_clients() {
let client = get_test_client();
let response = client
.get_clients(GetClientParams::default().search("nonexistentuser"))
.await
.unwrap();
assert_eq!(response.total_results, 0);
assert_eq!(response.number_returned, 0);
assert_eq!(response.start_number, 0);
assert_eq!(response.clients.len(), 0);
}
#[ignore = "Require WHMCS instance"]
#[tokio::test]
async fn get_single_client() {
let client = get_test_client();
let email = std::env::var("EMAIL").unwrap();
let response = client
.get_clients(GetClientParams::default().search(email.clone()))
.await
.unwrap();
assert_ne!(response.total_results, 0);
assert_eq!(response.number_returned, 1);
assert_eq!(response.start_number, 0);
assert_eq!(response.clients.len(), 1);
assert_eq!(response.clients[0].group_id, ClientGroupId::new(0));
assert_eq!(response.clients[0].status, ClientStatus::Active);
assert_eq!(response.clients[0].email, email);
}
#[ignore = "Require WHMCS instance"]
#[tokio::test]
async fn get_client_details() {
let client = get_test_client();
let email = std::env::var("EMAIL").unwrap();
let response = client
.get_client_details(GetClientDetailsParams::default().email(email.clone()))
.await
.unwrap();
assert_eq!(response.client.email, email);
assert_eq!(response.client.users.len(), 1);
assert_eq!(response.client.users[0].email, email);
assert!(response.client.users[0].is_owner);
assert!(response.stats.is_none());
}
}