Skip to content

Commit

Permalink
Remove lifetime parameter from client
Browse files Browse the repository at this point in the history
This change lets the Client struct take ownership of the Provider trait
object so that we are remove the lifetime parameter from the Client.

This change simplifies usage of the Client object. Without this it is
difficult to pass the Client object to a thread.
  • Loading branch information
donatello committed Sep 25, 2023
1 parent aee702f commit 204c4e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/s3/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,18 @@ fn parse_list_objects_common_prefixes(
Ok(())
}

#[derive(Clone, Debug, Default)]
pub struct Client<'a> {
#[derive(Debug, Default)]
pub struct Client {
client: reqwest::Client,
base_url: BaseUrl,
provider: Option<&'a (dyn Provider + Send + Sync)>,
provider: Option<Box<(dyn Provider + Send + Sync + 'static)>>,
region_map: DashMap<String, String>,
}

impl<'a> Client<'a> {
impl Client {
pub fn new(
base_url: BaseUrl,
provider: Option<&(dyn Provider + Send + Sync)>,
provider: Option<Box<(dyn Provider + Send + Sync + 'static)>>,
ssl_cert_file: Option<String>,
ignore_cert_check: Option<bool>,
) -> Result<Client, Error> {
Expand Down Expand Up @@ -295,7 +295,7 @@ impl<'a> Client<'a> {
let date = utc_now();
headers.insert(String::from("x-amz-date"), to_amz_date(date));

if let Some(p) = self.provider {
if let Some(p) = &self.provider {
let creds = p.fetch();
if creds.session_token.is_some() {
headers.insert(
Expand Down Expand Up @@ -773,7 +773,7 @@ impl<'a> Client<'a> {
})
}

async fn calculate_part_count(
async fn calculate_part_count<'a>(
&self,
sources: &'a mut Vec<ComposeSource<'_>>,
) -> Result<u16, Error> {
Expand Down Expand Up @@ -2180,7 +2180,7 @@ impl<'a> Client<'a> {
Some(args.object),
)?;

if let Some(p) = self.provider {
if let Some(p) = &self.provider {
let creds = p.fetch();
if let Some(t) = creds.session_token {
query_params.insert(String::from("X-Amz-Security-Token"), t);
Expand Down Expand Up @@ -2226,7 +2226,7 @@ impl<'a> Client<'a> {
}

let region = self.get_region(policy.bucket, policy.region).await?;
let creds = self.provider.unwrap().fetch();
let creds = self.provider.as_ref().unwrap().fetch();
policy.form_data(
creds.access_key,
creds.secret_key,
Expand Down
16 changes: 8 additions & 8 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,30 @@ fn rand_object_name() -> String {
Alphanumeric.sample_string(&mut rand::thread_rng(), 8)
}

struct ClientTest<'a> {
struct ClientTest {
base_url: BaseUrl,
access_key: String,
secret_key: String,
ignore_cert_check: Option<bool>,
ssl_cert_file: Option<String>,
client: Client<'a>,
client: Client,
test_bucket: String,
}

impl<'a> ClientTest<'_> {
impl ClientTest {
const SQS_ARN: &str = "arn:minio:sqs::miniojavatest:webhook";

fn new(
base_url: BaseUrl,
access_key: String,
secret_key: String,
static_provider: &'a StaticProvider,
static_provider: StaticProvider,
ignore_cert_check: Option<bool>,
ssl_cert_file: Option<String>,
) -> ClientTest<'a> {
) -> ClientTest {
let client = Client::new(
base_url.clone(),
Some(static_provider),
Some(Box::new(static_provider)),
ssl_cert_file.as_ref().cloned(),
ignore_cert_check,
)
Expand Down Expand Up @@ -539,7 +539,7 @@ impl<'a> ClientTest<'_> {
let static_provider = StaticProvider::new(&access_key, &secret_key, None);
let client = Client::new(
base_url,
Some(&static_provider),
Some(Box::new(static_provider)),
ssl_cert_file,
ignore_cert_check,
)
Expand Down Expand Up @@ -1162,7 +1162,7 @@ async fn s3_tests() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
base_url,
access_key,
secret_key,
&static_provider,
static_provider,
Some(ignore_cert_check),
ssl_cert_file,
);
Expand Down

0 comments on commit 204c4e3

Please sign in to comment.