rust_anticaptcha/core/
request_interface.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
use reqwest::Response;
use serde_json::Value;

use super::constants::BASE_REQUEST_URL;
use super::enums::EnpPostfix;
use super::structs::{CreateTaskRequest, ResultTaskRequest};

/// Structure-interface to send different network requests
pub struct RequestInterface {
    request_client: reqwest::Client,
}
impl Default for RequestInterface {
    fn default() -> Self {
        RequestInterface::new()
    }
}

impl RequestInterface {
    /// New `RequestInterface` instance creation
    pub fn new() -> Self {
        RequestInterface {
            request_client: reqwest::Client::new(),
        }
    }

    /// Function send GET request to URL and return response or string error
    pub async fn send_get_request(&self, url: &String) -> Result<Response, String> {
        let response = self.request_client.get(url).send().await.unwrap();

        if response.status().eq(&200) {
            Ok(response)
        } else {
            Err(format!(
                "Invalid request to {}, status code - {} response - {}",
                url,
                response.status(),
                response.text().await.unwrap()
            ))
        }
    }

    /// Function send POST request to URL and return response or string error
    pub async fn send_post_request(
        &self,
        payload: &Value,
        url: &String,
    ) -> Result<Response, String> {
        let response = self.request_client.post(url).json(payload).send().await.unwrap();

        if response.status().eq(&200) {
            Ok(response)
        } else {
            Err(format!(
                "Invalid request  to {}, status code - {} response - {}",
                url,
                response.status(),
                response.text().await.unwrap()
            ))
        }
    }

    /// Function send `createTask` request to URL and return JSON response or string error
    pub async fn send_create_task_request(
        &self,
        payload: &CreateTaskRequest,
        enp_postfix: &EnpPostfix,
    ) -> Result<Value, String> {
        let req_url = BASE_REQUEST_URL.to_string() + &enp_postfix.to_string();

        let response = self
            .request_client
            .post(&req_url)
            .json(payload)
            .send()
            .await
            .unwrap();

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

    /// Function send `getTaskResult ` request to URL and return JSON response or string error
    pub async fn send_get_result_request(
        &self,
        payload: &ResultTaskRequest,
        enp_postfix: &EnpPostfix,
    ) -> Result<Value, String> {
        let req_url = BASE_REQUEST_URL.to_string() + &enp_postfix.to_string();

        let response = self
            .request_client
            .post(&req_url)
            .json(payload)
            .send()
            .await
            .unwrap();

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