1use std::path::Path;
30use std::time::Duration;
31
32use serde_json::Value;
33
34use crate::error::{Error, Result};
35use crate::solution::Solution;
36use crate::tasks;
37
38macro_rules! forward {
40 ($( $(#[$meta:meta])* $name:ident ( $($arg:ident : $ty:ty),* ) -> $ret:ty; )*) => {
41 $(
42 $(#[$meta])*
43 pub fn $name(&self, $($arg: $ty),*) -> Result<$ret> {
44 self.runtime.block_on(self.inner.$name($($arg),*))
45 }
46 )*
47 };
48}
49
50#[derive(Debug)]
52pub struct Client {
53 inner: crate::Client,
54 runtime: tokio::runtime::Runtime,
55}
56
57impl Client {
58 pub fn new(api_key: impl Into<String>) -> Result<Self> {
62 Self::from_async(crate::Client::new(api_key))
63 }
64
65 pub fn from_async(inner: crate::Client) -> Result<Self> {
67 let runtime = tokio::runtime::Builder::new_current_thread()
68 .enable_all()
69 .build()
70 .map_err(|source| Error::Io {
71 path: "tokio runtime".into(),
72 source,
73 })?;
74
75 Ok(Self { inner, runtime })
76 }
77
78 pub fn inner(&self) -> &crate::Client {
80 &self.inner
81 }
82
83 pub fn set_api_key(&mut self, api_key: impl Into<String>) {
84 self.inner.set_api_key(api_key);
85 }
86
87 pub fn configure(&mut self, change: impl FnOnce(crate::Client) -> crate::Client) {
90 let current = self.inner.clone();
91 self.inner = change(current);
92 }
93
94 pub fn file_to_base64(path: impl AsRef<Path>) -> Result<String> {
96 crate::Client::file_to_base64(path)
97 }
98
99 pub fn to_base64(bytes: impl AsRef<[u8]>) -> String {
101 crate::Client::to_base64(bytes)
102 }
103
104 forward! {
105 get_balance() -> f64;
107 get_credits_balance() -> f64;
109
110 json_request(method: &str, payload: Value) -> Value;
112 create_task(task: Value) -> i64;
114 wait_for_result(task_id: i64) -> Solution;
116 solve_task(task: Value) -> Solution;
118
119 solve_image(base64_body: &str, settings: &tasks::ImageSettings) -> Solution;
120 solve_image_to_coordinates(base64_body: &str, settings: &tasks::ImageToCoordinates) -> Solution;
121 solve_recaptcha_v2(settings: &tasks::RecaptchaV2) -> Solution;
122 solve_recaptcha_v2_proxy_on(settings: &tasks::RecaptchaV2) -> Solution;
123 solve_recaptcha_v3(settings: &tasks::RecaptchaV3) -> Solution;
124 solve_hcaptcha(settings: &tasks::HCaptcha) -> Solution;
125 solve_hcaptcha_proxy_on(settings: &tasks::HCaptcha) -> Solution;
126 solve_funcaptcha(settings: &tasks::FunCaptcha) -> Solution;
127 solve_funcaptcha_proxy_on(settings: &tasks::FunCaptcha) -> Solution;
128 solve_geetest(settings: &tasks::GeeTest) -> Solution;
129 solve_geetest_proxy_on(settings: &tasks::GeeTest) -> Solution;
130 solve_turnstile(settings: &tasks::Turnstile) -> Solution;
131 solve_turnstile_proxy_on(settings: &tasks::Turnstile) -> Solution;
132 solve_prosopo(settings: &tasks::Prosopo) -> Solution;
133 solve_prosopo_proxy_on(settings: &tasks::Prosopo) -> Solution;
134 solve_friendly_captcha(settings: &tasks::FriendlyCaptcha) -> Solution;
135 solve_friendly_captcha_proxy_on(settings: &tasks::FriendlyCaptcha) -> Solution;
136 solve_altcha(settings: &tasks::Altcha) -> Solution;
137 solve_altcha_proxy_on(settings: &tasks::Altcha) -> Solution;
138 solve_amazon(settings: &tasks::Amazon) -> Solution;
139 solve_amazon_proxy_on(settings: &tasks::Amazon) -> Solution;
140 solve_antigate(settings: &tasks::AntiGate) -> Solution;
141 solve_antibot_cookie(settings: &tasks::AntiBotCookie) -> Solution;
142
143 report_incorrect_image_captcha(task_id: i64) -> ();
144 report_incorrect_recaptcha(task_id: i64) -> ();
145 report_correct_recaptcha(task_id: i64) -> ();
146 report_incorrect_hcaptcha(task_id: i64) -> ();
147 }
148
149 pub fn solve_image_file(
151 &self,
152 path: impl AsRef<Path>,
153 settings: &tasks::ImageSettings,
154 ) -> Result<Solution> {
155 self.runtime
156 .block_on(self.inner.solve_image_file(path, settings))
157 }
158
159 pub fn solve_image_to_coordinates_file(
161 &self,
162 path: impl AsRef<Path>,
163 settings: &tasks::ImageToCoordinates,
164 ) -> Result<Solution> {
165 self.runtime
166 .block_on(self.inner.solve_image_to_coordinates_file(path, settings))
167 }
168
169 pub fn set_max_waiting_time(&mut self, timeout: Duration) {
171 self.configure(|client| client.with_max_waiting_time(timeout));
172 }
173}
174
175#[cfg(test)]
176mod tests {
177 use super::*;
178
179 #[test]
180 fn empty_api_key_is_refused() {
181 let client = Client::from_async(crate::Client::new("").quiet()).unwrap();
182 let error = client.get_balance().unwrap_err();
183
184 assert!(matches!(error, Error::InvalidTask(_)));
185 }
186
187 #[test]
188 fn configure_replaces_the_inner_client() {
189 let mut client = Client::from_async(crate::Client::new("").quiet()).unwrap();
190 client.set_max_waiting_time(Duration::from_secs(42));
191 client.configure(|inner| inner.with_soft_id(1187));
192 client.set_api_key("KEY");
193
194 let error = client.solve_altcha(&crate::Altcha::default()).unwrap_err();
197
198 assert!(matches!(error, Error::InvalidTask(_)));
199 }
200}