Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ClientBuilder and support a configureable reqwest client #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ impl Client {
}
}

/// Creates a new [ClientBuilder]
pub fn builder() -> ClientBuilder {
ClientBuilder::new()
}

/// Operations on [`Bucket`](crate::bucket::Bucket)s.
pub fn bucket(&self) -> BucketClient<'_> {
BucketClient(self)
Expand Down Expand Up @@ -104,3 +109,44 @@ impl Client {
Ok(result)
}
}

/// A ClientBuilder can be used to create a Client with custom configuration.
#[derive(Default)]
pub struct ClientBuilder {
client: Option<reqwest::Client>,
/// Static `Token` struct that caches
token_cache: Option<sync::Arc<dyn crate::TokenCache + Send>>,
}

impl ClientBuilder {
/// Constructs a new ClientBuilder
pub fn new() -> Self {
Default::default()
}

/// Returns a `Client` that uses this `ClientBuilder` configuration.
pub fn build(self) -> Client {
Client {
client: self.client.unwrap_or_default(),
token_cache: self
.token_cache
.unwrap_or(sync::Arc::new(crate::Token::default())),
}
}

/// Sets refreshable token
pub fn with_cache(self, token: impl TokenCache + Send + 'static) -> Self {
ClientBuilder {
token_cache: Some(sync::Arc::new(token)),
..self
}
}

/// Sets internal [reqwest Client](https://docs.rs/reqwest/latest/reqwest/struct.Client.html)
pub fn with_reqwest_client(self, reqwest_client: reqwest::Client) -> Self {
ClientBuilder {
client: Some(reqwest_client),
..self
}
}
}