-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add default client configurations #1312
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
Sources/Core/AWSClientRuntime/AWSClientConfigDefaultsProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
import ClientRuntime | ||
|
||
typealias RuntimeConfigType = DefaultSDKRuntimeConfiguration<DefaultRetryStrategy, DefaultRetryErrorInfoProvider> | ||
|
||
/// Provides default configuration properties for AWS services. | ||
public class AWSClientConfigDefaultsProvider { | ||
|
||
public static let httpClientEngine: HTTPClient = RuntimeConfigType.makeClient( | ||
httpClientConfiguration: RuntimeConfigType.defaultHttpClientConfiguration) | ||
|
||
public static let httpClientConfiguration: HttpClientConfiguration | ||
= RuntimeConfigType.defaultHttpClientConfiguration | ||
|
||
public static let idempotencyTokenGenerator: IdempotencyTokenGenerator | ||
= RuntimeConfigType.defaultIdempotencyTokenGenerator | ||
|
||
public static func logger(_ clientName: String) -> LogAgent { | ||
return RuntimeConfigType.defaultLogger(clientName: clientName) | ||
} | ||
|
||
public static let clientLogMode: ClientLogMode = RuntimeConfigType.defaultClientLogMode | ||
|
||
public static func credentialsProvider( | ||
_ credentialsProvider: AWSClientRuntime.CredentialsProviding? = nil) throws -> CredentialsProviding { | ||
let resolvedCredentialsProvider: AWSClientRuntime.CredentialsProviding | ||
let fileBasedConfig = try CRTFileBasedConfiguration.make() | ||
if let credentialsProvider = credentialsProvider { | ||
resolvedCredentialsProvider = credentialsProvider | ||
} else { | ||
resolvedCredentialsProvider = try DefaultChainCredentialsProvider(fileBasedConfig: fileBasedConfig) | ||
} | ||
return resolvedCredentialsProvider | ||
} | ||
|
||
public static func region(_ region: String? = nil) async throws -> String? { | ||
let resolvedRegion: String? | ||
let fileBasedConfig = try await CRTFileBasedConfiguration.makeAsync() | ||
if let region = region { | ||
resolvedRegion = region | ||
} else { | ||
let regionResolver = try DefaultRegionResolver { _, _ in fileBasedConfig } | ||
resolvedRegion = await regionResolver.getRegion() | ||
} | ||
|
||
return resolvedRegion | ||
} | ||
|
||
public static func appID(_ appID: String? = nil) throws -> String? { | ||
let fileBasedConfig = try CRTFileBasedConfiguration.make() | ||
let resolvedAppID: String? | ||
if let appID = appID { | ||
resolvedAppID = appID | ||
} else { | ||
resolvedAppID = AppIDConfig.appID( | ||
configValue: nil, | ||
profileName: nil, | ||
fileBasedConfig: fileBasedConfig | ||
) | ||
} | ||
return resolvedAppID | ||
} | ||
|
||
public static func retryMode(_ retryMode: AWSRetryMode? = nil) throws -> AWSRetryMode { | ||
let fileBasedConfig = try CRTFileBasedConfiguration.make() | ||
let resolvedRetryMode: AWSRetryMode? | ||
if let retryMode = retryMode { | ||
resolvedRetryMode = retryMode | ||
} else { | ||
resolvedRetryMode = AWSRetryConfig.retryMode( | ||
configValue: nil, | ||
profileName: nil, | ||
fileBasedConfig: fileBasedConfig | ||
) | ||
} | ||
return resolvedRetryMode! | ||
} | ||
|
||
public static func retryStrategyOptions() throws -> RetryStrategyOptions { | ||
let fileBasedConfig = try CRTFileBasedConfiguration.make() | ||
let resolvedMaxAttempts = AWSRetryConfig.maxAttempts( | ||
configValue: nil, | ||
profileName: nil, | ||
fileBasedConfig: fileBasedConfig | ||
) | ||
|
||
let resolvedRateLimitingMode: RetryStrategyOptions.RateLimitingMode | ||
|
||
switch try self.retryMode(nil) { | ||
case .legacy, .standard: | ||
resolvedRateLimitingMode = .standard | ||
case .adaptive: | ||
resolvedRateLimitingMode = .adaptive | ||
} | ||
|
||
return RetryStrategyOptions(maxRetriesBase: resolvedMaxAttempts - 1, rateLimitingMode: resolvedRateLimitingMode) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Sources/Core/AWSClientRuntime/Config/AWSDefaultClientConfiguration.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
public protocol AWSDefaultClientConfiguration { | ||
/// The credentials provider to be used for AWS credentials. | ||
/// | ||
/// If no credentials provider is supplied, the SDK will look for credentials in the environment, then in the `~/.aws/credentials` file. | ||
var credentialsProvider: CredentialsProviding { get set } | ||
|
||
/// Specifies whether FIPS endpoints should be used. | ||
var useFIPS: Bool? { get set } | ||
|
||
/// Specifies whether dual-stack endpoints should be used. | ||
var useDualStack: Bool? { get set } | ||
|
||
/// An identifying string for the application being used with this SDK. | ||
/// | ||
/// This value is set after resolving app ID from the standard progression of potential sources. | ||
/// | ||
/// The application ID is submitted as part of the `user-agent` and allows for better tracking and troubleshooting. | ||
/// The application ID may also be retrieved from the environment variable `AWS_SDK_UA_APP_ID` or from the | ||
/// configuration file field `sdk_ua_app_id` if it is not set here. | ||
var appID: String? { get set } | ||
|
||
/// The AWS retry mode to be used. | ||
/// | ||
/// This value is set after resolving retry mode from the standard progression of potential sources. | ||
/// | ||
/// May be one of `legacy`, `standard`, or `adaptive`. `standard` and `adaptive` retry strategies are as defined in | ||
/// Smithy Reference Architecture. For the Swift SDK, `legacy` is the same behavior as `standard`. | ||
var awsRetryMode: AWSRetryMode { get set } | ||
} |
19 changes: 19 additions & 0 deletions
19
Sources/Core/AWSClientRuntime/Config/AWSRegionClientConfiguration.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
public protocol AWSRegionClientConfiguration { | ||
|
||
/// The AWS region to use, i.e. `us-east-1` or `us-west-2`, etc. | ||
/// | ||
/// If no region is specified here, one must be specified in the `~/.aws/configuration` file. | ||
var region: String? { get set } | ||
|
||
/// The signing region to be used for signing AWS requests. | ||
/// | ||
/// If none is specified, it is supplied by the SDK. | ||
var signingRegion: String? { get set } | ||
} |
24 changes: 24 additions & 0 deletions
24
Sources/Core/AWSClientRuntime/Plugins/DefaultAWSClientPlugin.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import ClientRuntime | ||
|
||
public class DefaultAWSClientPlugin: Plugin { | ||
private var clientName: String | ||
|
||
public init(clientName: String) { | ||
self.clientName = clientName | ||
} | ||
|
||
public func configureClient(clientConfiguration: ClientConfiguration) throws { | ||
if var config = clientConfiguration as? DefaultClientConfiguration | ||
& AWSDefaultClientConfiguration | ||
& AWSRegionClientConfiguration { | ||
config.retryStrategyOptions = try AWSClientConfigDefaultsProvider.retryStrategyOptions() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import ClientRuntime | ||
|
||
public class RegionPlugin: Plugin { | ||
private var region: String | ||
|
||
public init(_ region: String) { | ||
self.region = region | ||
} | ||
|
||
public func configureClient(clientConfiguration: ClientConfiguration) { | ||
if var config = clientConfiguration as? AWSRegionClientConfiguration { | ||
config.region = self.region | ||
config.signingRegion = self.region | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This narrows down service specific configurations to default configurations (which I think is good).
But is there a use case where we might need service specific configuration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I refactored the
fromConfig
function because 2)AWSClientConfiguration
is going to be deprecated and 2) the function only uses variables specified by the two configuration protocols. (it doesn't use any service specific stuff)