scrapeunblocker 0.20.0

Official Rust client for the ScrapeUnblocker API - scrape any page past Cloudflare, DataDome, PerimeterX and Akamai as HTML, Markdown or parsed JSON.
Documentation
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
//! The async [`Client`], its builder, and the core endpoints.

use std::fmt;
use std::sync::Arc;
use std::time::Duration;

use reqwest::header::{HeaderMap, HeaderValue, ACCEPT};
use reqwest::Url;
use serde_json::Value;

use crate::error::{api_error, Error, Result};
use crate::models::{CapturedPage, PageResult, ParsedPage, Step};
use crate::request::{executable, Call, Decode};

/// The production API host.
pub const DEFAULT_BASE_URL: &str = "https://api.scrapeunblocker.com";
/// Per-request timeout. Rendering heavy, protected pages can take a while.
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(180);
/// Retries for transient failures (HTTP 429/502/503/504 and network errors).
pub const DEFAULT_MAX_RETRIES: u32 = 2;
/// The environment variable the API key is read from when none is given.
pub const API_KEY_ENV: &str = "SCRAPEUNBLOCKER_KEY";
/// This crate's version, sent in the `User-Agent` header.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

const API_KEY_HEADER: &str = "x-scrapeunblocker-key";

/// Status codes worth retrying: concurrency limit, transient upstream outage
/// and gateway errors. A 400/401/402/403 is deterministic - retrying only
/// wastes time, and a 402 lifts on a billing change, not on another attempt.
const RETRYABLE_STATUS: [u16; 4] = [429, 502, 503, 504];

/// Client for the ScrapeUnblocker API.
///
/// Cheap to clone: clones share one connection pool. Every endpoint method
/// returns a request builder; set options on it and `.await` it.
///
/// ```no_run
/// # async fn run() -> scrapeunblocker::Result<()> {
/// use scrapeunblocker::Client;
///
/// let su = Client::new("YOUR_API_KEY")?; // or Client::from_env()?
/// let html = su.get_page_source("https://example.com").await?;
/// let results = su.amazon_search("wireless headphones").marketplace("amazon.de").await?;
/// # Ok(()) }
/// ```
#[derive(Clone)]
pub struct Client {
    inner: Arc<Inner>,
}

struct Inner {
    http: reqwest::Client,
    base_url: String,
    timeout: Duration,
    max_retries: u32,
}

impl fmt::Debug for Client {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Client")
            .field("base_url", &self.inner.base_url)
            .field("timeout", &self.inner.timeout)
            .field("max_retries", &self.inner.max_retries)
            .finish_non_exhaustive()
    }
}

/// Builds a [`Client`] with non-default settings.
#[derive(Debug, Clone)]
#[must_use]
pub struct ClientBuilder {
    api_key: Option<String>,
    base_url: String,
    timeout: Duration,
    max_retries: u32,
    app: Option<String>,
}

impl Default for ClientBuilder {
    fn default() -> Self {
        ClientBuilder {
            api_key: None,
            base_url: DEFAULT_BASE_URL.to_owned(),
            timeout: DEFAULT_TIMEOUT,
            max_retries: DEFAULT_MAX_RETRIES,
            app: None,
        }
    }
}

impl ClientBuilder {
    /// The API key. When not set, `SCRAPEUNBLOCKER_KEY` is read.
    pub fn api_key(mut self, api_key: impl Into<String>) -> Self {
        self.api_key = Some(api_key.into());
        self
    }

