From b58320172b9c1a22d6afa3f9c1ad0fdc3d406057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6neberger?= Date: Mon, 6 Jan 2025 19:37:24 +0100 Subject: [PATCH] Add ability to provide custom reqwest/ureq client builder to dispatcher builder --- src/dispatcher/async.rs | 5 ++++- src/dispatcher/blocking.rs | 7 +++++-- src/dispatcher/builder.rs | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/dispatcher/async.rs b/src/dispatcher/async.rs index 60b11a8..79b8f19 100644 --- a/src/dispatcher/async.rs +++ b/src/dispatcher/async.rs @@ -20,8 +20,11 @@ pub struct Async { impl Async { #[inline] pub(crate) fn new(builder: DispatcherBuilder) -> Result { - let mut client = ClientBuilder::new(); + Self::new_with_client(builder, ClientBuilder::new()) + } + #[inline] + pub(crate) fn new_with_client(builder: DispatcherBuilder, mut client: ClientBuilder) -> Result { if let Some(auth) = builder.auth { let mut headers = HeaderMap::new(); let mut auth_value = HeaderValue::from_str(&auth.to_header_value())?; diff --git a/src/dispatcher/blocking.rs b/src/dispatcher/blocking.rs index 7c98a94..2d6a1cd 100644 --- a/src/dispatcher/blocking.rs +++ b/src/dispatcher/blocking.rs @@ -1,7 +1,7 @@ // Copyright (c) 2022 Yuki Kishimoto // Distributed under the MIT software license -use ureq::{Agent, MiddlewareNext, Request, Response}; +use ureq::{Agent, AgentBuilder, MiddlewareNext, Request, Response}; use url::Url; use super::builder::DispatcherBuilder; @@ -17,8 +17,11 @@ pub struct Blocking { impl Blocking { #[inline] pub(crate) fn new(builder: DispatcherBuilder) -> Result { - let mut client = ureq::builder(); + Self::new_with_client(builder, ureq::builder()) + } + #[inline] + pub(crate) fn new_with_client(builder: DispatcherBuilder, mut client: AgentBuilder) -> Result { if let Some(auth) = builder.auth { let heaver_value = auth.to_header_value(); diff --git a/src/dispatcher/builder.rs b/src/dispatcher/builder.rs index a1fb23d..b5c6974 100644 --- a/src/dispatcher/builder.rs +++ b/src/dispatcher/builder.rs @@ -6,9 +6,13 @@ use url::Url; #[cfg(feature = "async")] use super::Async; +#[cfg(feature = "async")] +use reqwest::ClientBuilder; use super::Auth; #[cfg(feature = "blocking")] use super::Blocking; +#[cfg(feature = "blocking")] +use ureq::AgentBuilder; #[cfg(any(feature = "async", feature = "blocking"))] use super::{Dispatcher, Error}; @@ -67,6 +71,14 @@ impl DispatcherBuilder { }) } + #[cfg(feature = "async")] + pub fn build_async_with_client(self, client: ClientBuilder) -> Result, Error> { + Ok(Dispatcher { + url: Url::parse(&self.url)?, + inner: Async::new_with_client(self, client)?, + }) + } + #[cfg(feature = "blocking")] pub fn build_blocking(self) -> Result, Error> { Ok(Dispatcher { @@ -74,4 +86,13 @@ impl DispatcherBuilder { inner: Blocking::new(self)?, }) } + + #[cfg(feature = "blocking")] + pub fn build_blocking_with_client(self, client: AgentBuilder) -> Result, Error> { + + Ok(Dispatcher { + url: Url::parse(&self.url)?, + inner: Blocking::new_with_client(self, client)?, + }) + } }