rust_anticaptcha/
control.rs

1//! # Control module
2//!
3//! This module is responsible for additional AntiCaptcha API service methods:
4//!
5//! - Get balance
6//! - Get app statistics
7//! - Report incorrect captcha
8//! - Report correct captcha
9//! - Get spending stats
10//! - Push antigate variables
11//!
12//! ## Basic example for Control `get_balance` method
13//!
14//! ```no_run
15//! use rust_anticaptcha::control::Control;
16//!
17//! async fn run() {
18//!     let control_client = Control::new();
19//!     control_client.get_balance(&"API_KEY".to_string()).await;
20//! }
21//! ```
22//!
23//! # Notes
24//! Read more here:
25//!
26//! <https://anti-captcha.com/apidoc/methods/getBalance>
27//!
28//! <https://anti-captcha.com/apidoc/methods/getAppStats>
29//!
30//! <https://anti-captcha.com/apidoc/methods/getSpendingStats>
31//!
32use serde_json::{json, Value};
33
34use super::core::constants::BASE_REQUEST_URL;
35use super::core::enums::EnpPostfix;
36use super::core::request_interface::RequestInterface;
37
38/// Structure help processing additional AntiCaptcha methods
39///
40/// # Examples
41///
42/// ```
43/// use rust_anticaptcha::control::Control;
44///
45/// let control_client = Control::new();
46/// ```
47///
48/// # Notes
49/// Read more here:
50///
51/// <https://anti-captcha.com/apidoc>
52///
53pub struct Control {
54    request_interface: RequestInterface,
55}
56impl Default for Control {
57    fn default() -> Self {
58        Control::new()
59    }
60}
61impl Control {
62    /// Method init new Control struct with Web Requests Client
63    ///
64    /// # Examples
65    ///
66    /// ```
67    /// use rust_anticaptcha::control::Control;
68    ///
69    /// let control_client = Control::new();
70    /// ```
71    pub fn new() -> Self {
72        Control {
73            request_interface: RequestInterface::new(),
74        }
75    }
76
77    /// Method prepare and send control request to API
78    ///
79    /// # Arguments
80    /// `payload` - request JSON payload
81    /// `enp_postfix` - API URL postfix from `EnpPostfix`
82    ///
83    /// # Returns
84    /// Method return API response JSON
85    ///
86    async fn send_control_request(&self, payload: &Value, enp_postfix: &EnpPostfix) -> Value {
87        let req_url = BASE_REQUEST_URL.to_string() + &enp_postfix.to_string();
88        let result = self
89            .request_interface
90            .send_post_request(payload, &req_url)
91            .await
92            .unwrap();
93
94        if result.status().eq(&200) {
95            result.json().await.unwrap()
96        } else {
97            panic!(
98                "{}",
99                format!(
100                    "Invalid request to API, status code - {} response - {}",
101                    result.status(),
102                    result.text().await.unwrap()
103                )
104            )
105        }
106    }
107
108    /// Method request `getBalance` enp
109    ///
110    /// # Examples
111    ///
112    /// ```no_run
113    /// use rust_anticaptcha::control::Control;
114    ///
115    /// async fn run() {
116    ///     let control_client = Control::new();
117    ///     control_client.get_balance(&"API_KEY".to_string()).await;
118    /// }
119    /// ```
120    ///
121    /// # Notes
122    /// Read more here:
123    ///
124    /// <https://anti-captcha.com/apidoc/methods/getBalance>
125    pub async fn get_balance(&self, api_key: &String) -> Value {
126        let map = json!({"clientKey": api_key});
127
128        self.send_control_request(&map, &EnpPostfix::getBalance).await
129    }
130
131    /// Method request `getQueueStats ` enp
132    ///
133    /// # Examples
134    ///
135    /// ```no_run
136    /// use serde_json::json;
137    ///
138    /// use rust_anticaptcha::control::Control;
139    ///
140    /// async fn run() {
141    ///     let control_client = Control::new();
142    ///     let map = json!({"queueId": 6});
143    ///     let result = control_client.get_queue_stats(&map).await;
144    /// }
145    /// ```
146    ///
147    /// # Notes
148    /// Read more here:
149    ///
150    /// <https://anti-captcha.com/apidoc/methods/getQueueStats>
151    pub async fn get_queue_stats(&self, enp_payload: &Value) -> Value {
152        self.send_control_request(enp_payload, &EnpPostfix::getQueueStats)
153            .await
154    }
155
156    /// Method request `reportIncorrectImageCaptcha ` enp
157    ///
158    /// # Examples
159    ///
160    /// ```no_run
161    /// use serde_json::json;
162    ///
163    /// use rust_anticaptcha::control::Control;
164    ///
165    /// async fn run() {
166    ///     let control_client = Control::new();
167    ///     let map = json!({"clientKey": "API_KEY", "taskId": 12345});
168    ///     let result = control_client.report_incorrect_image(&map).await;
169    /// }
170    /// ```
171    ///
172    /// # Notes
173    /// Read more here:
174    ///
175    /// <https://anti-captcha.com/apidoc/methods/reportIncorrectImageCaptcha>
176    pub async fn report_incorrect_image(&self, enp_payload: &Value) -> Value {
177        self.send_control_request(enp_payload, &EnpPostfix::reportIncorrectImageCaptcha)
178            .await
179    }
180
181    /// Method request `reportIncorrectRecaptcha ` enp
182    ///
183    /// # Examples
184    ///
185    /// ```no_run
186    /// use serde_json::json;
187    ///
188    /// use rust_anticaptcha::control::Control;
189    ///
190    /// async fn run() {
191    ///     let control_client = Control::new();
192    ///     let map = json!({"clientKey": "API_KEY", "taskId": 12345});
193    ///     let result = control_client.report_incorrect_recaptcha(&map).await;
194    /// }
195    /// ```
196    ///
197    /// # Notes
198    /// Read more here:
199    ///
200    /// <https://anti-captcha.com/apidoc/methods/reportIncorrectRecaptcha>
201    pub async fn report_incorrect_recaptcha(&self, enp_payload: &Value) -> Value {
202        self.send_control_request(enp_payload, &EnpPostfix::reportIncorrectRecaptcha)
203            .await
204    }
205
206    /// Method request `reportCorrectRecaptcha ` enp
207    ///
208    /// # Examples
209    ///
210    /// ```no_run
211    /// use serde_json::json;
212    ///
213    /// use rust_anticaptcha::control::Control;
214    ///
215    /// async fn run() {
216    ///     let control_client = Control::new();
217    ///     let map = json!({"clientKey": "API_KEY", "taskId": 12345});
218    ///     control_client.report_correct_recaptcha(&map).await;
219    /// }
220    /// ```
221    ///
222    /// # Notes
223    /// Read more here:
224    ///
225    /// <https://anti-captcha.com/apidoc/methods/reportCorrectRecaptcha>
226    pub async fn report_correct_recaptcha(&self, enp_payload: &Value) -> Value {
227        self.send_control_request(enp_payload, &EnpPostfix::reportCorrectRecaptcha)
228            .await
229    }
230
231    /// Method request `reportIncorrectHcaptcha ` enp
232    ///
233    /// # Examples
234    ///
235    /// ```no_run
236    /// use serde_json::json;
237    ///
238    /// use rust_anticaptcha::control::Control;
239    ///
240    /// async fn run() {
241    ///     let control_client = Control::new();
242    ///     let map = json!({"clientKey": "API_KEY", "taskId": 12345});
243    ///     control_client.report_incorrect_hcaptcha(&map).await;
244    /// };
245    /// ```
246    ///
247    /// # Notes
248    /// Read more here:
249    ///
250    /// <https://anti-captcha.com/apidoc/methods/reportIncorrectHcaptcha>
251    pub async fn report_incorrect_hcaptcha(&self, enp_payload: &Value) -> Value {
252        self.send_control_request(enp_payload, &EnpPostfix::reportIncorrectHcaptcha)
253            .await
254    }
255
256    /// Method request `pushAntiGateVariable ` enp
257    ///
258    /// # Examples
259    ///
260    /// ```no_run
261    /// use serde_json::json;
262    ///
263    /// use rust_anticaptcha::control::Control;
264    ///
265    /// async fn run() {
266    ///     let control_client = Control::new();
267    ///     let map = json!({
268    ///                     "clientKey": "API_KEY",
269    ///                     "taskId": 12345,
270    ///                     "name": "my_late_variable",
271    ///                     "value": "some_value"
272    ///                 });
273    ///     control_client.push_antigate_var(&map).await;
274    /// }
275    /// ```
276    ///
277    /// # Notes
278    /// Read more here:
279    ///
280    /// <https://anti-captcha.com/apidoc/methods/pushAntiGateVariable>
281    pub async fn push_antigate_var(&self, enp_payload: &Value) -> Value {
282        self.send_control_request(enp_payload, &EnpPostfix::pushAntiGateVariable)
283            .await
284    }
285
286    /// Method request `getSpendingStats ` enp
287    ///
288    /// # Examples
289    ///
290    /// ```no_run
291    /// use serde_json::json;
292    ///
293    /// use rust_anticaptcha::control::Control;
294    ///
295    /// async fn run() {
296    ///     let control_client = Control::new();
297    ///     let map = json!({
298    ///                     "clientKey": "API_KEY",
299    ///                     "date": 1672185600,
300    ///                     "queue": "Recaptcha Proxyless"
301    ///                 });
302    ///     control_client.get_spending_stats(&map).await;
303    /// }
304    /// ```
305    ///
306    /// # Returns
307    /// This method grabs account spendings and task volume statistics for a 24 hour period.
308    ///
309    /// # Notes
310    /// Read more here:
311    ///
312    /// <https://anti-captcha.com/apidoc/methods/getSpendingStats>
313    pub async fn get_spending_stats(&self, enp_payload: &Value) -> Value {
314        self.send_control_request(enp_payload, &EnpPostfix::getSpendingStats)
315            .await
316    }
317
318    /// Method request `getAppStats ` enp
319    ///
320    /// # Examples
321    ///
322    /// ```no_run
323    /// use serde_json::json;
324    ///
325    /// use rust_anticaptcha::control::Control;
326    ///
327    /// async fn run() {
328    ///     let control_client = Control::new();
329    ///     let map = json!({
330    ///                     "clientKey": "API_KEY",
331    ///                     "softId": 867,
332    ///                     "mode": "money"
333    ///                 });
334    ///     control_client.get_app_stats(&map).await;
335    /// }
336    /// ```
337    ///
338    /// # Returns
339    /// This method retrieves daily statistics for your application,
340    /// which you register in Developer Center.
341    ///
342    /// # Notes
343    /// Read more here:
344    ///
345    /// <https://anti-captcha.com/apidoc/methods/getAppStats>
346    pub async fn get_app_stats(&self, enp_payload: &Value) -> Value {
347        self.send_control_request(enp_payload, &EnpPostfix::getAppStats)
348            .await
349    }
350}