    /// Another API host (rarely needed).
    pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
        self.base_url = base_url.into();
        self
    }

    /// Per-request timeout. Default 180 seconds.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// How many times to retry HTTP 429/502/503/504 and network errors, with
    /// exponential backoff (0.5 s, 1 s, 2 s ... at most 8 s). Default 2.
    pub fn max_retries(mut self, max_retries: u32) -> Self {
        self.max_retries = max_retries;
        self
    }

    /// Your app's own product token, sent after the SDK's in the
    /// `User-Agent` header, e.g. `"my-crawler/1.4"`.
    pub fn user_agent(mut self, app: impl Into<String>) -> Self {
        self.app = Some(app.into());
        self
    }

    /// Build the client.
    ///
    /// Fails with [`Error::MissingApiKey`] when no key was given and
    /// `SCRAPEUNBLOCKER_KEY` is not set.
    pub fn build(self) -> Result<Client> {
        let api_key = self
            .api_key
            .or_else(|| std::env::var(API_KEY_ENV).ok())
            .map(|k| k.trim().to_owned())
            .filter(|k| !k.is_empty())
            .ok_or(Error::MissingApiKey)?;
        let mut key = HeaderValue::from_str(&api_key).map_err(|_| {
            Error::Config("the API key holds characters an HTTP header cannot carry".into())
        })?;
        key.set_sensitive(true);

        let base_url = self.base_url.trim_end_matches('/').to_owned();
        Url::parse(&base_url).map_err(|e| Error::Config(format!("base URL {base_url:?}: {e}")))?;

        let mut headers = HeaderMap::new();
        headers.insert(API_KEY_HEADER, key);
        headers.insert(ACCEPT, HeaderValue::from_static("*/*"));
        let http = reqwest::Client::builder()
            .default_headers(headers)
            .user_agent(user_agent(self.app.as_deref()))
            .timeout(self.timeout)
            .build()
            .map_err(|e| Error::Config(e.to_string()))?;

        Ok(Client {
            inner: Arc::new(Inner {
                http,
                base_url,
                timeout: self.timeout,
                max_retries: self.max_retries,
            }),
        })
    }
}

/// The SDK's product token, then the app's own when it gives one (control
/// characters, which would break the header, become spaces).
fn user_agent(app: Option<&str>) -> String {
    let own = format!("scrapeunblocker-rust/{VERSION}");
    let app: String = app
        .unwrap_or("")
        .chars()
        .map(|c| if c.is_control() { ' ' } else { c })
        .collect();
    let app = app.split_whitespace().collect::<Vec<_>>().join(" ");
    if app.is_empty() {
        own
    } else {
        format!("{own} {app}")
    }
}

fn backoff(attempt: u32) -> Duration {
    Duration::from_secs_f64((0.5 * 2f64.powi(attempt.min(10) as i32)).min(8.0))
}

impl Client {
    /// A client with the given API key and default settings.
    pub fn new(api_key: impl Into<String>) -> Result<Self> {
        ClientBuilder::default().api_key(api_key).build()
    }

    /// A client whose API key comes from the `SCRAPEUNBLOCKER_KEY` environment variable.
    pub fn from_env() -> Result<Self> {
        ClientBuilder::default().build()
    }

    /// Start building a client with non-default settings.
    pub fn builder() -> ClientBuilder {
        ClientBuilder::default()
    }

    /// Send one call, retrying transient failures, and decode the answer.
    pub(crate) async fn execute<T: Decode>(&self, call: Call) -> Result<T> {
        let mut url = Url::parse(&format!("{}{}", self.inner.base_url, call.path))
            .map_err(|e| Error::Config(format!("endpoint {:?}: {e}", call.path)))?;
        if !call.query.pairs().is_empty() {
            url.query_pairs_mut().extend_pairs(call.query.pairs());
        }

        let mut attempt = 0;
        loop {
            let sent = self.inner.http.post(url.clone()).body("").send().await;
            let response = match sent {
                Ok(response) => response,
                Err(e) if e.is_timeout() => return Err(Error::Timeout(e)),
                Err(_) if attempt < self.inner.max_retries => {
                    tokio::time::sleep(backoff(attempt)).await;
                    attempt += 1;
                    continue;
                }
                Err(e) => return Err(Error::Connection(e)),
            };

            let status = response.status().as_u16();
            if RETRYABLE_STATUS.contains(&status) && attempt < self.inner.max_retries {
                drop(response);
                tokio::time::sleep(backoff(attempt)).await;
                attempt += 1;
                continue;
            }

            let header = |name: &str| {
                response
                    .headers()
                    .get(name)
                    .and_then(|v| v.to_str().ok())
                    .map(str::to_owned)
            };
            let origin_status = header("x-origin-status");
            let destination_url = header("x-destination-url");
            let success = response.status().is_success();
            let body = response.bytes().await.map_err(|e| {
                if e.is_timeout() {
                    Error::Timeout(e)
                } else {
                    Error::Connection(e)
                }
            })?;

            if success {
                return T::decode(body.to_vec());
            }
            let text = String::from_utf8_lossy(&body).into_owned();
            return Err(api_error(
                status,
                Some(text),
                origin_status.as_deref(),
                destination_url,
            )
            .into());
        }
    }

