This is an automated email from the ASF dual-hosted git repository. xuanwo pushed a commit to branch xuanwo/default-provider-api-huaweicloud-obs in repository https://gitbox.apache.org/repos/asf/opendal-reqsign.git
commit 06c9fdae91789ba1c74e2aef411f6a5eb2fa4d85 Author: Xuanwo <[email protected]> AuthorDate: Wed Mar 18 18:45:28 2026 +0800 feat(huaweicloud-obs): migrate default provider builder api --- .../src/provide_credential/default.rs | 60 +++++++++++++++------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/services/huaweicloud-obs/src/provide_credential/default.rs b/services/huaweicloud-obs/src/provide_credential/default.rs index 07b2f6e..403cd1b 100644 --- a/services/huaweicloud-obs/src/provide_credential/default.rs +++ b/services/huaweicloud-obs/src/provide_credential/default.rs @@ -77,36 +77,35 @@ impl DefaultCredentialProvider { /// Builder for `DefaultCredentialProvider`. /// -/// Use `configure_env` to customize environment loading and -/// `disable_env(bool)` to control participation, then `build()` to create the provider. -#[derive(Default)] +/// Use `env(...)` to customize the environment provider or `no_env()` to +/// remove it from the chain, then call `build()`. pub struct DefaultCredentialProviderBuilder { env: Option<EnvCredentialProvider>, } +impl Default for DefaultCredentialProviderBuilder { + fn default() -> Self { + Self { + env: Some(EnvCredentialProvider::default()), + } + } +} + impl DefaultCredentialProviderBuilder { /// Create a new builder with default state. pub fn new() -> Self { Self::default() } - /// Configure the environment credential provider. - pub fn configure_env<F>(mut self, f: F) -> Self - where - F: FnOnce(EnvCredentialProvider) -> EnvCredentialProvider, - { - let p = self.env.take().unwrap_or_default(); - self.env = Some(f(p)); + /// Set the environment credential provider. + pub fn env(mut self, provider: EnvCredentialProvider) -> Self { + self.env = Some(provider); self } - /// Disable (true) or ensure enabled (false) the environment provider. - pub fn disable_env(mut self, disable: bool) -> Self { - if disable { - self.env = None; - } else if self.env.is_none() { - self.env = Some(EnvCredentialProvider::new()); - } + /// Remove the environment credential provider from the chain. + pub fn no_env(mut self) -> Self { + self.env = None; self } @@ -115,8 +114,6 @@ impl DefaultCredentialProviderBuilder { let mut chain = ProvideCredentialChain::new(); if let Some(p) = self.env { chain = chain.push(p); - } else { - chain = chain.push(EnvCredentialProvider::new()); } DefaultCredentialProvider::with_chain(chain) } @@ -154,6 +151,31 @@ mod tests { assert!(credential.is_none()); } + #[tokio::test] + async fn test_builder_no_env_removes_provider() { + let ctx = Context::new() + .with_file_read(TokioFileRead) + .with_http_send(ReqwestHttpSend::default()); + let ctx = ctx.with_env(StaticEnv { + home_dir: None, + envs: HashMap::from_iter([ + ( + HUAWEI_CLOUD_ACCESS_KEY_ID.to_string(), + "access_key_id".to_string(), + ), + ( + HUAWEI_CLOUD_SECRET_ACCESS_KEY.to_string(), + "secret_access_key".to_string(), + ), + ]), + }); + + let loader = DefaultCredentialProvider::builder().no_env().build(); + let credential = loader.provide_credential(&ctx).await.unwrap(); + + assert!(credential.is_none()); + } + #[tokio::test] async fn test_default_loader_with_env() { let ctx = Context::new()
