aws_types/
sdk_config.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6#![deny(missing_docs)]
7
8//! AWS Shared Config
9//!
10//! This module contains a shared configuration representation that is agnostic from a specific service.
11
12use crate::app_name::AppName;
13use crate::docs_for;
14use crate::endpoint_config::AccountIdEndpointMode;
15use crate::origin::Origin;
16use crate::region::Region;
17use crate::service_config::LoadServiceConfig;
18use aws_credential_types::provider::token::SharedTokenProvider;
19pub use aws_credential_types::provider::SharedCredentialsProvider;
20use aws_smithy_async::rt::sleep::AsyncSleep;
21pub use aws_smithy_async::rt::sleep::SharedAsyncSleep;
22pub use aws_smithy_async::time::{SharedTimeSource, TimeSource};
23use aws_smithy_runtime_api::client::auth::AuthSchemePreference;
24use aws_smithy_runtime_api::client::behavior_version::BehaviorVersion;
25use aws_smithy_runtime_api::client::http::HttpClient;
26pub use aws_smithy_runtime_api::client::http::SharedHttpClient;
27use aws_smithy_runtime_api::client::identity::{ResolveCachedIdentity, SharedIdentityCache};
28pub use aws_smithy_runtime_api::client::stalled_stream_protection::StalledStreamProtectionConfig;
29use aws_smithy_runtime_api::shared::IntoShared;
30pub use aws_smithy_types::checksum_config::{
31    RequestChecksumCalculation, ResponseChecksumValidation,
32};
33pub use aws_smithy_types::retry::RetryConfig;
34pub use aws_smithy_types::timeout::TimeoutConfig;
35use std::collections::HashMap;
36use std::sync::Arc;
37
38/// Unified docstrings to keep crates in sync. Not intended for public use
39pub mod unified_docs {
40    /// A macro that generates docs for selected fields of `SdkConfig`.
41    #[macro_export]
42    macro_rules! docs_for {
43        (use_fips) => {
44"When true, send this request to the FIPS-compliant regional endpoint.
45
46If no FIPS-compliant endpoint can be determined, dispatching the request will return an error."
47        };
48        (use_dual_stack) => {
49"When true, send this request to the dual-stack endpoint.
50
51If no dual-stack endpoint is available the request MAY return an error.
52
53**Note**: Some services do not offer dual-stack as a configurable parameter (e.g. Code Catalyst). For
54these services, this setting has no effect"
55        };
56        (time_source) => {
57"The time source use to use for this client.
58
59This only needs to be required for creating deterministic tests or platforms where `SystemTime::now()` is not supported."};
60        (disable_request_compression) => {
61"When `true`, disable request compression. Defaults to `false`.
62
63**Only some services support request compression.** For services
64that don't support request compression, this setting does nothing.
65" };
66        (request_min_compression_size_bytes) => {
67"The minimum size of request that should be compressed. Defaults to `10240` bytes.
68
69When a request body's size is lower than this, request compression will be skipped.
70This is useful for request bodies because, for small request bodies, compression may actually increase their size.
71
72**Only some services support request compression.** For services
73that don't support request compression, this setting does nothing.
74" };
75        (account_id_endpoint_mode) => {
76"Controls the account ID-based routing behavior.
77
78By default, the routing behavior is set to `preferred`.
79Customers can adjust this setting to other values to switch between different routing patterns or temporarily disable the feature.
80
81See the developer guide on [account-based endpoints](https://docs.aws.amazon.com/sdkref/latest/guide/feature-account-endpoints.html)
82for more information.
83
84For services that do not use the account-based endpoints, this setting does nothing.
85" };
86        (auth_scheme_preference) => {
87"Set the auth scheme preference for an auth scheme resolver
88(typically the default auth scheme resolver).
89
90Each operation has a predefined order of auth schemes, as determined by the service,
91for auth scheme resolution. By using the auth scheme preference, customers
92can reorder the schemes resolved by the auth scheme resolver.
93
94The preference list is intended as a hint rather than a strict override.
95Any schemes not present in the originally resolved auth schemes will be ignored.
96" };
97    }
98}
99
100/// AWS Shared Configuration
101#[derive(Debug, Clone)]
102pub struct SdkConfig {
103    app_name: Option<AppName>,
104    auth_scheme_preference: Option<AuthSchemePreference>,
105    identity_cache: Option<SharedIdentityCache>,
106    credentials_provider: Option<SharedCredentialsProvider>,
107    token_provider: Option<SharedTokenProvider>,
108    region: Option<Region>,
109    account_id_endpoint_mode: Option<AccountIdEndpointMode>,
110    endpoint_url: Option<String>,
111    retry_config: Option<RetryConfig>,
112    sleep_impl: Option<SharedAsyncSleep>,
113    time_source: Option<SharedTimeSource>,
114    timeout_config: Option<TimeoutConfig>,
115    stalled_stream_protection_config: Option<StalledStreamProtectionConfig>,
116    http_client: Option<SharedHttpClient>,
117    use_fips: Option<bool>,
118    use_dual_stack: Option<bool>,
119    behavior_version: Option<BehaviorVersion>,
120    service_config: Option<Arc<dyn LoadServiceConfig>>,
121    config_origins: HashMap<&'static str, Origin>,
122    disable_request_compression: Option<bool>,
123    request_min_compression_size_bytes: Option<u32>,
124    request_checksum_calculation: Option<RequestChecksumCalculation>,
125    response_checksum_validation: Option<ResponseChecksumValidation>,
126}
127
128/// Builder for AWS Shared Configuration
129///
130/// _Important:_ Using the `aws-config` crate to configure the SDK is preferred to invoking this
131/// builder directly. Using this builder directly won't pull in any AWS recommended default
132/// configuration values.
133#[derive(Debug, Default)]
134pub struct Builder {
135    app_name: Option<AppName>,
136    auth_scheme_preference: Option<AuthSchemePreference>,
137    identity_cache: Option<SharedIdentityCache>,
138    credentials_provider: Option<SharedCredentialsProvider>,
139    token_provider: Option<SharedTokenProvider>,
140    region: Option<Region>,
141    account_id_endpoint_mode: Option<AccountIdEndpointMode>,
142    endpoint_url: Option<String>,
143    retry_config: Option<RetryConfig>,
144    sleep_impl: Option<SharedAsyncSleep>,
145    time_source: Option<SharedTimeSource>,
146    timeout_config: Option<TimeoutConfig>,
147    stalled_stream_protection_config: Option<StalledStreamProtectionConfig>,
148    http_client: Option<SharedHttpClient>,
149    use_fips: Option<bool>,
150    use_dual_stack: Option<bool>,
151    behavior_version: Option<BehaviorVersion>,
152    service_config: Option<Arc<dyn LoadServiceConfig>>,
153    config_origins: HashMap<&'static str, Origin>,
154    disable_request_compression: Option<bool>,
155    request_min_compression_size_bytes: Option<u32>,
156    request_checksum_calculation: Option<RequestChecksumCalculation>,
157    response_checksum_validation: Option<ResponseChecksumValidation>,
158}
159
160impl Builder {
161    /// Set the region for the builder
162    ///
163    /// # Examples
164    /// ```rust
165    /// use aws_types::SdkConfig;
166    /// use aws_types::region::Region;
167    /// let config = SdkConfig::builder().region(Region::new("us-east-1")).build();
168    /// ```
169    pub fn region(mut self, region: impl Into<Option<Region>>) -> Self {
170        self.set_region(region);
171        self
172    }
173
174    /// Set the region for the builder
175    ///
176    /// # Examples
177    /// ```rust
178    /// fn region_override() -> Option<Region> {
179    ///     // ...
180    ///     # None
181    /// }
182    /// use aws_types::SdkConfig;
183    /// use aws_types::region::Region;
184    /// let mut builder = SdkConfig::builder();
185    /// if let Some(region) = region_override() {
186    ///     builder.set_region(region);
187    /// }
188    /// let config = builder.build();
189    /// ```
190    pub fn set_region(&mut self, region: impl Into<Option<Region>>) -> &mut Self {
191        self.region = region.into();
192        self
193    }
194
195    #[doc = docs_for!(account_id_endpoint_mode)]
196    pub fn account_id_endpoint_mode(
197        mut self,
198        account_id_endpoint_mode: AccountIdEndpointMode,
199    ) -> Self {
200        self.set_account_id_endpoint_mode(Some(account_id_endpoint_mode));
201        self
202    }
203
204    #[doc = docs_for!(account_id_endpoint_mode)]
205    pub fn set_account_id_endpoint_mode(
206        &mut self,
207        account_id_endpoint_mode: Option<AccountIdEndpointMode>,
208    ) -> &mut Self {
209        self.account_id_endpoint_mode = account_id_endpoint_mode;
210        self
211    }
212
213    /// Set the endpoint URL to use when making requests.
214    /// # Examples
215    /// ```
216    /// use aws_types::SdkConfig;
217    /// let config = SdkConfig::builder().endpoint_url("http://localhost:8080").build();
218    /// ```
219    pub fn endpoint_url(mut self, endpoint_url: impl Into<String>) -> Self {
220        self.set_endpoint_url(Some(endpoint_url.into()));
221        self
222    }
223
224    /// Set the endpoint URL to use when making requests.
225    pub fn set_endpoint_url(&mut self, endpoint_url: Option<String>) -> &mut Self {
226        self.endpoint_url = endpoint_url;
227        self
228    }
229
230    /// Set the checksum calculation strategy to use when making requests.
231    /// # Examples
232    /// ```
233    /// use aws_types::SdkConfig;
234    /// use aws_smithy_types::checksum_config::RequestChecksumCalculation;
235    /// let config = SdkConfig::builder().request_checksum_calculation(RequestChecksumCalculation::WhenSupported).build();
236    /// ```
237    pub fn request_checksum_calculation(
238        mut self,
239        request_checksum_calculation: RequestChecksumCalculation,
240    ) -> Self {
241        self.set_request_checksum_calculation(Some(request_checksum_calculation));
242        self
243    }
244
245    /// Set the checksum calculation strategy to use when making requests.
246    pub fn set_request_checksum_calculation(
247        &mut self,
248        request_checksum_calculation: Option<RequestChecksumCalculation>,
249    ) -> &mut Self {
250        self.request_checksum_calculation = request_checksum_calculation;
251        self
252    }
253
254    /// Set the checksum calculation strategy to use for responses.
255    /// # Examples
256    /// ```
257    /// use aws_types::SdkConfig;
258    /// use aws_smithy_types::checksum_config::ResponseChecksumValidation;
259    /// let config = SdkConfig::builder().response_checksum_validation(ResponseChecksumValidation::WhenSupported).build();
260    /// ```
261    pub fn response_checksum_validation(
262        mut self,
263        response_checksum_validation: ResponseChecksumValidation,
264    ) -> Self {
265        self.set_response_checksum_validation(Some(response_checksum_validation));
266        self
267    }
268
269    /// Set the checksum calculation strategy to use for responses.
270    pub fn set_response_checksum_validation(
271        &mut self,
272        response_checksum_validation: Option<ResponseChecksumValidation>,
273    ) -> &mut Self {
274        self.response_checksum_validation = response_checksum_validation;
275        self
276    }
277
278    /// Set the retry_config for the builder
279    ///
280    /// _Note:_ Retries require a sleep implementation in order to work. When enabling retry, make
281    /// sure to set one with [Self::sleep_impl] or [Self::set_sleep_impl].
282    ///
283    /// # Examples
284    /// ```rust
285    /// use aws_types::SdkConfig;
286    /// use aws_smithy_types::retry::RetryConfig;
287    ///
288    /// let retry_config = RetryConfig::standard().with_max_attempts(5);
289    /// let config = SdkConfig::builder().retry_config(retry_config).build();
290    /// ```
291    pub fn retry_config(mut self, retry_config: RetryConfig) -> Self {
292        self.set_retry_config(Some(retry_config));
293        self
294    }
295
296    /// Set the retry_config for the builder
297    ///
298    /// _Note:_ Retries require a sleep implementation in order to work. When enabling retry, make
299    /// sure to set one with [Self::sleep_impl] or [Self::set_sleep_impl].
300    ///
301    /// # Examples
302    /// ```rust
303    /// use aws_types::sdk_config::{SdkConfig, Builder};
304    /// use aws_smithy_types::retry::RetryConfig;
305    ///
306    /// fn disable_retries(builder: &mut Builder) {
307    ///     let retry_config = RetryConfig::standard().with_max_attempts(1);
308    ///     builder.set_retry_config(Some(retry_config));
309    /// }
310    ///
311    /// let mut builder = SdkConfig::builder();
312    /// disable_retries(&mut builder);
313    /// ```
314    pub fn set_retry_config(&mut self, retry_config: Option<RetryConfig>) -> &mut Self {
315        self.retry_config = retry_config;
316        self
317    }
318
319    /// Set the [`TimeoutConfig`] for the builder
320    ///
321    /// _Note:_ Timeouts require a sleep implementation in order to work.
322    /// When enabling timeouts, be sure to set one with [Self::sleep_impl] or
323    /// [Self::set_sleep_impl].
324    ///
325    /// # Examples
326    ///
327    /// ```rust
328    /// # use std::time::Duration;
329    /// use aws_types::SdkConfig;
330    /// use aws_smithy_types::timeout::TimeoutConfig;
331    ///
332    /// let timeout_config = TimeoutConfig::builder()
333    ///     .operation_attempt_timeout(Duration::from_secs(2))
334    ///     .operation_timeout(Duration::from_secs(5))
335    ///     .build();
336    /// let config = SdkConfig::builder()
337    ///     .timeout_config(timeout_config)
338    ///     .build();
339    /// ```
340    pub fn timeout_config(mut self, timeout_config: TimeoutConfig) -> Self {
341        self.set_timeout_config(Some(timeout_config));
342        self
343    }
344
345    /// Set the [`TimeoutConfig`] for the builder
346    ///
347    /// _Note:_ Timeouts require a sleep implementation in order to work.
348    /// When enabling timeouts, be sure to set one with [Self::sleep_impl] or
349    /// [Self::set_sleep_impl].
350    ///
351    /// # Examples
352    /// ```rust
353    /// # use std::time::Duration;
354    /// use aws_types::sdk_config::{SdkConfig, Builder};
355    /// use aws_smithy_types::timeout::TimeoutConfig;
356    ///
357    /// fn set_preferred_timeouts(builder: &mut Builder) {
358    ///     let timeout_config = TimeoutConfig::builder()
359    ///         .operation_attempt_timeout(Duration::from_secs(2))
360    ///         .operation_timeout(Duration::from_secs(5))
361    ///         .build();
362    ///     builder.set_timeout_config(Some(timeout_config));
363    /// }
364    ///
365    /// let mut builder = SdkConfig::builder();
366    /// set_preferred_timeouts(&mut builder);
367    /// let config = builder.build();
368    /// ```
369    pub fn set_timeout_config(&mut self, timeout_config: Option<TimeoutConfig>) -> &mut Self {
370        self.timeout_config = timeout_config;
371        self
372    }
373
374    /// Set the sleep implementation for the builder.
375    ///
376    /// The sleep implementation is used to create timeout futures.
377    ///
378    /// _Note:_ If you're using the Tokio runtime, a `TokioSleep` implementation is available in
379    /// the `aws-smithy-async` crate.
380    ///
381    /// # Examples
382    ///
383    /// ```rust
384    /// use aws_smithy_async::rt::sleep::{AsyncSleep, SharedAsyncSleep, Sleep};
385    /// use aws_types::SdkConfig;
386    ///
387    /// ##[derive(Debug)]
388    /// pub struct ForeverSleep;
389    ///
390    /// impl AsyncSleep for ForeverSleep {
391    ///     fn sleep(&self, duration: std::time::Duration) -> Sleep {
392    ///         Sleep::new(std::future::pending())
393    ///     }
394    /// }
395    ///
396    /// let sleep_impl = SharedAsyncSleep::new(ForeverSleep);
397    /// let config = SdkConfig::builder().sleep_impl(sleep_impl).build();
398    /// ```
399    pub fn sleep_impl(mut self, sleep_impl: impl AsyncSleep + 'static) -> Self {
400        self.set_sleep_impl(Some(sleep_impl.into_shared()));
401        self
402    }
403
404    /// Set the sleep implementation for the builder. The sleep implementation is used to create
405    /// timeout futures.
406    ///
407    /// _Note:_ If you're using the Tokio runtime, a `TokioSleep` implementation is available in
408    /// the `aws-smithy-async` crate.
409    ///
410    /// # Examples
411    /// ```rust
412    /// # use aws_smithy_async::rt::sleep::{AsyncSleep, SharedAsyncSleep, Sleep};
413    /// # use aws_types::sdk_config::{Builder, SdkConfig};
414    /// #[derive(Debug)]
415    /// pub struct ForeverSleep;
416    ///
417    /// impl AsyncSleep for ForeverSleep {
418    ///     fn sleep(&self, duration: std::time::Duration) -> Sleep {
419    ///         Sleep::new(std::future::pending())
420    ///     }
421    /// }
422    ///
423    /// fn set_never_ending_sleep_impl(builder: &mut Builder) {
424    ///     let sleep_impl = SharedAsyncSleep::new(ForeverSleep);
425    ///     builder.set_sleep_impl(Some(sleep_impl));
426    /// }
427    ///
428    /// let mut builder = SdkConfig::builder();
429    /// set_never_ending_sleep_impl(&mut builder);
430    /// let config = builder.build();
431    /// ```
432    pub fn set_sleep_impl(&mut self, sleep_impl: Option<SharedAsyncSleep>) -> &mut Self {
433        self.sleep_impl = sleep_impl;
434        self
435    }
436
437    /// Set the identity cache for caching credentials and SSO tokens.
438    ///
439    /// The default identity cache will wait until the first request that requires authentication
440    /// to load an identity. Once the identity is loaded, it is cached until shortly before it
441    /// expires.
442    ///
443    /// # Examples
444    /// Disabling identity caching:
445    /// ```rust
446    /// # use aws_types::SdkConfig;
447    /// use aws_smithy_runtime::client::identity::IdentityCache;
448    /// let config = SdkConfig::builder()
449    ///     .identity_cache(IdentityCache::no_cache())
450    ///     .build();
451    /// ```
452    /// Changing settings on the default cache implementation:
453    /// ```rust
454    /// # use aws_types::SdkConfig;
455    /// use aws_smithy_runtime::client::identity::IdentityCache;
456    /// use std::time::Duration;
457    ///
458    /// let config = SdkConfig::builder()
459    ///     .identity_cache(
460    ///         IdentityCache::lazy()
461    ///             .load_timeout(Duration::from_secs(10))
462    ///             .build()
463    ///     )
464    ///     .build();
465    /// ```
466    pub fn identity_cache(mut self, cache: impl ResolveCachedIdentity + 'static) -> Self {
467        self.set_identity_cache(Some(cache.into_shared()));
468        self
469    }
470
471    /// Set the identity cache for caching credentials and SSO tokens.
472    ///
473    /// The default identity cache will wait until the first request that requires authentication
474    /// to load an identity. Once the identity is loaded, it is cached until shortly before it
475    /// expires.
476    ///
477    /// # Examples
478    /// ```rust
479    /// # use aws_types::SdkConfig;
480    /// use aws_smithy_runtime::client::identity::IdentityCache;
481    ///
482    /// fn override_identity_cache() -> bool {
483    ///   // ...
484    ///   # true
485    /// }
486    ///
487    /// let mut builder = SdkConfig::builder();
488    /// if override_identity_cache() {
489    ///     builder.set_identity_cache(Some(IdentityCache::lazy().build()));
490    /// }
491    /// let config = builder.build();
492    /// ```
493    pub fn set_identity_cache(&mut self, cache: Option<SharedIdentityCache>) -> &mut Self {
494        self.identity_cache = cache;
495        self
496    }
497
498    /// Set the credentials provider for the builder
499    ///
500    /// # Examples
501    /// ```rust
502    /// use aws_credential_types::provider::{ProvideCredentials, SharedCredentialsProvider};
503    /// use aws_types::SdkConfig;
504    /// fn make_provider() -> impl ProvideCredentials {
505    ///   // ...
506    ///   # use aws_credential_types::Credentials;
507    ///   # Credentials::new("test", "test", None, None, "example")
508    /// }
509    ///
510    /// let config = SdkConfig::builder()
511    ///     .credentials_provider(SharedCredentialsProvider::new(make_provider()))
512    ///     .build();
513    /// ```
514    pub fn credentials_provider(mut self, provider: SharedCredentialsProvider) -> Self {
515        self.set_credentials_provider(Some(provider));
516        self
517    }
518
519    /// Set the credentials provider for the builder
520    ///
521    /// # Examples
522    /// ```rust
523    /// use aws_credential_types::provider::{ProvideCredentials, SharedCredentialsProvider};
524    /// use aws_types::SdkConfig;
525    /// fn make_provider() -> impl ProvideCredentials {
526    ///   // ...
527    ///   # use aws_credential_types::Credentials;
528    ///   # Credentials::new("test", "test", None, None, "example")
529    /// }
530    ///
531    /// fn override_provider() -> bool {
532    ///   // ...
533    ///   # true
534    /// }
535    ///
536    /// let mut builder = SdkConfig::builder();
537    /// if override_provider() {
538    ///     builder.set_credentials_provider(Some(SharedCredentialsProvider::new(make_provider())));
539    /// }
540    /// let config = builder.build();
541    /// ```
542    pub fn set_credentials_provider(
543        &mut self,
544        provider: Option<SharedCredentialsProvider>,
545    ) -> &mut Self {
546        self.credentials_provider = provider;
547        self
548    }
549
550    /// Set the bearer auth token provider for the builder
551    ///
552    /// # Examples
553    /// ```rust
554    /// use aws_credential_types::provider::token::{ProvideToken, SharedTokenProvider};
555    /// use aws_types::SdkConfig;
556    ///
557    /// fn make_provider() -> impl ProvideToken {
558    ///   // ...
559    ///   # aws_credential_types::Token::new("example", None)
560    /// }
561    ///
562    /// let config = SdkConfig::builder()
563    ///     .token_provider(SharedTokenProvider::new(make_provider()))
564    ///     .build();
565    /// ```
566    pub fn token_provider(mut self, provider: SharedTokenProvider) -> Self {
567        self.set_token_provider(Some(provider));
568        self
569    }
570
571    /// Set the bearer auth token provider for the builder
572    ///
573    /// # Examples
574    /// ```rust
575    /// use aws_credential_types::provider::token::{ProvideToken, SharedTokenProvider};
576    /// use aws_types::SdkConfig;
577    ///
578    /// fn make_provider() -> impl ProvideToken {
579    ///   // ...
580    ///   # aws_credential_types::Token::new("example", None)
581    /// }
582    ///
583    /// fn override_provider() -> bool {
584    ///   // ...
585    ///   # true
586    /// }
587    ///
588    /// let mut builder = SdkConfig::builder();
589    /// if override_provider() {
590    ///     builder.set_token_provider(Some(SharedTokenProvider::new(make_provider())));
591    /// }
592    /// let config = builder.build();
593    /// ```
594    pub fn set_token_provider(&mut self, provider: Option<SharedTokenProvider>) -> &mut Self {
595        self.token_provider = provider;
596        self
597    }
598
599    /// Sets the name of the app that is using the client.
600    ///
601    /// This _optional_ name is used to identify the application in the user agent that
602    /// gets sent along with requests.
603    pub fn app_name(mut self, app_name: AppName) -> Self {
604        self.set_app_name(Some(app_name));
605        self
606    }
607
608    /// Sets the name of the app that is using the client.
609    ///
610    /// This _optional_ name is used to identify the application in the user agent that
611    /// gets sent along with requests.
612    pub fn set_app_name(&mut self, app_name: Option<AppName>) -> &mut Self {
613        self.app_name = app_name;
614        self
615    }
616
617    /// Sets the HTTP client to use when making requests.
618    ///
619    /// ## Examples
620    /// ```no_run
621    /// # #[cfg(feature = "examples")]
622    /// # fn example() {
623    /// use aws_types::sdk_config::{SdkConfig, TimeoutConfig};
624    /// use aws_smithy_runtime::client::http::hyper_014::HyperClientBuilder;
625    /// use std::time::Duration;
626    ///
627    /// // Create a connector that will be used to establish TLS connections
628    /// let tls_connector = hyper_rustls::HttpsConnectorBuilder::new()
629    ///     .with_webpki_roots()
630    ///     .https_only()
631    ///     .enable_http1()
632    ///     .enable_http2()
633    ///     .build();
634    /// // Create a HTTP client that uses the TLS connector. This client is
635    /// // responsible for creating and caching a HttpConnector when given HttpConnectorSettings.
636    /// // This hyper client will create HttpConnectors backed by hyper and the tls_connector.
637    /// let http_client = HyperClientBuilder::new().build(tls_connector);
638    /// let sdk_config = SdkConfig::builder()
639    ///     .http_client(http_client)
640    ///     // Connect/read timeouts are passed to the HTTP client when servicing a request
641    ///     .timeout_config(
642    ///         TimeoutConfig::builder()
643    ///             .connect_timeout(Duration::from_secs(5))
644    ///             .build()
645    ///     )
646    ///     .build();
647    /// # }
648    /// ```
649    pub fn http_client(mut self, http_client: impl HttpClient + 'static) -> Self {
650        self.set_http_client(Some(http_client.into_shared()));
651        self
652    }
653
654    /// Sets the HTTP client to use when making requests.
655    ///
656    /// ## Examples
657    /// ```no_run
658    /// # #[cfg(feature = "examples")]
659    /// # fn example() {
660    /// use aws_types::sdk_config::{Builder, SdkConfig, TimeoutConfig};
661    /// use aws_smithy_runtime::client::http::hyper_014::HyperClientBuilder;
662    /// use std::time::Duration;
663    ///
664    /// fn override_http_client(builder: &mut Builder) {
665    ///     // Create a connector that will be used to establish TLS connections
666    ///     let tls_connector = hyper_rustls::HttpsConnectorBuilder::new()
667    ///         .with_webpki_roots()
668    ///         .https_only()
669    ///         .enable_http1()
670    ///         .enable_http2()
671    ///         .build();
672    ///     // Create a HTTP client that uses the TLS connector. This client is
673    ///     // responsible for creating and caching a HttpConnector when given HttpConnectorSettings.
674    ///     // This hyper client will create HttpConnectors backed by hyper and the tls_connector.
675    ///     let http_client = HyperClientBuilder::new().build(tls_connector);
676    ///
677    ///     builder.set_http_client(Some(http_client));
678    /// }
679    ///
680    /// let mut builder = SdkConfig::builder();
681    /// override_http_client(&mut builder);
682    /// let config = builder.build();
683    /// # }
684    /// ```
685    pub fn set_http_client(&mut self, http_client: Option<SharedHttpClient>) -> &mut Self {
686        self.http_client = http_client;
687        self
688    }
689
690    #[doc = docs_for!(use_fips)]
691    pub fn use_fips(mut self, use_fips: bool) -> Self {
692        self.set_use_fips(Some(use_fips));
693        self
694    }
695
696    #[doc = docs_for!(use_fips)]
697    pub fn set_use_fips(&mut self, use_fips: Option<bool>) -> &mut Self {
698        self.use_fips = use_fips;
699        self
700    }
701
702    #[doc = docs_for!(use_dual_stack)]
703    pub fn use_dual_stack(mut self, use_dual_stack: bool) -> Self {
704        self.set_use_dual_stack(Some(use_dual_stack));
705        self
706    }
707
708    #[doc = docs_for!(use_dual_stack)]
709    pub fn set_use_dual_stack(&mut self, use_dual_stack: Option<bool>) -> &mut Self {
710        self.use_dual_stack = use_dual_stack;
711        self
712    }
713
714    #[doc = docs_for!(time_source)]
715    pub fn time_source(mut self, time_source: impl TimeSource + 'static) -> Self {
716        self.set_time_source(Some(SharedTimeSource::new(time_source)));
717        self
718    }
719
720    #[doc = docs_for!(time_source)]
721    pub fn set_time_source(&mut self, time_source: Option<SharedTimeSource>) -> &mut Self {
722        self.time_source = time_source;
723        self
724    }
725
726    #[doc = docs_for!(disable_request_compression)]
727    pub fn disable_request_compression(mut self, disable_request_compression: bool) -> Self {
728        self.set_disable_request_compression(Some(disable_request_compression));
729        self
730    }
731
732    #[doc = docs_for!(disable_request_compression)]
733    pub fn set_disable_request_compression(
734        &mut self,
735        disable_request_compression: Option<bool>,
736    ) -> &mut Self {
737        self.disable_request_compression = disable_request_compression;
738        self
739    }
740
741    #[doc = docs_for!(request_min_compression_size_bytes)]
742    pub fn request_min_compression_size_bytes(
743        mut self,
744        request_min_compression_size_bytes: u32,
745    ) -> Self {
746        self.set_request_min_compression_size_bytes(Some(request_min_compression_size_bytes));
747        self
748    }
749
750    #[doc = docs_for!(request_min_compression_size_bytes)]
751    pub fn set_request_min_compression_size_bytes(
752        &mut self,
753        request_min_compression_size_bytes: Option<u32>,
754    ) -> &mut Self {
755        self.request_min_compression_size_bytes = request_min_compression_size_bytes;
756        self
757    }
758
759    /// Sets the [`BehaviorVersion`] for the [`SdkConfig`]
760    pub fn behavior_version(mut self, behavior_version: BehaviorVersion) -> Self {
761        self.set_behavior_version(Some(behavior_version));
762        self
763    }
764
765    /// Sets the [`BehaviorVersion`] for the [`SdkConfig`]
766    pub fn set_behavior_version(&mut self, behavior_version: Option<BehaviorVersion>) -> &mut Self {
767        self.behavior_version = behavior_version;
768        self
769    }
770
771    /// Sets the service config provider for the [`SdkConfig`].
772    ///
773    /// This provider is used when creating a service-specific config from an
774    /// `SdkConfig` and provides access to config defined in the environment
775    /// which would otherwise be inaccessible.
776    pub fn service_config(mut self, service_config: impl LoadServiceConfig + 'static) -> Self {
777        self.set_service_config(Some(service_config));
778        self
779    }
780
781    /// Sets the service config provider for the [`SdkConfig`].
782    ///
783    /// This provider is used when creating a service-specific config from an
784    /// `SdkConfig` and provides access to config defined in the environment
785    /// which would otherwise be inaccessible.
786    pub fn set_service_config(
787        &mut self,
788        service_config: Option<impl LoadServiceConfig + 'static>,
789    ) -> &mut Self {
790        self.service_config = service_config.map(|it| Arc::new(it) as Arc<dyn LoadServiceConfig>);
791        self
792    }
793
794    #[doc = docs_for!(auth_scheme_preference)]
795    pub fn auth_scheme_preference(
796        mut self,
797        auth_scheme_preference: impl Into<AuthSchemePreference>,
798    ) -> Self {
799        self.set_auth_scheme_preference(Some(auth_scheme_preference));
800        self
801    }
802
803    #[doc = docs_for!(auth_scheme_preference)]
804    pub fn set_auth_scheme_preference(
805        &mut self,
806        auth_scheme_preference: Option<impl Into<AuthSchemePreference>>,
807    ) -> &mut Self {
808        self.auth_scheme_preference = auth_scheme_preference.map(|pref| pref.into());
809        self
810    }
811
812    /// Set the origin of a setting.
813    ///
814    /// This is used internally to understand how to merge config structs while
815    /// respecting precedence of origins.
816    pub fn insert_origin(&mut self, setting: &'static str, origin: Origin) {
817        self.config_origins.insert(setting, origin);
818    }
819
820    /// Build a [`SdkConfig`] from this builder.
821    pub fn build(self) -> SdkConfig {
822        SdkConfig {
823            app_name: self.app_name,
824            auth_scheme_preference: self.auth_scheme_preference,
825            identity_cache: self.identity_cache,
826            credentials_provider: self.credentials_provider,
827            token_provider: self.token_provider,
828            region: self.region,
829            account_id_endpoint_mode: self.account_id_endpoint_mode,
830            endpoint_url: self.endpoint_url,
831            retry_config: self.retry_config,
832            sleep_impl: self.sleep_impl,
833            timeout_config: self.timeout_config,
834            http_client: self.http_client,
835            use_fips: self.use_fips,
836            use_dual_stack: self.use_dual_stack,
837            time_source: self.time_source,
838            behavior_version: self.behavior_version,
839            stalled_stream_protection_config: self.stalled_stream_protection_config,
840            service_config: self.service_config,
841            config_origins: self.config_origins,
842            disable_request_compression: self.disable_request_compression,
843            request_min_compression_size_bytes: self.request_min_compression_size_bytes,
844            request_checksum_calculation: self.request_checksum_calculation,
845            response_checksum_validation: self.response_checksum_validation,
846        }
847    }
848}
849
850impl Builder {
851    /// Set the [`StalledStreamProtectionConfig`] to configure protection for stalled streams.
852    ///
853    /// This configures stalled stream protection. When enabled, download streams
854    /// that stall (stream no data) for longer than a configured grace period will return an error.
855    ///
856    /// _Note:_ Stalled stream protection requires both a sleep implementation and a time source
857    /// in order to work. When enabling stalled stream protection, make sure to set
858    /// - A sleep impl with [Self::sleep_impl] or [Self::set_sleep_impl].
859    /// - A time source with [Self::time_source] or [Self::set_time_source].
860    ///
861    /// # Examples
862    /// ```rust
863    /// use std::time::Duration;
864    /// use aws_types::SdkConfig;
865    /// pub use aws_smithy_runtime_api::client::stalled_stream_protection::StalledStreamProtectionConfig;
866    ///
867    /// let stalled_stream_protection_config = StalledStreamProtectionConfig::enabled()
868    ///     .grace_period(Duration::from_secs(1))
869    ///     .build();
870    /// let config = SdkConfig::builder()
871    ///     .stalled_stream_protection(stalled_stream_protection_config)
872    ///     .build();
873    /// ```
874    pub fn stalled_stream_protection(
875        mut self,
876        stalled_stream_protection_config: StalledStreamProtectionConfig,
877    ) -> Self {
878        self.set_stalled_stream_protection(Some(stalled_stream_protection_config));
879        self
880    }
881
882    /// Set the [`StalledStreamProtectionConfig`] to configure protection for stalled streams.
883    ///
884    /// This configures stalled stream protection. When enabled, download streams
885    /// that stall (stream no data) for longer than a configured grace period will return an error.
886    ///
887    /// By default, streams that transmit less than one byte per-second for five seconds will
888    /// be cancelled.
889    ///
890    /// _Note:_ Stalled stream protection requires both a sleep implementation and a time source
891    /// in order to work. When enabling stalled stream protection, make sure to set
892    /// - A sleep impl with [Self::sleep_impl] or [Self::set_sleep_impl].
893    /// - A time source with [Self::time_source] or [Self::set_time_source].
894    ///
895    /// # Examples
896    /// ```rust
897    /// use std::time::Duration;
898    /// use aws_types::sdk_config::{SdkConfig, Builder};
899    /// pub use aws_smithy_runtime_api::client::stalled_stream_protection::StalledStreamProtectionConfig;
900    ///
901    /// fn set_stalled_stream_protection(builder: &mut Builder) {
902    ///     let stalled_stream_protection_config = StalledStreamProtectionConfig::enabled()
903    ///         .grace_period(Duration::from_secs(1))
904    ///         .build();
905    ///     builder.set_stalled_stream_protection(Some(stalled_stream_protection_config));
906    /// }
907    ///
908    /// let mut builder = SdkConfig::builder();
909    /// set_stalled_stream_protection(&mut builder);
910    /// let config = builder.build();
911    /// ```
912    pub fn set_stalled_stream_protection(
913        &mut self,
914        stalled_stream_protection_config: Option<StalledStreamProtectionConfig>,
915    ) -> &mut Self {
916        self.stalled_stream_protection_config = stalled_stream_protection_config;
917        self
918    }
919}
920
921impl SdkConfig {
922    /// Configured region
923    pub fn region(&self) -> Option<&Region> {
924        self.region.as_ref()
925    }
926
927    /// Configured account ID endpoint mode
928    pub fn account_id_endpoint_mode(&self) -> Option<&AccountIdEndpointMode> {
929        self.account_id_endpoint_mode.as_ref()
930    }
931
932    /// Configured auth scheme preference
933    pub fn auth_scheme_preference(&self) -> Option<&AuthSchemePreference> {
934        self.auth_scheme_preference.as_ref()
935    }
936
937    /// Configured endpoint URL
938    pub fn endpoint_url(&self) -> Option<&str> {
939        self.endpoint_url.as_deref()
940    }
941
942    /// Configured retry config
943    pub fn retry_config(&self) -> Option<&RetryConfig> {
944        self.retry_config.as_ref()
945    }
946
947    /// Configured timeout config
948    pub fn timeout_config(&self) -> Option<&TimeoutConfig> {
949        self.timeout_config.as_ref()
950    }
951
952    /// Configured sleep implementation
953    pub fn sleep_impl(&self) -> Option<SharedAsyncSleep> {
954        self.sleep_impl.clone()
955    }
956
957    /// Configured identity cache
958    pub fn identity_cache(&self) -> Option<SharedIdentityCache> {
959        self.identity_cache.clone()
960    }
961
962    /// Configured credentials provider
963    pub fn credentials_provider(&self) -> Option<SharedCredentialsProvider> {
964        self.credentials_provider.clone()
965    }
966
967    /// Configured bearer auth token provider
968    pub fn token_provider(&self) -> Option<SharedTokenProvider> {
969        self.token_provider.clone()
970    }
971
972    /// Configured time source
973    pub fn time_source(&self) -> Option<SharedTimeSource> {
974        self.time_source.clone()
975    }
976
977    /// Configured app name
978    pub fn app_name(&self) -> Option<&AppName> {
979        self.app_name.as_ref()
980    }
981
982    /// Configured HTTP client
983    pub fn http_client(&self) -> Option<SharedHttpClient> {
984        self.http_client.clone()
985    }
986
987    /// Use FIPS endpoints
988    pub fn use_fips(&self) -> Option<bool> {
989        self.use_fips
990    }
991
992    /// Use dual-stack endpoint
993    pub fn use_dual_stack(&self) -> Option<bool> {
994        self.use_dual_stack
995    }
996
997    /// When true, request compression is disabled.
998    pub fn disable_request_compression(&self) -> Option<bool> {
999        self.disable_request_compression
1000    }
1001
1002    /// Configured checksum request behavior.
1003    pub fn request_checksum_calculation(&self) -> Option<RequestChecksumCalculation> {
1004        self.request_checksum_calculation
1005    }
1006
1007    /// Configured checksum response behavior.
1008    pub fn response_checksum_validation(&self) -> Option<ResponseChecksumValidation> {
1009        self.response_checksum_validation
1010    }
1011
1012    /// Configured minimum request compression size.
1013    pub fn request_min_compression_size_bytes(&self) -> Option<u32> {
1014        self.request_min_compression_size_bytes
1015    }
1016
1017    /// Configured stalled stream protection
1018    pub fn stalled_stream_protection(&self) -> Option<StalledStreamProtectionConfig> {
1019        self.stalled_stream_protection_config.clone()
1020    }
1021
1022    /// Behavior version configured for this client
1023    pub fn behavior_version(&self) -> Option<BehaviorVersion> {
1024        self.behavior_version
1025    }
1026
1027    /// Return an immutable reference to the service config provider configured for this client.
1028    pub fn service_config(&self) -> Option<&dyn LoadServiceConfig> {
1029        self.service_config.as_deref()
1030    }
1031
1032    /// Config builder
1033    ///
1034    /// _Important:_ Using the `aws-config` crate to configure the SDK is preferred to invoking this
1035    /// builder directly. Using this builder directly won't pull in any AWS recommended default
1036    /// configuration values.
1037    pub fn builder() -> Builder {
1038        Builder::default()
1039    }
1040
1041    /// Convert this [`SdkConfig`] into a [`Builder`] by cloning it first
1042    pub fn to_builder(&self) -> Builder {
1043        self.clone().into_builder()
1044    }
1045
1046    /// Get the origin of a setting.
1047    ///
1048    /// This is used internally to understand how to merge config structs while
1049    /// respecting precedence of origins.
1050    pub fn get_origin(&self, setting: &'static str) -> Origin {
1051        self.config_origins
1052            .get(setting)
1053            .cloned()
1054            .unwrap_or_default()
1055    }
1056
1057    /// Convert this [`SdkConfig`] back to a builder to enable modification
1058    pub fn into_builder(self) -> Builder {
1059        Builder {
1060            app_name: self.app_name,
1061            auth_scheme_preference: self.auth_scheme_preference,
1062            identity_cache: self.identity_cache,
1063            credentials_provider: self.credentials_provider,
1064            token_provider: self.token_provider,
1065            region: self.region,
1066            account_id_endpoint_mode: self.account_id_endpoint_mode,
1067            endpoint_url: self.endpoint_url,
1068            retry_config: self.retry_config,
1069            sleep_impl: self.sleep_impl,
1070            time_source: self.time_source,
1071            timeout_config: self.timeout_config,
1072            http_client: self.http_client,
1073            use_fips: self.use_fips,
1074            use_dual_stack: self.use_dual_stack,
1075            behavior_version: self.behavior_version,
1076            stalled_stream_protection_config: self.stalled_stream_protection_config,
1077            service_config: self.service_config,
1078            config_origins: self.config_origins,
1079            disable_request_compression: self.disable_request_compression,
1080            request_min_compression_size_bytes: self.request_min_compression_size_bytes,
1081            request_checksum_calculation: self.request_checksum_calculation,
1082            response_checksum_validation: self.response_checksum_validation,
1083        }
1084    }
1085}