    // -- core endpoints ---------------------------------------------------

    /// Fetch a URL and return the fully rendered HTML.
    ///
    /// The page is loaded in a real browser behind the anti-bot bypass chain,
    /// so JavaScript-heavy and protected sites come back rendered. When the
    /// target page itself answers 404 or 410 the call fails with
    /// [`ApiErrorKind::TargetNotFound`](crate::ApiErrorKind::TargetNotFound)
    /// (billed; the not-found page is on its `html`).
    ///
    /// Calls `POST /getPageSource`.
    pub fn get_page_source(&self, url: impl Into<String>) -> GetPageSource {
        GetPageSource::new(self.clone(), url.into())
    }

    /// List the page's interactive elements (buttons, inputs, selects, links,
    /// forms) with ready selectors, as JSON (`{"url", "count", "elements": [...]}`).
    ///
    /// Each element's `selector` drops straight into a [`Step`].
    ///
    /// Calls `POST /getPageSource` with `list_elements=true`.
    pub fn list_elements(&self, url: impl Into<String>) -> ListElements {
        ListElements::new(self.clone(), url.into())
    }

    /// Fetch a URL and return the rendered page as clean Markdown, for LLMs,
    /// agents and RAG pipelines.
    ///
    /// Headings, lists, tables, absolute links, images with their alt text and
    /// code blocks, without scripts, styles, navigation, footers, forms or
    /// hidden elements. Billed like an HTML call.
    ///
    /// Calls `POST /getPageSource` with `markdown=true`.
    pub fn get_markdown(&self, url: impl Into<String>) -> GetMarkdown {
        GetMarkdown::new(self.clone(), url.into())
    }

    /// Fetch a URL and return structured JSON instead of HTML.
    ///
    /// The API extracts fields with Schema.org, `__NEXT_DATA__` or
    /// AI-generated rules - no per-site parser to maintain. When the page
    /// rendered but held no structured data, the result has
    /// `data_extracted == false` and the page on `html` (billed like any
    /// delivered page).
    ///
    /// Calls `POST /getPageSource` with `parsed_data=true`.
    pub fn get_parsed(&self, url: impl Into<String>) -> GetParsed {
        GetParsed::new(self.clone(), url.into())
    }

    /// Fetch a URL and also return the cookies and proxy that served it.
    ///
    /// Calls `POST /getPageSource` with `get_cookies=true`.
    pub fn get_page_with_cookies(&self, url: impl Into<String>) -> GetPageWithCookies {
        GetPageWithCookies::new(self.clone(), url.into())
    }

    /// Fetch a URL and return the page's own XHR/fetch responses with it.
    ///
    /// Many sites render from a JSON API the page calls itself, and that API
    /// often refuses direct requests (the page signs each call). The page is
    /// rendered in a real browser and every XHR/fetch response whose URL
    /// contains `capture_xhr` (e.g. `"/api/search"`) comes back in the order
    /// the page received it: up to 20 responses and 10 MB of bodies. Add
    /// [`steps`](GetPageWithXhr::steps) to make the page fire the call. A
    /// failed step returns [`ApiErrorKind::StepFailed`](crate::ApiErrorKind::StepFailed)
    /// whose [`StepFailure::xhr`](crate::StepFailure::xhr) holds what the page
    /// received before that.
    ///
    /// Calls `POST /getPageSource` with `capture_xhr`.
    pub fn get_page_with_xhr(
        &self,
        url: impl Into<String>,
        capture_xhr: impl Into<String>,
    ) -> GetPageWithXhr {
        GetPageWithXhr::new(self.clone(), url.into(), capture_xhr.into())
    }

