Skip to content

Commit ff088ff

Browse files
authored
chore: Refactor non-AWS context config into runtime (#941)
1 parent c9427c8 commit ff088ff

File tree

8 files changed

+61
-37
lines changed

8 files changed

+61
-37
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import class Smithy.ContextBuilder
9+
10+
extension ContextBuilder {
11+
12+
public func withSmithyDefaultConfig<Config: DefaultClientConfiguration & DefaultHttpClientConfiguration>(
13+
_ config: Config
14+
) -> Smithy.ContextBuilder {
15+
return self
16+
.withIdempotencyTokenGenerator(value: config.idempotencyTokenGenerator)
17+
.withLogger(value: config.logger)
18+
.withPartitionID(value: config.partitionID)
19+
.withAuthSchemes(value: config.authSchemes ?? [])
20+
.withAuthSchemePreference(value: config.authSchemePreference)
21+
.withAuthSchemeResolver(value: config.authSchemeResolver)
22+
.withSocketTimeout(value: config.httpClientConfiguration.socketTimeout)
23+
.withIdentityResolver(value: config.bearerTokenIdentityResolver, schemeID: "smithy.api#httpBearerAuth")
24+
.withIdentityResolver(value: config.awsCredentialIdentityResolver, schemeID: "aws.auth#sigv4")
25+
}
26+
}

Sources/ClientRuntime/Config/DefaultClientConfiguration.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
import protocol Smithy.LogAgent
89
import struct SmithyRetriesAPI.RetryStrategyOptions
910

1011
public protocol DefaultClientConfiguration: ClientConfiguration {
12+
1113
/// The configuration for retry of failed network requests.
1214
///
1315
/// Default options are used if none are set.
1416
var retryStrategyOptions: RetryStrategyOptions { get set }
1517

18+
/// The LogAgent to be used when logging messages related to API calls.
19+
var logger: LogAgent { get }
20+
1621
/// The log mode to use for request / response messages.
1722
///
1823
/// If none is provided, `.none` will be used.

Sources/ClientRuntime/Config/DefaultHttpClientConfiguration.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import protocol SmithyHTTPAPI.HTTPClient
99
import protocol SmithyHTTPAuthAPI.AuthScheme
1010
import protocol SmithyHTTPAuthAPI.AuthSchemeResolver
11+
import protocol SmithyIdentity.AWSCredentialIdentityResolver
1112
import protocol SmithyIdentity.BearerTokenIdentityResolver
1213

1314
public protocol DefaultHttpClientConfiguration: ClientConfiguration {
@@ -21,11 +22,19 @@ public protocol DefaultHttpClientConfiguration: ClientConfiguration {
2122
/// Configuration for the HTTP client.
2223
var httpClientConfiguration: HttpClientConfiguration { get set }
2324

25+
var partitionID: String? { get }
26+
2427
/// List of auth schemes to use for requests.
2528
///
2629
/// Defaults to auth schemes defined on the underlying Smithy model of a service.
2730
var authSchemes: [AuthScheme]? { get set }
2831

32+
/// An ordered, prioritized list of auth scheme IDs that should be used for this client's requests.
33+
///
34+
/// If no auth scheme preference is given, the first supported auth scheme defined in `authSchemes`
35+
/// will be used. If a value was not provided for `authSchemes`, then the service's first defined, supported auth scheme will be used.
36+
var authSchemePreference: [String]? { get set }
37+
2938
/// The auth scheme resolver to use for resolving the auth scheme.
3039
///
3140
/// Defaults to an auth scheme resolver generated based on the underlying Smithy model of a service.
@@ -36,6 +45,9 @@ public protocol DefaultHttpClientConfiguration: ClientConfiguration {
3645
/// Default resolver will look for the token in the `~/.aws/sso/cache` directory.
3746
var bearerTokenIdentityResolver: any BearerTokenIdentityResolver { get set }
3847

48+
/// The AWS credential identity resolver to be used for AWS credentials.
49+
var awsCredentialIdentityResolver: any AWSCredentialIdentityResolver { get set }
50+
3951
/// Adds a `HttpInterceptorProvider` that will be used to provide interceptors for all HTTP operations.
4052
///
4153
/// - Parameter provider: The `HttpInterceptorProvider` to add.

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/ExtractTelemetryLoggerConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ExtractTelemetryLoggerConfig : SwiftIntegration {
2424
writer: SwiftWriter,
2525
section: ConfigClassVariablesCustomization,
2626
) {
27-
writer.write("internal let logger: \$N", SmithyTypes.LogAgent)
27+
writer.write("public let logger: \$N", SmithyTypes.LogAgent)
2828
writer.write("")
2929
}
3030
}

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/HttpProtocolClientGenerator.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,17 @@ open class HttpProtocolClientGenerator(
4646
val operationsIndex = OperationIndex.of(model)
4747

4848
writer.openBlock("extension \$L {", "}", serviceSymbol.name) {
49-
operations.forEach {
50-
val serviceName = ctx.settings.sdkId.toUpperCamelCase()
51-
ServiceGenerator.renderOperationDefinition(serviceName, model, serviceShape, symbolProvider, writer, operationsIndex, it)
49+
val serviceName = ctx.settings.sdkId.toUpperCamelCase()
50+
operations.forEach { operation ->
51+
ServiceGenerator.renderOperationDefinition(
52+
serviceName,
53+
model,
54+
serviceShape,
55+
symbolProvider,
56+
writer,
57+
operationsIndex,
58+
operation,
59+
)
5260
writer.openBlock(" {", "}") {
5361
val operationStackName = "operation"
5462
val generator =
@@ -60,11 +68,12 @@ open class HttpProtocolClientGenerator(
6068
operationMiddleware,
6169
operationStackName,
6270
)
63-
generator.render(serviceShape, it)
71+
generator.render(serviceShape, operation)
6472
writer.write("return try await op.execute(input: input)")
6573
}
6674
writer.write("")
6775
}
76+
writer.unwrite("\n")
6877
}
6978
}
7079
}

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/middleware/MiddlewareExecutionGenerator.kt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,8 @@ class MiddlewareExecutionGenerator(
9494
writer.write(" .withMethod(value: .\$L)", httpMethod)
9595
writer.write(" .withServiceName(value: serviceName)")
9696
writer.write(" .withOperation(value: \$S)", op.toLowerCamelCase())
97-
writer.write(" .withIdempotencyTokenGenerator(value: config.idempotencyTokenGenerator)")
98-
writer.write(" .withLogger(value: config.logger)")
99-
writer.write(" .withPartitionID(value: config.partitionID)")
100-
writer.write(" .withAuthSchemes(value: config.authSchemes ?? [])")
101-
writer.write(" .withAuthSchemePreference(value: config.authSchemePreference)")
102-
writer.write(" .withAuthSchemeResolver(value: config.authSchemeResolver)")
10397
writer.write(" .withUnsignedPayloadTrait(value: \$L)", op.hasTrait<UnsignedPayloadTrait>())
104-
writer.write(" .withSocketTimeout(value: config.httpClientConfiguration.socketTimeout)")
105-
writer.write(" .withIdentityResolver(value: config.bearerTokenIdentityResolver, schemeID: \$S)", "smithy.api#httpBearerAuth")
98+
writer.write(" .withSmithyDefaultConfig(config)")
10699

107100
// Add flag for presign / presign-url flows
108101
if (flowType == ContextAttributeCodegenFlowType.PRESIGN_REQUEST) {

smithy-swift-codegen/src/test/kotlin/software/amazon/smithy/swift/codegen/HttpProtocolClientGeneratorTests.kt

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,8 @@ extension RestJsonProtocolClient {
167167
.withMethod(value: .post)
168168
.withServiceName(value: serviceName)
169169
.withOperation(value: "getStatus")
170-
.withIdempotencyTokenGenerator(value: config.idempotencyTokenGenerator)
171-
.withLogger(value: config.logger)
172-
.withPartitionID(value: config.partitionID)
173-
.withAuthSchemes(value: config.authSchemes ?? [])
174-
.withAuthSchemePreference(value: config.authSchemePreference)
175-
.withAuthSchemeResolver(value: config.authSchemeResolver)
176170
.withUnsignedPayloadTrait(value: false)
177-
.withSocketTimeout(value: config.httpClientConfiguration.socketTimeout)
178-
.withIdentityResolver(value: config.bearerTokenIdentityResolver, schemeID: "smithy.api#httpBearerAuth")
171+
.withSmithyDefaultConfig(config)
179172
.build()
180173
"""
181174
contents.shouldContainOnlyOnce(expectedFragment)
@@ -200,15 +193,8 @@ extension RestJsonProtocolClient {
200193
.withMethod(value: .post)
201194
.withServiceName(value: serviceName)
202195
.withOperation(value: "allocateWidget")
203-
.withIdempotencyTokenGenerator(value: config.idempotencyTokenGenerator)
204-
.withLogger(value: config.logger)
205-
.withPartitionID(value: config.partitionID)
206-
.withAuthSchemes(value: config.authSchemes ?? [])
207-
.withAuthSchemePreference(value: config.authSchemePreference)
208-
.withAuthSchemeResolver(value: config.authSchemeResolver)
209196
.withUnsignedPayloadTrait(value: false)
210-
.withSocketTimeout(value: config.httpClientConfiguration.socketTimeout)
211-
.withIdentityResolver(value: config.bearerTokenIdentityResolver, schemeID: "smithy.api#httpBearerAuth")
197+
.withSmithyDefaultConfig(config)
212198
.build()
213199
let builder = ClientRuntime.OrchestratorBuilder<AllocateWidgetInput, AllocateWidgetOutput, SmithyHTTPAPI.HTTPRequest, SmithyHTTPAPI.HTTPResponse>()
214200
config.interceptorProviders.forEach { provider in

smithy-swift-codegen/src/test/kotlin/software/amazon/smithy/swift/codegen/requestandresponse/EventStreamTests.kt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,8 @@ extension EventStreamTestClientTypes.TestStream {
216216
.withMethod(value: .post)
217217
.withServiceName(value: serviceName)
218218
.withOperation(value: "testStreamOp")
219-
.withIdempotencyTokenGenerator(value: config.idempotencyTokenGenerator)
220-
.withLogger(value: config.logger)
221-
.withPartitionID(value: config.partitionID)
222-
.withAuthSchemes(value: config.authSchemes ?? [])
223-
.withAuthSchemePreference(value: config.authSchemePreference)
224-
.withAuthSchemeResolver(value: config.authSchemeResolver)
225219
.withUnsignedPayloadTrait(value: false)
226-
.withSocketTimeout(value: config.httpClientConfiguration.socketTimeout)
227-
.withIdentityResolver(value: config.bearerTokenIdentityResolver, schemeID: "smithy.api#httpBearerAuth")
220+
.withSmithyDefaultConfig(config)
228221
.build()
229222
let builder = ClientRuntime.OrchestratorBuilder<TestStreamOpInput, TestStreamOpOutput, SmithyHTTPAPI.HTTPRequest, SmithyHTTPAPI.HTTPResponse>()
230223
config.interceptorProviders.forEach { provider in

0 commit comments

Comments
 (0)