rust_anticaptcha/token_captcha.rs
1//! # TokenCaptcha module
2//!
3//! Module contains struct and functions to solve Token captcha,
4//! like `ReCaptchaV2`, `ReCaptchaV3`, `FunCaptcha`, `GeeTest`, `Turnstile`.
5//! Token captcha - captcha solved by tokens.
6//!
7//! ## Examples
8//! ```no_run
9//! use serde_json::json;
10//!
11//! use rust_anticaptcha::core::enums::TokenTaskType;
12//! use rust_anticaptcha::token_captcha::TokenCaptcha;
13//!
14//! async fn run() {
15//! let map = json!({
16//! "websiteKey": "6LfD3PIbAAAAAJs_eEHvoOl75_83eXSqpPSRFJ_u",
17//! "websiteURL":"https://rucaptcha.com/demo/recaptcha-v2"
18//! });
19//!
20//! let mut image_to_text_client = TokenCaptcha::new("API_KEY".to_string());
21//! image_to_text_client.captcha_handler(TokenTaskType::RecaptchaV2TaskProxyless, &map).await;
22//! }
23//! ```
24
25use serde_json::Value;
26
27use super::core::captcha_interface::CaptchaInterface;
28use super::core::enums::TokenTaskType;
29
30/// Token captcha solving method - ReCaptchaV2, ReCaptchaV3, FunCaptcha, GeeTest, Turnstile.
31///
32/// # Examples
33/// For `RecaptchaV2TaskProxyless`.
34/// Other captcha types works same, you need check docs and set correct `task_payload`.
35/// ```no_run
36/// use serde_json::json;
37///
38/// use rust_anticaptcha::core::enums::TokenTaskType;
39/// use rust_anticaptcha::token_captcha::TokenCaptcha;
40///
41/// async fn run() {
42/// let map = json!({
43/// "websiteKey": "6LfD3PIbAAAAAJs_eEHvoOl75_83eXSqpPSRFJ_u",
44/// "websiteURL":"https://rucaptcha.com/demo/recaptcha-v2"
45/// });
46///
47/// let mut image_to_text_client = TokenCaptcha::new("API_KEY".to_string());
48/// image_to_text_client.captcha_handler(TokenTaskType::RecaptchaV2TaskProxyless, &map).await;
49/// }
50/// ```
51///
52/// # Notes
53/// Read more here:
54///
55/// <https://anti-captcha.com/apidoc/task-types/RecaptchaV2TaskProxyless>
56///
57/// <https://anti-captcha.com/apidoc/task-types/RecaptchaV3TaskProxyless>
58///
59/// <https://anti-captcha.com/apidoc/task-types/RecaptchaV2EnterpriseTaskProxyless>
60///
61/// <https://anti-captcha.com/apidoc/task-types/RecaptchaV3Enterprise>
62///
63/// <https://anti-captcha.com/apidoc/task-types/FunCaptchaTaskProxyless>
64///
65/// <https://anti-captcha.com/apidoc/task-types/GeeTestTaskProxyless>
66///
67/// <https://anti-captcha.com/apidoc/task-types/TurnstileTaskProxyless>
68///
69/// <https://anti-captcha.com/apidoc/task-types/AntiGateTask>
70///
71pub struct TokenCaptcha {
72 pub captcha_interface: CaptchaInterface,
73}
74impl TokenCaptcha {
75 /// Method init new TokenCaptcha struct with Captcha Interface
76 ///
77 /// # Arguments
78 /// `api_key` - Service API key
79 ///
80 /// # Examples
81 ///
82 /// ```
83 /// use rust_anticaptcha::token_captcha::TokenCaptcha;
84 ///
85 /// let image_to_text_client = TokenCaptcha::new("API_KEY".to_string());
86 /// ```
87 /// # Returns
88 /// Method return new `TokenCaptcha` instance
89 ///
90 pub fn new(api_key: String) -> Self {
91 TokenCaptcha {
92 captcha_interface: CaptchaInterface::new(api_key),
93 }
94 }
95
96 /// Method run captcha solving logic
97 ///
98 /// # Arguments
99 /// `captcha_type` - One of image captcha types from `TokenTaskType`
100 /// `task_payload` - JSON with captcha task payload
101 ///
102 /// # Examples
103 ///
104 /// ```no_run
105 /// use serde_json::json;
106 ///
107 /// use rust_anticaptcha::core::enums::TokenTaskType;
108 /// use rust_anticaptcha::token_captcha::TokenCaptcha;
109 ///
110 /// async fn run() {
111 /// let map = json!({
112 /// "websiteKey": "6LfD3PIbAAAAAJs_eEHvoOl75_83eXSqpPSRFJ_u",
113 /// "websiteURL":"https://rucaptcha.com/demo/recaptcha-v2"
114 /// });
115 ///
116 /// let mut image_to_text_client = TokenCaptcha::new("API_KEY".to_string());
117 /// image_to_text_client.captcha_handler(TokenTaskType::RecaptchaV2TaskProxyless, &map).await;
118 /// }
119 /// ```
120 pub async fn captcha_handler(
121 &mut self,
122 captcha_type: TokenTaskType,
123 task_payload: &Value,
124 ) -> Value {
125 self.captcha_interface
126 .solve_captcha(captcha_type, task_payload)
127 .await
128 }
129}