    /// Run a Google search and return the parsed SERP as JSON.
    ///
    /// The result has `organic` results and `topAds` / `bottomAds` (title,
    /// real url, description, position, displayedUrl, domain, favicon,
    /// sitelinks; organic adds source, date, rating, richSnippet and
    /// thumbnail, ads advertiser), `aiOverview`, `totalResults`, and the first
    /// page's other blocks: peopleAlsoAsk, relatedSearches,
    /// relatedSearchLinks, knowledgeGraph, topStories, videos, localResults,
    /// shoppingResults and spellingCorrection (each empty or null when Google
    /// shows none).
    ///
    /// Calls `POST /serpApi`.
    pub fn serp(&self, keyword: impl Into<String>) -> Serp {
        Serp::new(self.clone(), keyword.into())
    }

    /// Fetch an image and return its bytes exactly as the image host serves
    /// them - the original format (JPEG, PNG, WebP, AVIF, GIF, SVG, ...),
    /// nothing re-encoded.
    ///
    /// `url` must point at the image itself: a page URL fails with
    /// [`ApiErrorKind::UnsupportedContent`](crate::ApiErrorKind::UnsupportedContent).
    ///
    /// Calls `POST /getImage`.
    pub fn get_image(&self, url: impl Into<String>) -> GetImage {
        GetImage::new(self.clone(), url.into())
    }

    /// Call any plugin endpoint by path, with parameters as the API names
    /// them (snake_case). Use it for an endpoint this SDK version has no
    /// method for yet.
    ///
    /// ```no_run
    /// # async fn run(su: scrapeunblocker::Client) -> scrapeunblocker::Result<()> {
    /// let results = su
    ///     .plugin("/marketplace/target-search")
    ///     .param("query", "tv")
    ///     .param("page", 2)
    ///     .await?;
    /// # Ok(()) }
    /// ```
    pub fn plugin(&self, path: impl Into<String>) -> Plugin {
        Plugin::new(self.clone(), path.into())
    }
}

// -- request builders -----------------------------------------------------

/// Setters shared by the page-rendering builders.
macro_rules! page_setters {
    () => {
        /// ISO country code to route the request through, e.g. `"us"`.
        pub fn proxy_country(mut self, country: impl Into<String>) -> Self {
            self.call.query.set("proxy_country", country.into());
            self
        }

        /// Extra seconds to wait after the page loads, before capture.
        pub fn time_sleep(mut self, seconds: u32) -> Self {
            self.call.query.set("time_sleep", seconds);
            self
        }

        /// Advanced render-wait method (`"css"`, `"js"`, ...), paired with [`value`](Self::value).
        pub fn method(mut self, method: impl Into<String>) -> Self {
            self.call.query.set("method", method.into());
            self
        }

        /// The selector or expression paired with [`method`](Self::method).
        pub fn value(mut self, value: impl Into<String>) -> Self {
            self.call.query.set("value", value.into());
            self
        }

        /// Cap in seconds for the render-wait method.
        pub fn method_timeout(mut self, seconds: u32) -> Self {
            self.call.query.set("method_timeout", seconds);
            self
        }
    };
}

/// Setter for browser steps.
macro_rules! steps_setter {
    () => {
        /// Browser actions to run, in order, after the page loads. See [`Step`].
        pub fn steps(mut self, steps: impl IntoIterator<Item = Step>) -> Self {
            let steps: Vec<Step> = steps.into_iter().collect();
            let json = serde_json::to_string(&steps).expect("steps serialize to JSON");
            self.call.query.set("steps", json);
            self
        }
    };
}

/// Request builder for [`Client::get_page_source`]. Await it for the HTML.
#[derive(Debug, Clone)]
#[must_use = "a request does nothing until it is awaited or sent"]
pub struct GetPageSource<C = Client> {
    client: C,
    call: Call,
}

impl<C> GetPageSource<C> {
    pub(crate) fn new(client: C, url: String) -> Self {
        let mut call = Call::new("/getPageSource");
        call.query.set("url", url);
        GetPageSource { client, call }
    }

