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