rust_anticaptcha/
control.rs

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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! # Control module
//!
//! This module is responsible for additional AntiCaptcha API service methods:
//!
//! - Get balance
//! - Get app statistics
//! - Report incorrect captcha
//! - Report correct captcha
//! - Get spending stats
//! - Push antigate variables
//!
//! ## Basic example for Control `get_balance` method
//!
//! ```no_run
//! use rust_anticaptcha::control::Control;
//!
//! async fn run() {
//!     let control_client = Control::new();
//!     control_client.get_balance(&"API_KEY".to_string()).await;
//! }
//! ```
//!
//! # Notes
//! Read more here:
//!
//! <https://anti-captcha.com/apidoc/methods/getBalance>
//!
//! <https://anti-captcha.com/apidoc/methods/getAppStats>
//!
//! <https://anti-captcha.com/apidoc/methods/getSpendingStats>
//!
use serde_json::{json, Value};

use super::core::constants::BASE_REQUEST_URL;
use super::core::enums::EnpPostfix;
use super::core::request_interface::RequestInterface;

/// Structure help processing additional AntiCaptcha methods
///
/// # Examples
///
/// ```
/// use rust_anticaptcha::control::Control;
///
/// let control_client = Control::new();
/// ```
///
/// # Notes
/// Read more here:
///
/// <https://anti-captcha.com/apidoc>
///
pub struct Control {
    request_interface: RequestInterface,
}
impl Control {
    /// Method init new Control struct with Web Requests Client
    ///
    /// # Examples
    ///
    /// ```
    /// use rust_anticaptcha::control::Control;
    ///
    /// let control_client = Control::new();
    /// ```
    pub fn new() -> Self {
        Control {
            request_interface: RequestInterface::new(),
        }
    }

    /// Method prepare and send control request to API
    ///
    /// # Arguments
    /// `payload` - request JSON payload
    /// `enp_postfix` - API URL postfix from `EnpPostfix`
    ///
    /// # Returns
    /// Method return API response JSON
    ///
    async fn send_control_request(&self, payload: &Value, enp_postfix: &EnpPostfix) -> Value {
        let req_url = BASE_REQUEST_URL.to_string() + &enp_postfix.to_string();
        let result = self
            .request_interface
            .send_post_request(&payload, &req_url)
            .await
            .unwrap();

        if result.status().eq(&200) {
            result.json().await.unwrap()
        } else {
            panic!(
                "{}",
                format!(
                    "Invalid request to API, status code - {} response - {}",
                    result.status(),
                    result.text().await.unwrap()
                )
            )
        }
    }

    /// Method request `getBalance` enp
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use rust_anticaptcha::control::Control;
    ///
    /// async fn run() {
    ///     let control_client = Control::new();
    ///     control_client.get_balance(&"API_KEY".to_string()).await;
    /// }
    /// ```
    ///
    /// # Notes
    /// Read more here:
    ///
    /// <https://anti-captcha.com/apidoc/methods/getBalance>
    pub async fn get_balance(&self, api_key: &String) -> Value {
        let map = json!({"clientKey": api_key});

        self.send_control_request(&map, &EnpPostfix::getBalance).await
    }

    /// Method request `getQueueStats ` enp
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use serde_json::json;
    ///
    /// use rust_anticaptcha::control::Control;
    ///
    /// async fn run() {
    ///     let control_client = Control::new();
    ///     let map = json!({"queueId": 6});
    ///     let result = control_client.get_queue_stats(&map).await;
    /// }
    /// ```
    ///
    /// # Notes
    /// Read more here:
    ///
    /// <https://anti-captcha.com/apidoc/methods/getQueueStats>
    pub async fn get_queue_stats(&self, enp_payload: &Value) -> Value {
        self.send_control_request(enp_payload, &EnpPostfix::getQueueStats)
            .await
    }

    /// Method request `reportIncorrectImageCaptcha ` enp
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use serde_json::json;
    ///
    /// use rust_anticaptcha::control::Control;
    ///
    /// async fn run() {
    ///     let control_client = Control::new();
    ///     let map = json!({"clientKey": "API_KEY", "taskId": 12345});
    ///     let result = control_client.report_incorrect_image(&map).await;
    /// }
    /// ```
    ///
    /// # Notes
    /// Read more here:
    ///
    /// <https://anti-captcha.com/apidoc/methods/reportIncorrectImageCaptcha>
    pub async fn report_incorrect_image(&self, enp_payload: &Value) -> Value {
        self.send_control_request(&enp_payload, &EnpPostfix::reportIncorrectImageCaptcha)
            .await
    }