    page_setters!();
    steps_setter!();
}

executable!(GetPageSource -> String);

/// Request builder for [`Client::list_elements`]. Await it for the JSON.
#[derive(Debug, Clone)]
#[must_use = "a request does nothing until it is awaited or sent"]
pub struct ListElements<C = Client> {
    client: C,
    call: Call,
}

impl<C> ListElements<C> {
    pub(crate) fn new(client: C, url: String) -> Self {
        let mut call = Call::new("/getPageSource");
        call.query.set("url", url);
        call.query.set("list_elements", true);
        ListElements { client, call }
    }

    page_setters!();
}

executable!(ListElements -> Value);

/// Request builder for [`Client::get_markdown`]. Await it for the Markdown.
#[derive(Debug, Clone)]
#[must_use = "a request does nothing until it is awaited or sent"]
pub struct GetMarkdown<C = Client> {
    client: C,
    call: Call,
}

impl<C> GetMarkdown<C> {
    pub(crate) fn new(client: C, url: String) -> Self {
        let mut call = Call::new("/getPageSource");
        call.query.set("url", url);
        call.query.set("markdown", true);
        GetMarkdown { client, call }
    }

    /// Keep only the page's main content, without navigation, the site
    /// header, footer and sidebars (the API default). `false` converts the
    /// whole page.
    pub fn main_content_only(mut self, main_content_only: bool) -> Self {
        self.call.query.set("main_content_only", main_content_only);
        self
    }

    page_setters!();
    steps_setter!();
}

executable!(GetMarkdown -> String);

/// Request builder for [`Client::get_parsed`]. Await it for a [`ParsedPage`].
#[derive(Debug, Clone)]
#[must_use = "a request does nothing until it is awaited or sent"]
pub struct GetParsed<C = Client> {
    client: C,
    call: Call,
}

impl<C> GetParsed<C> {
    pub(crate) fn new(client: C, url: String) -> Self {
        let mut call = Call::new("/getPageSource");
        call.query.set("url", url);
        call.query.set("parsed_data", true);
        GetParsed { client, call }
    }

    /// Force-regenerate the cached extraction rules for this domain (use when
    /// a parse comes back clearly wrong).
    pub fn refresh_rules(mut self, refresh: bool) -> Self {
        self.call.query.set("refresh_rules", refresh);
        self
    }

    /// Free-text steer for regenerated rules, e.g. `"price is missing"`.
    pub fn rules_hint(mut self, hint: impl Into<String>) -> Self {
        self.call.query.set("rules_hint", hint.into());
        self
    }

    /// ISO country code to route the request through, e.g. `"us"`.
    pub fn proxy_country(mut self, country: impl Into<String>) -> Self {
        self.call.query.set("proxy_country", country.into());
        self
    }

    /// Extra seconds to wait after the page loads, before capture.
    pub fn time_sleep(mut self, seconds: u32) -> Self {
        self.call.query.set("time_sleep", seconds);
        self
    }
}

executable!(GetParsed -> ParsedPage);

/// Request builder for [`Client::get_page_with_cookies`]. Await it for a [`PageResult`].
#[derive(Debug, Clone)]
#[must_use = "a request does nothing until it is awaited or sent"]
pub struct GetPageWithCookies<C = Client> {
    client: C,
    call: Call,
}

impl<C> GetPageWithCookies<C> {
    pub(crate) fn new(client: C, url: String) -> Self {
        let mut call = Call::new("/getPageSource");
        call.query.set("url", url);
        call.query.set("get_cookies", true);
        GetPageWithCookies { client, call }
    }

    /// ISO country code to route the request through, e.g. `"us"`.
    pub fn proxy_country(mut self, country: impl Into<String>) -> Self {
        self.call.query.set("proxy_country", country.into());
        self
    }

    /// Extra seconds to wait after the page loads, before capture.
    pub fn time_sleep(mut self, seconds: u32) -> Self {
        self.call.query.set("time_sleep", seconds);
        self
    }
}

executable!(GetPageWithCookies -> PageResult);

