spider_client/shapes/request.rs
1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4/// Structure representing the Chunking algorithm dictionary.
5#[derive(Debug, Deserialize, Serialize, Clone)]
6pub struct ChunkingAlgDict {
7 /// The chunking algorithm to use, defined as a specific type.
8 pub r#type: ChunkingType,
9 /// The amount to chunk by.
10 pub value: i32,
11}
12
13// The nested structures
14#[derive(Serialize, Deserialize, Debug, Clone, Default)]
15pub struct Timeout {
16 /// The seconds up to 60.
17 pub secs: u64,
18 /// The nanoseconds.
19 pub nanos: u32,
20}
21
22#[derive(Serialize, Deserialize, Debug, Clone)]
23pub struct IdleNetwork {
24 /// The timeout to wait until.
25 pub timeout: Timeout,
26}
27
28
29/// Represents various web automation actions.
30#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
31pub enum WebAutomation {
32 /// Runs custom JavaScript code.
33 Evaluate(String),
34 /// Clicks on an element.
35 Click(String),
36 /// Clicks on all elements.
37 ClickAll(String),
38 /// Clicks on all elements.
39 ClickAllClickable(),
40 /// Clicks at the position x and y coordinates.
41 ClickPoint {
42 /// The horizontal (X) coordinate.
43 x: f64,
44 /// The vertical (Y) coordinate.
45 y: f64,
46 },
47 Type {
48 /// The value to type.
49 value: String,
50 /// The click modifier.
51 modifier: Option<i64>
52 },
53 /// Waits for a fixed duration in milliseconds.
54 Wait(u64),
55 /// Waits for the next navigation event.
56 WaitForNavigation,
57 /// Wait for dom updates to stop.
58 WaitForDom {
59 /// The selector of the element to wait for updates.
60 selector: Option<String>,
61 /// The timeout to wait for in ms.
62 timeout: u32,
63 },
64 /// Waits for an element to appear.
65 WaitFor(String),
66 /// Waits for an element to appear with a timeout.
67 WaitForWithTimeout {
68 /// The selector of the element to wait for updates.
69 selector: String,
70 /// The timeout to wait for in ms.
71 timeout: u64,
72 },
73 /// Waits for an element to appear and then clicks on it.
74 WaitForAndClick(String),
75 /// Scrolls the screen in the horizontal axis by a specified amount in pixels.
76 ScrollX(i32),
77 /// Scrolls the screen in the vertical axis by a specified amount in pixels.
78 ScrollY(i32),
79 /// Fills an input element with a specified value.
80 Fill {
81 /// The selector of the input element to fill.
82 selector: String,
83 /// The value to fill the input element with.
84 value: String,
85 },
86 /// Scrolls the page until the end.
87 InfiniteScroll(u32),
88 /// Perform a screenshot on the page - fullscreen and omit background for params.
89 Screenshot {
90 /// Take a full page screenshot.
91 full_page: bool,
92 /// Omit the background.
93 omit_background: bool,
94 /// The output file to store the screenshot.
95 output: String,
96 },
97 /// Only continue to the next automation if the prior step was valid. Use this intermediate after a step to break out of the chain.
98 ValidateChain,
99}
100
101#[derive(Default, Serialize, Deserialize, Debug, Clone)]
102#[serde(tag = "type", rename_all = "PascalCase")]
103pub enum RedirectPolicy {
104 Loose,
105 #[default]
106 Strict,
107}
108
109pub type WebAutomationMap = std::collections::HashMap<String, Vec<WebAutomation>>;
110pub type ExecutionScriptsMap = std::collections::HashMap<String, String>;
111
112#[derive(Serialize, Deserialize, Debug, Clone)]
113pub struct Selector {
114 /// The timeout to wait until.
115 pub timeout: Timeout,
116 /// The selector to wait for.
117 pub selector: String,
118}
119
120#[derive(Serialize, Deserialize, Debug, Clone, Default)]
121pub struct Delay {
122 /// The timeout to wait until.
123 pub timeout: Timeout,
124}
125
126/// Default as true.
127fn default_some_true() -> Option<bool> {
128 Some(true)
129}
130
131#[derive(Serialize, Deserialize, Debug, Clone, Default)]
132pub struct WaitFor {
133 /// Wait until idle networks with a timeout of idleness.
134 pub idle_network: Option<IdleNetwork>,
135 /// Wait until network to be idle with a max timeout.
136 pub idle_network0: Option<IdleNetwork>,
137 /// Wait until network to almost be idle with a max timeout.
138 pub almost_idle_network0: Option<IdleNetwork>,
139 /// Wait until a selector exist. Can determine if a selector exist after executing all js and network events.
140 pub selector: Option<Selector>,
141 /// Wait for the dom to update
142 pub dom: Option<Selector>,
143 /// Wait until a hard delay.
144 pub delay: Option<Delay>,
145 /// Wait until page navigation happen. Default is true.
146 #[serde(default = "default_some_true")]
147 pub page_navigations: Option<bool>,
148}
149
150/// Query request to get a document.
151#[derive(Serialize, Deserialize, Debug, Clone, Default)]
152pub struct QueryRequest {
153 /// The exact website url.
154 pub url: Option<String>,
155 /// The website domain.
156 pub domain: Option<String>,
157 /// The path of the resource.
158 pub pathname: Option<String>,
159}
160
161/// Enum representing different types of Chunking.
162#[derive(Default, Debug, Deserialize, Serialize, Clone)]
163#[serde(rename_all = "lowercase")]
164pub enum ChunkingType {
165 #[default]
166 /// By the word count.
167 ByWords,
168 /// By the line count.
169 ByLines,
170 /// By the char length.
171 ByCharacterLength,
172 /// By sentence.
173 BySentence,
174}
175
176#[derive(Default, Debug, Deserialize, Serialize, Clone)]
177/// View port handling for chrome.
178pub struct Viewport {
179 /// Device screen Width
180 pub width: u32,
181 /// Device screen size
182 pub height: u32,
183 /// Device scale factor
184 pub device_scale_factor: Option<f64>,
185 /// Emulating Mobile?
186 pub emulating_mobile: bool,
187 /// Use landscape mode instead of portrait.
188 pub is_landscape: bool,
189 /// Touch screen device?
190 pub has_touch: bool,
191}
192
193// Define the CSSSelector struct
194#[derive(Debug, Clone, Default, Deserialize, Serialize)]
195pub struct CSSSelector {
196 /// The name of the selector group
197 pub name: String,
198 /// A vector of CSS selectors
199 pub selectors: Vec<String>,
200}
201
202// Define the CSSExtractionMap type
203pub type CSSExtractionMap = HashMap<String, Vec<CSSSelector>>;
204
205/// Represents the settings for a webhook configuration
206#[derive(Debug, Default, Deserialize, Serialize, Clone)]
207pub struct WebhookSettings {
208 /// The destination where the webhook information will be sent
209 destination: String,
210 /// Trigger an action when all credits are depleted
211 on_credits_depleted: bool,
212 /// Trigger an action when half of the credits are depleted
213 on_credits_half_depleted: bool,
214 /// Trigger an action on a website status update event
215 on_website_status: bool,
216 /// Send information about a new page find (such as links and bytes)
217 on_find: bool,
218 /// Handle the metadata of a found page
219 on_find_metadata: bool,
220}
221
222/// Proxy pool selection for outbound request routing.
223/// Choose a pool based on your use case (e.g., stealth, speed, or stability).
224///
225/// - 'residential' → cost-effective entry-level residential pool
226/// - 'residential_fast' → faster residential pool for higher throughput
227/// - 'residential_static' → static residential IPs, rotated daily
228/// - 'residential_premium' → low-latency premium IPs
229/// - 'residential_core' → balanced plan (quality vs. cost)
230/// - 'residential_plus' → largest and highest quality core pool
231/// - 'mobile' → 4G/5G mobile proxies for maximum evasion
232/// - 'isp' → ISP-grade datacenters
233#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, Hash)]
234pub enum ProxyType {
235 /// Cost-effective entry-level residential pool.
236 #[serde(rename = "residential")]
237 Residential,
238 /// 4G / 5G mobile proxies for maximum stealth and evasion.
239 #[serde(rename = "mobile")]
240 Mobile,
241 /// ISP-grade residential routing (alias: `datacenter`).
242 #[serde(rename = "isp", alias = "datacenter")]
243 #[default]
244 Isp
245}
246
247/// List of proxies.
248pub const PROXY_TYPE_LIST: [ProxyType; 3] = [
249 ProxyType::Residential,
250 ProxyType::Isp,
251 ProxyType::Mobile
252];
253
254impl ProxyType {
255 /// Get the canonical string representation of the proxy type.
256 pub fn as_str(&self) -> &'static str {
257 match self {
258 ProxyType::Residential => "residential",
259 ProxyType::Mobile => "mobile",
260 ProxyType::Isp => "isp"
261 }
262 }
263}
264
265/// Send multiple return formats.
266#[derive(Debug, Deserialize, Serialize, Clone)]
267#[serde(untagged)]
268pub enum ReturnFormatHandling {
269 /// A single return item.
270 Single(ReturnFormat),
271 /// Multiple return formats.
272 Multi(std::collections::HashSet<ReturnFormat>),
273}
274
275impl Default for ReturnFormatHandling {
276 fn default() -> ReturnFormatHandling {
277 ReturnFormatHandling::Single(ReturnFormat::Raw)
278 }
279}
280
281#[derive(Debug, Default, Deserialize, Serialize, Clone)]
282pub struct EventTracker {
283 /// The responses received.
284 pub responses: Option<bool>,
285 /// The request sent.
286 pub requests: Option<bool>,
287 /// Track the automation events with data changes and screenshots.
288 pub automation: Option<bool>,
289}
290
291/// Structure representing request parameters.
292#[derive(Debug, Default, Deserialize, Serialize, Clone)]
293pub struct RequestParams {
294 #[serde(default)]
295 /// The URL to be crawled.
296 pub url: Option<String>,
297 #[serde(default)]
298 /// The type of request to be made.
299 pub request: Option<RequestType>,
300 #[serde(default)]
301 /// The maximum number of pages the crawler should visit.
302 pub limit: Option<u32>,
303 #[serde(default)]
304 /// The format in which the result should be returned.
305 pub return_format: Option<ReturnFormatHandling>,
306 /// The country code for request
307 pub country_code: Option<String>,
308 #[serde(default)]
309 /// Specifies whether to only visit the top-level domain.
310 pub tld: Option<bool>,
311 #[serde(default)]
312 /// The depth of the crawl.
313 pub depth: Option<u32>,
314 #[serde(default)]
315 /// Specifies whether the request should be cached.
316 pub cache: Option<bool>,
317 #[serde(default)]
318 /// Perform an infinite scroll on the page as new content arises. The request param also needs to be set to 'chrome' or 'smart'.
319 pub scroll: Option<u32>,
320 #[serde(default)]
321 /// The budget for various resources.
322 pub budget: Option<HashMap<String, u32>>,
323 #[serde(default)]
324 /// The blacklist routes to ignore. This can be a Regex string pattern.
325 pub blacklist: Option<Vec<String>>,
326 #[serde(default)]
327 /// The whitelist routes to only crawl. This can be a Regex string pattern and used with black_listing.
328 pub whitelist: Option<Vec<String>>,
329 #[serde(default)]
330 /// The locale to be used during the crawl.
331 pub locale: Option<String>,
332 #[serde(default)]
333 /// The cookies to be set for the request, formatted as a single string.
334 pub cookies: Option<String>,
335 #[serde(default)]
336 /// Specifies whether to use stealth techniques to avoid detection.
337 pub stealth: Option<bool>,
338 #[serde(default)]
339 /// The headers to be used for the request.
340 pub headers: Option<HashMap<String, String>>,
341 #[serde(default)]
342 /// Specifies whether to send data via webhooks.
343 pub webhooks: Option<WebhookSettings>,
344 #[serde(default)]
345 /// Specifies whether to include metadata in the response.
346 pub metadata: Option<bool>,
347 #[serde(default)]
348 /// The dimensions of the viewport.
349 pub viewport: Option<Viewport>,
350 #[serde(default)]
351 /// The encoding to be used for the request.
352 pub encoding: Option<String>,
353 #[serde(default)]
354 /// Specifies whether to include subdomains in the crawl.
355 pub subdomains: Option<bool>,
356 #[serde(default)]
357 /// The user agent string to be used for the request.
358 pub user_agent: Option<String>,
359 #[serde(default)]
360 /// Specifies whether to use fingerprinting protection.
361 pub fingerprint: Option<bool>,
362 #[serde(default)]
363 /// Specifies whether to perform the request without using storage.
364 pub storageless: Option<bool>,
365 #[serde(default)]
366 /// Specifies whether readability optimizations should be applied.
367 pub readability: Option<bool>,
368 #[serde(default)]
369 /// Specifies whether to use a proxy for the request. [Deprecated]: use the 'proxy' param instead.
370 pub proxy_enabled: Option<bool>,
371 #[serde(default)]
372 /// Specifies whether to respect the site's robots.txt file.
373 pub respect_robots: Option<bool>,
374 #[serde(default)]
375 /// CSS selector to be used to filter the content.
376 pub root_selector: Option<String>,
377 #[serde(default)]
378 /// Specifies whether to load all resources of the crawl target.
379 pub full_resources: Option<bool>,
380 #[serde(default)]
381 /// The text string to extract data from.
382 pub text: Option<String>,
383 #[serde(default)]
384 /// Specifies whether to use the sitemap links.
385 pub sitemap: Option<bool>,
386 #[serde(default)]
387 /// External domains to include the crawl.
388 pub external_domains: Option<Vec<String>>,
389 #[serde(default)]
390 /// Returns the OpenAI embeddings for the title and description. Other values, such as keywords, may also be included. Requires the `metadata` parameter to be set to `true`.
391 pub return_embeddings: Option<bool>,
392 #[serde(default)]
393 /// Returns the HTTP response headers.
394 pub return_headers: Option<bool>,
395 #[serde(default)]
396 /// Returns the link(s) found on the page that match the crawler query.
397 pub return_page_links: Option<bool>,
398 #[serde(default)]
399 /// Returns the HTTP response cookies.
400 pub return_cookies: Option<bool>,
401 #[serde(default)]
402 /// The timeout for the request, in seconds.
403 pub request_timeout: Option<u8>,
404 #[serde(default)]
405 /// Specifies whether to run the request in the background.
406 pub run_in_background: Option<bool>,
407 #[serde(default)]
408 /// Specifies whether to skip configuration checks.
409 pub skip_config_checks: Option<bool>,
410 #[serde(default)]
411 /// Use CSS query selectors to scrape contents from the web page. Set the paths and the CSS extraction object map to perform extractions per path or page.
412 pub css_extraction_map: Option<CSSExtractionMap>,
413 #[serde(default)]
414 /// The chunking algorithm to use.
415 pub chunking_alg: Option<ChunkingAlgDict>,
416 #[serde(default)]
417 /// Disable request interception when running 'request' as 'chrome' or 'smart'. This can help when the page uses 3rd party or external scripts to load content.
418 pub disable_intercept: Option<bool>,
419 #[serde(default)]
420 /// The wait for events on the page. You need to make your `request` `chrome` or `smart`.
421 pub wait_for: Option<WaitFor>,
422 #[serde(default)]
423 /// Perform custom Javascript tasks on a url or url path. You need to make your `request` `chrome` or `smart`
424 pub execution_scripts: Option<ExecutionScriptsMap>,
425 #[serde(default)]
426 /// Perform web automated tasks on a url or url path. You need to make your `request` `chrome` or `smart`
427 pub automation_scripts: Option<WebAutomationMap>,
428 #[serde(default)]
429 /// The redirect policy for HTTP request. Set the value to Loose to allow all.
430 pub redirect_policy: Option<RedirectPolicy>,
431 #[serde(default)]
432 /// Track the request sent and responses received for `chrome` or `smart`. The responses will track the bytes used and the requests will have the monotime sent.
433 pub event_tracker: Option<EventTracker>,
434 #[serde(default)]
435 /// The timeout to stop the crawl.
436 pub crawl_timeout: Option<Timeout>,
437 #[serde(default)]
438 /// Evaluates given script in every frame upon creation (before loading frame's scripts).
439 pub evaluate_on_new_document: Option<Box<String>>,
440 #[serde(default)]
441 /// Runs the request using lite_mode:Lite mode reduces data transfer costs by 50%, with trade-offs in speed, accuracy,
442 /// geo-targeting, and reliability. It’s best suited for non-urgent data collection or when
443 /// targeting websites with minimal anti-bot protections.
444 pub lite_mode: Option<bool>,
445 #[serde(default)]
446 /// The proxy to use for request.
447 pub proxy: Option<ProxyType>,
448 #[serde(default)]
449 /// Use a remote proxy at ~50% reduced cost for file downloads.
450 /// This requires a user-supplied static IP proxy endpoint.
451 pub remote_proxy: Option<String>,
452 #[serde(default)]
453 /// Set the maximum number of credits to use per page.
454 /// Credits are measured in decimal units, where 10,000 credits equal one dollar (100 credits per penny).
455 /// Credit limiting only applies to request that are Javascript rendered using smart_mode or chrome for the 'request' type.
456 pub max_credits_per_page: Option<f64>,
457}
458
459
460#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
461#[serde(rename_all = "lowercase")]
462pub enum TBS {
463 #[serde(rename = "qdr:h")]
464 PastHour,
465 #[serde(rename = "qdr:d")]
466 Past24Hours,
467 #[serde(rename = "qdr:w")]
468 PastWeek,
469 #[serde(rename = "qdr:m")]
470 PastMonth,
471 #[serde(rename = "qdr:y")]
472 PastYear,
473}
474
475/// The engine to use.
476#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
477pub enum Engine {
478 /// Google
479 Google,
480 /// Brave
481 Brave,
482 /// All
483 #[default]
484 All
485}
486
487/// The structure representing request parameters for a search request.
488#[derive(Debug, Default, Deserialize, Serialize, Clone)]
489pub struct SearchRequestParams {
490 /// The base request parameters.
491 #[serde(default, flatten)]
492 pub base: RequestParams,
493 // The search request.
494 pub search: String,
495 /// The search limit.
496 pub search_limit: Option<u32>,
497 // Fetch the page content. Defaults to true.
498 pub fetch_page_content: Option<bool>,
499 /// The search location of the request
500 pub location: Option<String>,
501 /// The country code of the request
502 pub country: Option<crate::shapes::country_codes::CountryCode>,
503 /// The language code of the request.
504 pub language: Option<String>,
505 /// The number of search results
506 pub num: Option<u32>,
507 /// The time period range.
508 pub tbs: Option<TBS>,
509 /// The page of the search results.
510 pub page: Option<u32>,
511 /// The websites limit if a list is sent from text or urls comma split. This helps automatic configuration of the system.
512 pub website_limit: Option<u32>,
513 /// Prioritize speed over output quantity.
514 pub quick_search: Option<bool>,
515 /// Auto paginate pages ( up to 100 pages ).
516 pub auto_pagination: Option<bool>,
517 /// The search engine to use.
518 pub engine: Option<Engine>
519}
520
521/// Structure representing request parameters for transforming files.
522#[derive(Debug, Default, Deserialize, Serialize, Clone)]
523pub struct TransformParams {
524 #[serde(default)]
525 /// The format in which the result should be returned.
526 pub return_format: Option<ReturnFormat>,
527 #[serde(default)]
528 /// Specifies whether readability optimizations should be applied.
529 pub readability: Option<bool>,
530 #[serde(default)]
531 /// Clean the markdown or text for AI.
532 pub clean: Option<bool>,
533 #[serde(default)]
534 /// Clean the markdown or text for AI removing footers, navigation, and more.
535 pub clean_full: Option<bool>,
536 /// The data being transformed.
537 pub data: Vec<Resource>,
538}
539
540#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
541/// Transformation resource to use.
542pub struct Resource {
543 #[serde(default)]
544 /// the html to transform
545 pub html: Option<bytes::Bytes>,
546 #[serde(default)]
547 /// the content to transform
548 pub content: Option<bytes::Bytes>,
549 #[serde(default)]
550 /// the url of the html incase of readability to improve transformations.
551 pub url: Option<String>,
552 #[serde(default)]
553 /// the language of the resource.
554 pub lang: Option<String>,
555}
556
557/// the request type to perform
558#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
559#[serde(rename_all = "lowercase")]
560pub enum RequestType {
561 /// Default HTTP request
562 Http,
563 /// Chrome browser rendering
564 Chrome,
565 #[default]
566 /// Smart mode defaulting to HTTP and using Chrome when needed.
567 SmartMode,
568}
569
570/// Enum representing different return formats.
571#[derive(Default, Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Hash)]
572#[serde(rename_all = "lowercase")]
573pub enum ReturnFormat {
574 #[default]
575 /// The default return format of the resource.
576 Raw,
577 /// Return the response as Markdown.
578 Markdown,
579 /// Return the response as Commonmark.
580 Commonmark,
581 /// Return the response as Html2text.
582 Html2text,
583 /// Return the response as Text.
584 Text,
585 /// Returns a screenshot as Base64Url
586 Screenshot,
587 /// Return the response as XML.
588 Xml,
589 /// Return the response as Bytes.
590 Bytes,
591}