SerpClientBuilder

Struct SerpClientBuilder 

Source
pub struct SerpClientBuilder { /* private fields */ }
Expand description

A builder for constructing SerpClient instances with custom configuration.

The builder pattern provides a flexible and type-safe way to configure the client with various options. All configuration methods return self for method chaining.

§Default Configuration

  • Timeout: 30 seconds
  • Retry Policy: Default policy with exponential backoff
  • Base URL: https://serpapi.com
  • User Agent: serp-sdk-rust/{version}

§Configuration Options

§Examples

§Basic Configuration

let client = SerpClient::builder()
    .api_key("your-api-key")
    .build()?;

§Full Configuration

let client = SerpClient::builder()
    .api_key("your-api-key")
    .timeout(Duration::from_secs(60))
    .retry_policy(
        RetryPolicy::new(3)
            .with_base_delay(Duration::from_millis(500))
            .with_max_delay(Duration::from_secs(30))
    )
    .user_agent("my-search-app/2.0")
    .default_header("X-Custom-Header", "value")?
    .build()?;

§Using Environment Variables

// API key will be read from SERP_API_KEY environment variable
std::env::set_var("SERP_API_KEY", "your-api-key");
let client = SerpClient::builder().build()?;

Implementations§

Source§

impl SerpClientBuilder

Source

pub fn new() -> Self

Creates a new client builder with default settings.

The default configuration is suitable for most use cases:

  • 30-second timeout
  • Standard retry policy with exponential backoff
  • Official SerpAPI endpoint
§Examples
use serp_sdk::SerpClient;

let builder = SerpClient::builder();
Source

pub fn api_key(self, key: impl Into<String>) -> Self

Sets the API key for authentication.

The API key is required for all SerpAPI requests. You can obtain one by signing up at https://serpapi.com.

§Arguments
  • key - The SerpAPI authentication key
§Examples
let client = SerpClient::builder()
    .api_key("your-secret-api-key")
    .build()?;
Source

pub fn base_url(self, url: impl Into<String>) -> Self

Sets a custom base URL for the API.

This is useful for:

  • Using a proxy server
  • Connecting to a mock server for testing
  • Using a regional endpoint
§Arguments
  • url - The base URL (without trailing slash)
§Examples
let client = SerpClient::builder()
    .api_key("test-key")
    .base_url("https://proxy.example.com")
    .build()?;
Source

pub fn timeout(self, timeout: Duration) -> Self

Sets the request timeout duration.

This timeout applies to the entire request/response cycle, including:

  • DNS resolution
  • TCP connection
  • TLS handshake
  • Request transmission
  • Response reception
§Arguments
  • timeout - Maximum duration for a single request
§Examples
let client = SerpClient::builder()
    .api_key("key")
    .timeout(Duration::from_secs(60))  // 1 minute timeout
    .build()?;
Source

pub fn retry_policy(self, policy: RetryPolicy) -> Self

Sets the retry policy for handling transient failures.

The retry policy determines:

  • Maximum number of retry attempts
  • Delay between retries (with exponential backoff)
  • Jitter for avoiding thundering herd
§Arguments
  • policy - Custom retry policy configuration
§Examples
let policy = RetryPolicy::new(5)
    .with_base_delay(Duration::from_millis(100))
    .with_max_delay(Duration::from_secs(60))
    .with_jitter(true);

let client = SerpClient::builder()
    .api_key("key")
    .retry_policy(policy)
    .build()?;
Source

pub fn user_agent(self, agent: impl Into<String>) -> Self

Sets a custom User-Agent header.

The User-Agent identifies your application to the API server. This can be useful for debugging and analytics.

§Arguments
  • agent - User-Agent string
§Examples
let client = SerpClient::builder()
    .api_key("key")
    .user_agent("MySearchApp/1.0 (contact@example.com)")
    .build()?;
Source

pub fn default_header( self, name: impl AsRef<str>, value: impl AsRef<str>, ) -> SerpResult<Self>

Adds a default header to all requests.

Custom headers are useful for:

  • Adding authentication tokens
  • Setting correlation IDs for tracing
  • Adding custom metadata
§Arguments
  • name - Header name
  • value - Header value
§Returns

Returns Result<Self> as header parsing can fail for invalid names/values.

§Examples
let client = SerpClient::builder()
    .api_key("key")
    .default_header("X-Request-ID", "abc123")?
    .default_header("X-Client-Version", "2.0")?
    .build()?;
Source

pub fn build(self) -> SerpResult<SerpClient>

Builds the configured SerpClient instance.

This method validates the configuration and creates the client. It will fail if:

  • No API key is provided (via builder or environment)
  • The API key is empty or whitespace-only
  • HTTP client construction fails
§Returns

Returns Result<SerpClient> which will be:

  • Ok(client) if configuration is valid
  • Err(SerpError) if configuration is invalid
§Examples
let client = SerpClient::builder()
    .api_key("your-api-key")
    .build()?;

assert!(client.is_configured());
§Errors

Trait Implementations§

Source§

impl Default for SerpClientBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,