Skip to content

Commit

Permalink
Add ability to provide custom reqwest/ureq client builder to dispatch…
Browse files Browse the repository at this point in the history
…er builder
  • Loading branch information
JoeJoeTV committed Jan 6, 2025
1 parent 6c48ed4 commit b583201
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/dispatcher/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ pub struct Async {
impl Async {
#[inline]
pub(crate) fn new(builder: DispatcherBuilder) -> Result<Self, Error> {
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<Self, Error> {
if let Some(auth) = builder.auth {
let mut headers = HeaderMap::new();
let mut auth_value = HeaderValue::from_str(&auth.to_header_value())?;
Expand Down
7 changes: 5 additions & 2 deletions src/dispatcher/blocking.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,8 +17,11 @@ pub struct Blocking {
impl Blocking {
#[inline]
pub(crate) fn new(builder: DispatcherBuilder) -> Result<Self, Error> {
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<Self, Error> {
if let Some(auth) = builder.auth {
let heaver_value = auth.to_header_value();

Expand Down
21 changes: 21 additions & 0 deletions src/dispatcher/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -67,11 +71,28 @@ impl DispatcherBuilder {
})
}

#[cfg(feature = "async")]
pub fn build_async_with_client(self, client: ClientBuilder) -> Result<Dispatcher<Async>, Error> {
Ok(Dispatcher {
url: Url::parse(&self.url)?,
inner: Async::new_with_client(self, client)?,
})
}

#[cfg(feature = "blocking")]
pub fn build_blocking(self) -> Result<Dispatcher<Blocking>, Error> {
Ok(Dispatcher {
url: Url::parse(&self.url)?,
inner: Blocking::new(self)?,
})
}

#[cfg(feature = "blocking")]
pub fn build_blocking_with_client(self, client: AgentBuilder) -> Result<Dispatcher<Blocking>, Error> {

Ok(Dispatcher {
url: Url::parse(&self.url)?,
inner: Blocking::new_with_client(self, client)?,
})
}
}

0 comments on commit b583201

Please sign in to comment.