Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ async def main():
client = Client(
impersonate=Impersonate.Firefox133,
user_agent="rnet",
async_dns=True,
proxies=[
Proxy.http("socks5h://abc:def@127.0.0.1:1080"),
Proxy.https(url="socks5h://127.0.0.1:1080", username="abc", password="def"),
Expand Down
15 changes: 9 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use arc_swap::{ArcSwap, Guard};
use pyo3::prelude::*;
use pyo3_stub_gen::derive::{gen_stub_pyclass, gen_stub_pymethods};
use rquest::{
dns::LookupIpStrategy,
header::{HeaderMap, HeaderName, HeaderValue},
redirect::Policy,
Url,
Expand All @@ -29,6 +28,11 @@ macro_rules! apply_option {
$builder = $builder.$method(value);
}
};
(apply_if_ok, $builder:expr, $result:expr, $method:ident) => {
if let Ok(value) = $result() {
$builder = $builder.$method(value);
}
};
(apply_if_some_ref, $builder:expr, $option:expr, $method:ident) => {
if let Some(value) = $option.take() {
$builder = $builder.$method(&value);
Expand Down Expand Up @@ -80,7 +84,9 @@ impl Client {
/// ```
pub fn default() -> &'static Self {
static CLIENT: LazyLock<Client> = LazyLock::new(|| {
rquest::Client::builder()
let mut builder = rquest::Client::builder();
apply_option!(apply_if_ok, builder, dns::get_or_try_init, dns_resolver);
builder
.no_hickory_dns()
.no_keepalive()
.build()
Expand Down Expand Up @@ -576,10 +582,7 @@ impl Client {
apply_option!(apply_if_some, builder, params.cookie_store, cookie_store);

// Async resolver options.
if params.async_dns.unwrap_or(false) {
let hickory_dns_resolver = dns::get_or_try_init(LookupIpStrategy::Ipv4AndIpv6)?;
builder = builder.dns_resolver(hickory_dns_resolver);
}
apply_option!(apply_if_ok, builder, dns::get_or_try_init, dns_resolver);

// Timeout options.
apply_option!(
Expand Down
7 changes: 2 additions & 5 deletions src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ use std::sync::{Arc, OnceLock};
///
/// let resolver = get_or_try_init(LookupIpStrategy::default()).unwrap();
/// ```
pub fn get_or_try_init<S>(strategy: S) -> crate::Result<Arc<HickoryDnsResolver>>
where
S: Into<Option<LookupIpStrategy>>,
{
pub fn get_or_try_init() -> crate::Result<Arc<HickoryDnsResolver>> {
static DNS_RESOLVER: OnceLock<Result<Arc<HickoryDnsResolver>, &'static str>> = OnceLock::new();

DNS_RESOLVER
.get_or_init(move || {
HickoryDnsResolver::new(strategy.into())
HickoryDnsResolver::new(LookupIpStrategy::Ipv4AndIpv6)
.map(Arc::new)
.map_err(|err| {
#[cfg(feature = "logging")]
Expand Down
5 changes: 0 additions & 5 deletions src/param/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ pub struct ClientParams {
#[pyo3(get)]
pub cookie_store: Option<bool>,

/// Whether to use async DNS resolver.
#[pyo3(get)]
pub async_dns: Option<bool>,

// ========= Timeout options =========
/// The timeout to use for the request. (in seconds)
#[pyo3(get)]
Expand Down Expand Up @@ -259,7 +255,6 @@ impl<'py> FromPyObject<'py> for ClientParams {
extract_option!(ob, params, referer);
extract_option!(ob, params, allow_redirects);
extract_option!(ob, params, cookie_store);
extract_option!(ob, params, async_dns);

extract_option!(ob, params, timeout);
extract_option!(ob, params, connect_timeout);
Expand Down