    /// Method request `reportIncorrectRecaptcha ` enp
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use serde_json::json;
    ///
    /// use rust_anticaptcha::control::Control;
    ///
    /// async fn run() {
    ///     let control_client = Control::new();
    ///     let map = json!({"clientKey": "API_KEY", "taskId": 12345});
    ///     let result = control_client.report_incorrect_recaptcha(&map).await;
    /// }
    /// ```
    ///
    /// # Notes
    /// Read more here:
    ///
    /// <https://anti-captcha.com/apidoc/methods/reportIncorrectRecaptcha>
    pub async fn report_incorrect_recaptcha(&self, enp_payload: &Value) -> Value {
        self.send_control_request(&enp_payload, &EnpPostfix::reportIncorrectRecaptcha)
            .await
    }

    /// Method request `reportCorrectRecaptcha ` enp
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use serde_json::json;
    ///
    /// use rust_anticaptcha::control::Control;
    ///
    /// async fn run() {
    ///     let control_client = Control::new();
    ///     let map = json!({"clientKey": "API_KEY", "taskId": 12345});
    ///     control_client.report_correct_recaptcha(&map).await;
    /// }
    /// ```
    ///
    /// # Notes
    /// Read more here:
    ///
    /// <https://anti-captcha.com/apidoc/methods/reportCorrectRecaptcha>
    pub async fn report_correct_recaptcha(&self, enp_payload: &Value) -> Value {
        self.send_control_request(&enp_payload, &EnpPostfix::reportCorrectRecaptcha)
            .await
    }

    /// Method request `reportIncorrectHcaptcha ` enp
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use serde_json::json;
    ///
    /// use rust_anticaptcha::control::Control;
    ///
    /// async fn run() {
    ///     let control_client = Control::new();
    ///     let map = json!({"clientKey": "API_KEY", "taskId": 12345});
    ///     control_client.report_incorrect_hcaptcha(&map).await;
    /// };
    /// ```
    ///
    /// # Notes
    /// Read more here:
    ///
    /// <https://anti-captcha.com/apidoc/methods/reportIncorrectHcaptcha>
    pub async fn report_incorrect_hcaptcha(&self, enp_payload: &Value) -> Value {
        self.send_control_request(&enp_payload, &EnpPostfix::reportIncorrectHcaptcha)
            .await
    }

    /// Method request `pushAntiGateVariable ` enp
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use serde_json::json;
    ///
    /// use rust_anticaptcha::control::Control;
    ///
    /// async fn run() {
    ///     let control_client = Control::new();
    ///     let map = json!({
    ///                     "clientKey": "API_KEY",
    ///                     "taskId": 12345,
    ///                     "name": "my_late_variable",
    ///                     "value": "some_value"
    ///                 });
    ///     control_client.push_antigate_var(&map).await;
    /// }
    /// ```
    ///
    /// # Notes
    /// Read more here:
    ///
    /// <https://anti-captcha.com/apidoc/methods/pushAntiGateVariable>
    pub async fn push_antigate_var(&self, enp_payload: &Value) -> Value {
        self.send_control_request(&enp_payload, &EnpPostfix::pushAntiGateVariable)
            .await
    }

    /// Method request `getSpendingStats ` enp
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use serde_json::json;
    ///
    /// use rust_anticaptcha::control::Control;
    ///
    /// async fn run() {
    ///     let control_client = Control::new();
    ///     let map = json!({
    ///                     "clientKey": "API_KEY",
    ///                     "date": 1672185600,
    ///                     "queue": "Recaptcha Proxyless"
    ///                 });
    ///     control_client.get_spending_stats(&map).await;
    /// }
    /// ```
    ///
    /// # Returns
    /// This method grabs account spendings and task volume statistics for a 24 hour period.
    ///
    /// # Notes
    /// Read more here:
    ///
    /// <https://anti-captcha.com/apidoc/methods/getSpendingStats>
    pub async fn get_spending_stats(&self, enp_payload: &Value) -> Value {
        self.send_control_request(&enp_payload, &EnpPostfix::getSpendingStats)
            .await
    }

    /// Method request `getAppStats ` enp
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use serde_json::json;
    ///
    /// use rust_anticaptcha::control::Control;
    ///
    /// async fn run() {
    ///     let control_client = Control::new();
    ///     let map = json!({
    ///                     "clientKey": "API_KEY",
    ///                     "softId": 867,
    ///                     "mode": "money"
    ///                 });
    ///     control_client.get_app_stats(&map).await;
    /// }
    /// ```
    ///
    /// # Returns
    /// This method retrieves daily statistics for your application,
    /// which you register in Developer Center.
    ///
    /// # Notes
    /// Read more here:
    ///
    /// <https://anti-captcha.com/apidoc/methods/getAppStats>
    pub async fn get_app_stats(&self, enp_payload: &Value) -> Value {
        self.send_control_request(&enp_payload, &EnpPostfix::getAppStats)
            .await
    }
}