Skip to content

Commit

Permalink
Add builder style constructor for Client
Browse files Browse the repository at this point in the history
Builder style makes it easier to construct the Client because each
setting is done via a separate function thus documenting the purpose the setting.

Ref: https://rust-lang.github.io/api-guidelines/type-safety.html#builders-enable-construction-of-complex-values-c-builder
  • Loading branch information
donatello committed Sep 26, 2023
1 parent 4676ae8 commit 018b348
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
run: |
cargo fmt --all -- --check
cargo clippy --all-targets --all-features -- -A clippy::result_large_err -A clippy::type_complexity -A clippy::too_many_arguments
cargo build --verbose
cargo build --bins --examples --tests --benches --verbose
- name: Run tests
run: |
Expand Down
122 changes: 92 additions & 30 deletions src/s3/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use std::collections::{HashMap, VecDeque};
use std::fs::File;
use std::io::prelude::*;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use xmltree::Element;

Expand Down Expand Up @@ -203,6 +204,91 @@ fn parse_list_objects_common_prefixes(
Ok(())
}

/// Client Builder manufactures a Client using given parameters.
#[derive(Debug, Default)]
pub struct ClientBuilder {
base_url: BaseUrl,
provider: Option<Arc<Box<(dyn Provider + Send + Sync + 'static)>>>,
ssl_cert_file: Option<PathBuf>,
ignore_cert_check: Option<bool>,
user_agent: Option<String>,
}

impl ClientBuilder {
/// Creates a builder given a base URL for the MinIO service or other AWS S3
/// compatible object storage service.
pub fn new(base_url: BaseUrl) -> Self {
let mut c = ClientBuilder::default();
c.base_url = base_url;
c
}

/// Set the credential provider. If not set anonymous access is used.
pub fn provider(
mut self,
provider: Option<Box<(dyn Provider + Send + Sync + 'static)>>,
) -> Self {
self.provider = provider.map(Arc::new);
self
}

/// Set the user agent.
pub fn user_agent(mut self, user_agent: Option<String>) -> Self {
self.user_agent = user_agent;
self
}

/// Set file for loading a trust certificate.
pub fn ssl_cert_file(mut self, ssl_cert_file: Option<&Path>) -> Self {
self.ssl_cert_file = ssl_cert_file.map(PathBuf::from);
self
}

/// Set the ignore_cert_check.
pub fn ignore_cert_check(mut self, ignore_cert_check: Option<bool>) -> Self {
self.ignore_cert_check = ignore_cert_check;
self
}

/// Build the Client.
pub fn build(self) -> Result<Client, Error> {
let mut builder = reqwest::Client::builder().no_gzip();

if let Some(v) = self.user_agent {
builder = builder.user_agent(v);
} else {
let info = os_info::get();
let user_agent = String::from("MinIO (")
+ &info.os_type().to_string()
+ "; "
+ info.architecture().unwrap_or("unknown")
+ ") minio-rs/"
+ env!("CARGO_PKG_VERSION");
builder = builder.user_agent(user_agent.to_string());
}

if let Some(v) = self.ignore_cert_check {
builder = builder.danger_accept_invalid_certs(v);
}

if let Some(v) = self.ssl_cert_file {
let mut buf = Vec::new();
File::open(v)?.read_to_end(&mut buf)?;
let cert = reqwest::Certificate::from_pem(&buf)?;
builder = builder.add_root_certificate(cert);
}

let client = builder.build()?;

Ok(Client {
client,
base_url: self.base_url,
provider: self.provider,
region_map: DashMap::new(),
})
}
}

/// Simple Storage Service (aka S3) client to perform bucket and object operations.
///
/// If credential provider is passed, all S3 operation requests are signed using
Expand Down Expand Up @@ -235,38 +321,14 @@ impl Client {
pub fn new(
base_url: BaseUrl,
provider: Option<Box<(dyn Provider + Send + Sync + 'static)>>,
ssl_cert_file: Option<String>,
ssl_cert_file: Option<&Path>,
ignore_cert_check: Option<bool>,
) -> Result<Client, Error> {
let info = os_info::get();
let user_agent = String::from("MinIO (")
+ &info.os_type().to_string()
+ "; "
+ info.architecture().unwrap_or("unknown")
+ ") minio-rs/"
+ env!("CARGO_PKG_VERSION");

let mut builder = reqwest::Client::builder()
.no_gzip()
.user_agent(user_agent.to_string());
if let Some(v) = ignore_cert_check {
builder = builder.danger_accept_invalid_certs(v);
}
if let Some(v) = ssl_cert_file {
let mut buf = Vec::new();
File::open(v.to_string())?.read_to_end(&mut buf)?;
let cert = reqwest::Certificate::from_pem(&buf)?;
builder = builder.add_root_certificate(cert);
}

let client = builder.build()?;

Ok(Client {
client,
base_url,
provider: provider.map(|v| Arc::new(v)),
region_map: DashMap::new(),
})
ClientBuilder::new(base_url)
.provider(provider)
.ssl_cert_file(ssl_cert_file)
.ignore_cert_check(ignore_cert_check)
.build()
}

fn build_headers(
Expand Down
12 changes: 7 additions & 5 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use rand::distributions::{Alphanumeric, DistString};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::{fs, io};
use tokio::sync::mpsc;

Expand Down Expand Up @@ -78,7 +79,7 @@ struct ClientTest {
access_key: String,
secret_key: String,
ignore_cert_check: Option<bool>,
ssl_cert_file: Option<String>,
ssl_cert_file: Option<PathBuf>,
client: Client,
test_bucket: String,
}
Expand All @@ -92,7 +93,7 @@ impl ClientTest {
secret_key: String,
static_provider: StaticProvider,
ignore_cert_check: Option<bool>,
ssl_cert_file: Option<String>,
ssl_cert_file: Option<&Path>,
) -> ClientTest {
let client = Client::new(
base_url.clone(),
Expand All @@ -107,7 +108,7 @@ impl ClientTest {
access_key,
secret_key,
ignore_cert_check,
ssl_cert_file,
ssl_cert_file: ssl_cert_file.map(PathBuf::from),
client,
test_bucket: rand_bucket_name(),
}
Expand Down Expand Up @@ -537,10 +538,11 @@ impl ClientTest {

let listen_task = move || async move {
let static_provider = StaticProvider::new(&access_key, &secret_key, None);
let ssl_cert_file = &ssl_cert_file;
let client = Client::new(
base_url,
Some(Box::new(static_provider)),
ssl_cert_file,
ssl_cert_file.as_deref(),
ignore_cert_check,
)
.unwrap();
Expand Down Expand Up @@ -1150,7 +1152,7 @@ async fn s3_tests() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let value = std::env::var("SSL_CERT_FILE")?;
let mut ssl_cert_file = None;
if !value.is_empty() {
ssl_cert_file = Some(value);
ssl_cert_file = Some(Path::new(&value));
}
let ignore_cert_check = std::env::var("IGNORE_CERT_CHECK").is_ok();
let region = std::env::var("SERVER_REGION").ok();
Expand Down

0 comments on commit 018b348

Please sign in to comment.