/// Request builder for [`Client::get_page_with_xhr`]. Await it for a [`CapturedPage`].
#[derive(Debug, Clone)]
#[must_use = "a request does nothing until it is awaited or sent"]
pub struct GetPageWithXhr<C = Client> {
    client: C,
    call: Call,
}

impl<C> GetPageWithXhr<C> {
    pub(crate) fn new(client: C, url: String, capture_xhr: String) -> Self {
        let mut call = Call::new("/getPageSource");
        call.query.set("url", url);
        call.query.set("capture_xhr", capture_xhr);
        GetPageWithXhr { client, call }
    }

    /// ISO country code to route the request through, e.g. `"us"`.
    pub fn proxy_country(mut self, country: impl Into<String>) -> Self {
        self.call.query.set("proxy_country", country.into());
        self
    }

    steps_setter!();
}

executable!(GetPageWithXhr -> CapturedPage);

/// Request builder for [`Client::serp`]. Await it for the SERP JSON.
#[derive(Debug, Clone)]
#[must_use = "a request does nothing until it is awaited or sent"]
pub struct Serp<C = Client> {
    client: C,
    call: Call,
}

impl<C> Serp<C> {
    pub(crate) fn new(client: C, keyword: String) -> Self {
        let mut call = Call::new("/serpApi");
        call.query.set("keyword", keyword);
        Serp { client, call }
    }

    /// ISO country code to search from, e.g. `"de"`.
    pub fn proxy_country(mut self, country: impl Into<String>) -> Self {
        self.call.query.set("proxy_country", country.into());
        self
    }

    /// How many result pages to fetch and merge (1-10). Each page is billed
    /// as one request. API default: 1.
    pub fn pages_to_check(mut self, pages: u32) -> Self {
        self.call.query.set("pages_to_check", pages);
        self
    }

    /// Extra seconds to wait before extraction. Rarely needed.
    pub fn wait_after_load(mut self, seconds: u32) -> Self {
        self.call.query.set("wait_after_load", seconds);
        self
    }

    /// Wait until Google gives out every result's real URL. Google releases
    /// a results page's links a few at a time, the last ones up to about 40
    /// seconds after the search. Without this the call returns sooner, and a
    /// result whose link has not come yet has the site's home page as its
    /// `url`. Billed the same.
    pub fn full_urls(mut self, full_urls: bool) -> Self {
        self.call.query.set("full_urls", full_urls);
        self
    }
}

executable!(Serp -> Value);

/// Request builder for [`Client::get_image`]. Await it for the image bytes.
#[derive(Debug, Clone)]
#[must_use = "a request does nothing until it is awaited or sent"]
pub struct GetImage<C = Client> {
    client: C,
    call: Call,
}

impl<C> GetImage<C> {
    pub(crate) fn new(client: C, url: String) -> Self {
        let mut call = Call::new("/getImage");
        call.query.set("url", url);
        GetImage { client, call }
    }

    /// ISO country code to route the request through, e.g. `"us"`.
    pub fn proxy_country(mut self, country: impl Into<String>) -> Self {
        self.call.query.set("proxy_country", country.into());
        self
    }
}

executable!(GetImage -> Vec<u8>);

/// Request builder for [`Client::plugin`]. Await it for the JSON.
#[derive(Debug, Clone)]
#[must_use = "a request does nothing until it is awaited or sent"]
pub struct Plugin<C = Client> {
    client: C,
    call: Call,
}

impl<C> Plugin<C> {
    pub(crate) fn new(client: C, path: String) -> Self {
        let path = if path.starts_with('/') {
            path
        } else {
            format!("/{path}")
        };
        Plugin {
            client,
            call: Call::new(path),
        }
    }

    /// Set one query parameter, as the API names it. Setting a name again
    /// replaces it.
    pub fn param(mut self, name: &str, value: impl ToString) -> Self {
        self.call.query.set(name, value);
        self
    }

    /// A repeated query parameter: one `name=value` pair per item.
    pub fn param_each<T: ToString>(
        mut self,
        name: &str,
        values: impl IntoIterator<Item = T>,
    ) -> Self {
        self.call.query.set_each(name, values);
        self
    }
}

executable!(Plugin -> Value);