Skip to main content

ezcapsolver/async_client/
solvers.rs

1use super::AsyncEzCapSolverClient;
2use crate::{
3    AkamaiSbsdTask, AkamaiWebTask, Cloudflare5sTask, CloudflareTurnstileTask, DataDomeTagsTask,
4    DataDomeTask, FunCaptchaClassificationTask, FunCaptchaTask, HcaptchaClassificationTask,
5    HcaptchaTask, IncapsulaTask, PerimeterXTask, RecaptchaV2ClassificationTask, RecaptchaV2Task,
6    RecaptchaV3Task, Result, Solved, TaskType, TlsForwardTask,
7    task_solvers::{decode_sync_result, for_each_task_solver},
8};
9
10/// Two methods per task type, one per execution mode.
11///
12/// `solve_*` creates the task and polls for its result; `sync_solve_*` runs the
13/// same task through the synchronous endpoint, which answers on the creating
14/// request. Both take the same request model and return the same [`Solved<T>`].
15///
16/// The synchronous name is concatenated from the polling one, so the task table
17/// stays the only place a method name is written down.
18macro_rules! define_solver {
19    ($mode:ident, $name:ident, $task:ty, $task_type:ident, $solution:ty) => {
20        /// Creates the task, polls until it finishes, and returns its decoded solution.
21        ///
22        /// The `sync_`-prefixed method runs the same type through the
23        /// synchronous endpoint instead.
24        pub async fn $name(&self, task: &$task) -> Result<Solved<$solution>> {
25            let solved = self.solve(TaskType::$task_type, task).await?;
26            let solution = solved.solution.deserialize()?;
27            Ok(Solved {
28                task_id: solved.task_id,
29                request_id: solved.request_id,
30                solution,
31                raw: solved.raw,
32            })
33        }
34
35        pastey::paste! {
36            /// Runs the task through the synchronous endpoint and returns its
37            /// decoded solution.
38            ///
39            /// [`Solved::task_id`] carries the identifier this endpoint assigns
40            /// and returns alongside the result.
41            ///
42            /// Whether a given type is accepted on this endpoint is the
43            /// service's decision; a type it does not serve is rejected with
44            /// `ERROR_TASK_TYPE_NOT_ALLOWED` before anything is billed.
45            pub async fn [<sync_ $name>](&self, task: &$task) -> Result<Solved<$solution>> {
46                let result = self.create_sync_task(TaskType::$task_type, task).await?;
47                decode_sync_result(result)
48            }
49        }
50    };
51}
52
53impl AsyncEzCapSolverClient {
54    for_each_task_solver!(define_solver);
55}