fsolver/
request_models.rs

1use serde::Serialize;
2use std::collections::HashMap;
3
4pub type FlareSolverrRequestCookie = HashMap<String, String>;
5
6#[derive(Serialize)]
7pub struct FlareSolverBasicCommand<'a> {
8    pub cmd: &'a str,
9}
10
11impl FlareSolverBasicCommand<'_> {
12    pub const SESSIONS_LIST: FlareSolverBasicCommand<'static> = FlareSolverBasicCommand {
13        cmd: "sessions.list",
14    };
15}
16
17#[derive(Serialize)]
18pub struct FlareSolverrCreateSessionCommand<'a> {
19    pub cmd: &'static str,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub session: Option<&'a str>,
22}
23
24impl<'a> FlareSolverrCreateSessionCommand<'a> {
25    pub fn new(session: Option<&'a str>) -> Self {
26        FlareSolverrCreateSessionCommand {
27            cmd: "sessions.create",
28            session,
29        }
30    }
31}
32
33#[derive(Serialize)]
34pub struct FlareSolverrDestroySessionCommand<'a> {
35    pub cmd: &'static str,
36    pub session: &'a str,
37}
38
39impl<'a> FlareSolverrDestroySessionCommand<'a> {
40    pub fn new(session: &'a str) -> Self {
41        FlareSolverrDestroySessionCommand {
42            cmd: "sessions.destroy",
43            session,
44        }
45    }
46}
47
48#[derive(Serialize)]
49pub struct Proxy {
50    /// You must include the proxy schema in the URL: http://, socks4:// or socks5://.
51    pub url: String,
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub username: Option<String>,
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub password: Option<String>,
56}
57
58#[derive(Serialize)]
59pub struct RequestProxy {
60    /// You must include the proxy schema in the URL: http://, socks4:// or socks5://.
61    pub url: String,
62}
63
64#[derive(Serialize)]
65pub struct SessionsCreate {
66    /// The session ID that you want to be assigned to the instance. If isn't set a random UUID will be assigned.
67    #[serde(skip_serializing_if = "Option::is_none")]
68    pub session: Option<String>,
69    #[serde(skip_serializing_if = "Option::is_none")]
70    pub proxy: Option<Proxy>,
71}
72
73#[derive(Serialize)]
74#[serde(rename_all = "camelCase")]
75pub struct RequestGet {
76    cmd: &'static str,
77
78    /// The URL to request.
79    pub url: String,
80    /// Will send the request from and existing browser instance.
81    /// If one is not sent it will create a temporary instance
82    /// that will be destroyed immediately after the request is completed.
83    #[serde(skip_serializing_if = "Option::is_none")]
84    pub session: Option<String>,
85    /// FlareSolverr will automatically rotate
86    /// expired sessions based on the TTL provided in minutes.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub session_ttl_minutes: Option<u64>,
89    /// Default value 60000. Max timeout to solve the challenge in milliseconds.
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub max_timeout: Option<u32>,
92    /// Cookies to be sent with the request. Will be used by the headless browser.
93    #[serde(skip_serializing_if = "Option::is_none")]
94    pub cookies: Option<Vec<FlareSolverrRequestCookie>>,
95    /// Only returns the cookies.
96    ///
97    /// Response data, headers and other parts of the response are removed.
98    #[serde(skip_serializing_if = "Option::is_none")]
99    pub return_only_cookies: Option<bool>,
100    #[serde(skip_serializing_if = "Option::is_none")]
101    pub proxy: Option<RequestProxy>,
102}
103
104impl RequestGet {
105    // New with all fields
106    pub fn new(
107        url: String,
108        session: Option<String>,
109        session_ttl_minutes: Option<u64>,
110        max_timeout: Option<u32>,
111        cookies: Option<Vec<FlareSolverrRequestCookie>>,
112        return_only_cookies: Option<bool>,
113        proxy: Option<RequestProxy>,
114    ) -> Self {
115        Self {
116            cmd: "request.get",
117            url,
118            session,
119            session_ttl_minutes,
120            max_timeout,
121            cookies,
122            return_only_cookies,
123            proxy,
124        }
125    }
126
127    pub fn new_empty(url: String) -> Self {
128        RequestGet {
129            cmd: "request.get",
130            url,
131            session: None,
132            session_ttl_minutes: None,
133            max_timeout: None,
134            cookies: None,
135            return_only_cookies: None,
136            proxy: None,
137        }
138    }
139}
140
141#[derive(Serialize)]
142#[serde(rename_all = "camelCase")]
143pub struct RequestPost {
144    cmd: &'static str,
145
146    /// The URL to request.
147    pub url: String,
148    /// Will send the request from and existing browser instance.
149    /// If one is not sent it will create a temporary instance
150    /// that will be destroyed immediately after the request is completed.
151    #[serde(skip_serializing_if = "Option::is_none")]
152    pub session: Option<String>,
153    /// FlareSolverr will automatically rotate
154    /// expired sessions based on the TTL provided in minutes.
155    #[serde(skip_serializing_if = "Option::is_none")]
156    pub session_ttl_minutes: Option<u32>,
157    /// Default value 60000. Max timeout to solve the challenge in milliseconds.
158    #[serde(skip_serializing_if = "Option::is_none")]
159    pub max_timeout: Option<u32>,
160    /// Cookies to be sent with the request. Will be used by the headless browser.
161    #[serde(skip_serializing_if = "Option::is_none")]
162    pub cookies: Option<Vec<HashMap<String, String>>>,
163    /// Only returns the cookies.
164    ///
165    /// Response data, headers and other parts of the response are removed.
166    #[serde(skip_serializing_if = "Option::is_none")]
167    pub return_only_cookies: Option<bool>,
168    #[serde(skip_serializing_if = "Option::is_none")]
169    pub proxy: Option<RequestProxy>,
170    /// The data to be sent in the request body.
171    ///
172    /// Must be a string with `application/x-www-form-urlencoded`. Eg: `a=b&c=d`
173    pub post_data: String,
174}
175
176impl RequestPost {
177    // New with all fields
178    pub fn new(
179        url: String,
180        session: Option<String>,
181        session_ttl_minutes: Option<u32>,
182        max_timeout: Option<u32>,
183        cookies: Option<Vec<HashMap<String, String>>>,
184        return_only_cookies: Option<bool>,
185        proxy: Option<RequestProxy>,
186        post_data: String,
187    ) -> Self {
188        Self {
189            cmd: "request.post",
190            url,
191            session,
192            session_ttl_minutes,
193            max_timeout,
194            cookies,
195            return_only_cookies,
196            proxy,
197            post_data,
198        }
199    }
200
201    pub fn new_empty(url: String, post_data: String) -> Self {
202        RequestPost {
203            cmd: "request.post",
204            url,
205            session: None,
206            session_ttl_minutes: None,
207            max_timeout: None,
208            cookies: None,
209            return_only_cookies: None,
210            proxy: None,
211            post_data,
212        }
213    }
214}