diff --git a/src/client.rs b/src/client.rs index 329428ef..aae362bf 100644 --- a/src/client.rs +++ b/src/client.rs @@ -18,7 +18,10 @@ use rquest::{ redirect::Policy, Url, }; -use std::{sync::Arc, time::Duration}; +use std::{ + sync::{Arc, LazyLock}, + time::Duration, +}; macro_rules! apply_option { (apply_if_some, $builder:expr, $option:expr, $method:ident) => { @@ -53,6 +56,43 @@ macro_rules! apply_option { #[pyclass] pub struct Client(ArcSwap); +impl Client { + /// Creates a new default `Client` instance. + /// + /// This method initializes a `Client` with default settings, including disabling + /// Hickory DNS and keepalive. + /// + /// # Panics + /// + /// This method will panic if the Client cannot be created. + /// + /// # Note + /// + /// This client does not maintain a session, meaning it does not share cookies, + /// headers, or other parameters between requests. + /// + /// # Examples + /// + /// ```rust + /// use rnet::Client; + /// + /// let client = Client::default(); + /// ``` + pub fn default() -> &'static Self { + static CLIENT: LazyLock = LazyLock::new(|| { + rquest::Client::builder() + .no_hickory_dns() + .no_keepalive() + .build() + .map(ArcSwap::from_pointee) + .map(Client) + .expect("Failed to build the default client.") + }); + + &*CLIENT + } +} + #[gen_stub_pymethods] #[pymethods] impl Client { @@ -856,30 +896,6 @@ impl Client { } } -/// Creates a new default `Client` instance. -/// -/// # Panics -/// -/// This method will panic if the Client cannot be created. -/// -/// # Examples -/// -/// ```rust -/// use rnet::Client; -/// -/// let client = Client::default(); -/// ``` -impl Default for Client { - fn default() -> Self { - rquest::Client::builder() - .no_hickory_dns() - .build() - .map(ArcSwap::from_pointee) - .map(Client) - .expect("Failed to build the default client.") - } -} - /// Executes an HTTP request. async fn execute_request( client: Guard>,