Skip to content

Commit

Permalink
Allow providing custom client builder to DispatcherBuilder
Browse files Browse the repository at this point in the history
As mentioned in #9, I added functionality to provide the dispatcher builder and the `Async`/`Blocking` structs with a custom client/agent builder, so things like the user agent, which is not handled by the dispatcher builder already, can be set beforehand.

Closes #10

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
JoeJoeTV authored and yukibtc committed Jan 8, 2025
1 parent 6c48ed4 commit 0a058cb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/dispatcher/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ 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())
}

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
9 changes: 7 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,13 @@ 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())
}

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
26 changes: 26 additions & 0 deletions src/dispatcher/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use super::Auth;
use super::Blocking;
#[cfg(any(feature = "async", feature = "blocking"))]
use super::{Dispatcher, Error};
#[cfg(feature = "async")]
use reqwest::ClientBuilder;
#[cfg(feature = "blocking")]
use ureq::AgentBuilder;

#[derive(Debug, Clone)]
pub struct DispatcherBuilder {
Expand Down Expand Up @@ -67,11 +71,33 @@ 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 0a058cb

Please sign in to comment.