Skip to content

Commit 705662f

Browse files
authored
feat: request compression trait (#1120)
1 parent 61d5ff8 commit 705662f

File tree

8 files changed

+115
-23
lines changed

8 files changed

+115
-23
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "20c241c6-1ade-4087-ab3c-9bf8ad85da82",
3+
"type": "feature",
4+
"description": "Added support for [request compression](https://smithy.io/2.0/spec/behavior-traits.html#requestcompression-trait)"
5+
}

aws-runtime/aws-config/api/aws-config.api

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ public abstract class aws/sdk/kotlin/runtime/config/AbstractAwsSdkClientFactory
221221
public final class aws/sdk/kotlin/runtime/config/AwsSdkSettingKt {
222222
}
223223

224+
public final class aws/sdk/kotlin/runtime/config/compression/RequestCompressionResolversKt {
225+
public static final fun resolveDisableRequestCompression (Laws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/util/LazyAsyncValue;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
226+
public static synthetic fun resolveDisableRequestCompression$default (Laws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/util/LazyAsyncValue;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
227+
public static final fun resolveRequestMinCompressionSizeBytes (Laws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/util/LazyAsyncValue;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
228+
public static synthetic fun resolveRequestMinCompressionSizeBytes$default (Laws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/util/LazyAsyncValue;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
229+
}
230+
224231
public final class aws/sdk/kotlin/runtime/config/endpoints/AccountIdEndpointMode : java/lang/Enum {
225232
public static final field DISABLED Laws/sdk/kotlin/runtime/config/endpoints/AccountIdEndpointMode;
226233
public static final field PREFERRED Laws/sdk/kotlin/runtime/config/endpoints/AccountIdEndpointMode;

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/AbstractAwsSdkClientFactory.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package aws.sdk.kotlin.runtime.config
77

88
import aws.sdk.kotlin.runtime.client.AwsSdkClientConfig
9+
import aws.sdk.kotlin.runtime.config.compression.resolveDisableRequestCompression
10+
import aws.sdk.kotlin.runtime.config.compression.resolveRequestMinCompressionSizeBytes
911
import aws.sdk.kotlin.runtime.config.endpoints.resolveUseDualStack
1012
import aws.sdk.kotlin.runtime.config.endpoints.resolveUseFips
1113
import aws.sdk.kotlin.runtime.config.profile.AwsProfile
@@ -20,6 +22,7 @@ import aws.smithy.kotlin.runtime.client.SdkClient
2022
import aws.smithy.kotlin.runtime.client.SdkClientConfig
2123
import aws.smithy.kotlin.runtime.client.SdkClientFactory
2224
import aws.smithy.kotlin.runtime.client.config.ClientSettings
25+
import aws.smithy.kotlin.runtime.client.config.CompressionClientConfig
2326
import aws.smithy.kotlin.runtime.config.resolve
2427
import aws.smithy.kotlin.runtime.telemetry.TelemetryConfig
2528
import aws.smithy.kotlin.runtime.telemetry.TelemetryProvider
@@ -77,6 +80,16 @@ public abstract class AbstractAwsSdkClientFactory<
7780
config.useDualStack = config.useDualStack ?: resolveUseDualStack(profile = profile)
7881
config.applicationId = config.applicationId ?: resolveUserAgentAppId(platform, profile)
7982

83+
if (config is CompressionClientConfig.Builder) {
84+
config.requestCompression.disableRequestCompression =
85+
config.requestCompression.disableRequestCompression
86+
?: resolveDisableRequestCompression(platform, profile)
87+
88+
config.requestCompression.requestMinCompressionSizeBytes =
89+
config.requestCompression.requestMinCompressionSizeBytes
90+
?: resolveRequestMinCompressionSizeBytes(platform, profile)
91+
}
92+
8093
finalizeConfig(builder, sharedConfig, profile)
8194
}
8295
return builder.build()

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/AwsSdkSetting.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ public object AwsSdkSetting {
190190
* The mode to use when resolving endpoints that make use of the AWS account ID.
191191
*/
192192
public val AwsAccountIdEndpointMode: EnvironmentSetting<AccountIdEndpointMode> = enumEnvSetting("aws.accountIdEndpointMode", "AWS_ACCOUNT_ID_ENDPOINT_MODE")
193+
194+
/**
195+
* Determines if a request should be compressed or not
196+
*/
197+
public val AwsDisableRequestCompression: EnvironmentSetting<Boolean> =
198+
boolEnvSetting("aws.disableRequestCompression", "AWS_DISABLE_REQUEST_COMPRESSION")
199+
200+
/**
201+
* The threshold used to determine if a request should be compressed. Is only checked if request compression is enabled
202+
*/
203+
public val AwsRequestMinCompressionSizeBytes: EnvironmentSetting<Long> =
204+
longEnvSetting("aws.requestMinCompressionSizeBytes", "AWS_REQUEST_MIN_COMPRESSION_SIZE_BYTES")
193205
}
194206

195207
/**
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package aws.sdk.kotlin.runtime.config.compression
6+
7+
import aws.sdk.kotlin.runtime.config.AwsSdkSetting
8+
import aws.sdk.kotlin.runtime.config.profile.AwsProfile
9+
import aws.sdk.kotlin.runtime.config.profile.disableRequestCompression
10+
import aws.sdk.kotlin.runtime.config.profile.requestMinCompressionSizeBytes
11+
import aws.smithy.kotlin.runtime.InternalApi
12+
import aws.smithy.kotlin.runtime.config.resolve
13+
import aws.smithy.kotlin.runtime.util.LazyAsyncValue
14+
import aws.smithy.kotlin.runtime.util.PlatformProvider
15+
16+
/**
17+
* Attempts to resolve disableRequestCompression from the specified sources.
18+
* @return disableRequestCompression setting if found, the default value if not.
19+
*/
20+
@InternalApi
21+
public suspend fun resolveDisableRequestCompression(
22+
platform: PlatformProvider = PlatformProvider.System,
23+
profile: LazyAsyncValue<AwsProfile>,
24+
): Boolean =
25+
AwsSdkSetting.AwsDisableRequestCompression.resolve(platform)
26+
?: profile.get().disableRequestCompression
27+
?: false
28+
29+
/**
30+
* Attempts to resolve requestMinCompressionSizeBytes from the specified sources.
31+
* @return requestMinCompressionSizeBytes setting if found, the default value if not.
32+
*/
33+
@InternalApi
34+
public suspend fun resolveRequestMinCompressionSizeBytes(
35+
platform: PlatformProvider = PlatformProvider.System,
36+
profile: LazyAsyncValue<AwsProfile>,
37+
): Long =
38+
AwsSdkSetting.AwsRequestMinCompressionSizeBytes.resolve(platform)
39+
?: profile.get().requestMinCompressionSizeBytes
40+
?: 10240

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/profile/AwsProfile.kt

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ public val AwsProfile.sourceProfile: String?
6767
*/
6868
@InternalSdkApi
6969
public val AwsProfile.maxAttempts: Int?
70-
get() = getOrNull("max_attempts")?.run {
71-
toIntOrNull() ?: throw ConfigurationException("Failed to parse max_attempts $this as an integer")
72-
}
70+
get() = getIntOrNull("max_attempts")
7371

7472
/**
7573
* The command which the SDK will invoke to retrieve credentials
@@ -148,6 +146,20 @@ public val AwsProfile.accountIdEndpointMode: AccountIdEndpointMode?
148146
)
149147
}
150148

149+
/**
150+
* Determines when a request should be compressed or not
151+
*/
152+
@InternalSdkApi
153+
public val AwsProfile.disableRequestCompression: Boolean?
154+
get() = getBooleanOrNull("disable_request_compression")
155+
156+
/**
157+
* The threshold used to determine when a request should be compressed
158+
*/
159+
@InternalSdkApi
160+
public val AwsProfile.requestMinCompressionSizeBytes: Long?
161+
get() = getLongOrNull("request_min_compression_size_bytes")
162+
151163
/**
152164
* Parse a config value as a boolean, ignoring case.
153165
*/
@@ -159,6 +171,28 @@ public fun AwsProfile.getBooleanOrNull(key: String, subKey: String? = null): Boo
159171
)
160172
}
161173

174+
/**
175+
* Parse a config value as an int.
176+
*/
177+
@InternalSdkApi
178+
public fun AwsProfile.getIntOrNull(key: String, subKey: String? = null): Int? =
179+
getOrNull(key, subKey)?.let {
180+
it.toIntOrNull() ?: throw ConfigurationException(
181+
"Failed to parse config property ${buildKeyString(key, subKey)} as an integer",
182+
)
183+
}
184+
185+
/**
186+
* Parse a config value as a long.
187+
*/
188+
@InternalSdkApi
189+
public fun AwsProfile.getLongOrNull(key: String, subKey: String? = null): Long? =
190+
getOrNull(key, subKey)?.let {
191+
it.toLongOrNull() ?: throw ConfigurationException(
192+
"Failed to parse config property ${buildKeyString(key, subKey)} as a long",
193+
)
194+
}
195+
162196
internal fun AwsProfile.getUrlOrNull(key: String, subKey: String? = null): Url? =
163197
getOrNull(key, subKey)?.let {
164198
try {

codegen/smithy-aws-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/aws/protocols/core/AwsHttpBindingProtocolGenerator.kt

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,6 @@ abstract class AwsHttpBindingProtocolGenerator : HttpBindingProtocolGenerator()
3636
}
3737

3838
override fun generateProtocolUnitTests(ctx: ProtocolGenerator.GenerationContext) {
39-
val ignoredTests = TestMemberDelta(
40-
setOf(
41-
// FIXME - compression not yet supported, see https://github.com/awslabs/smithy-kotlin/issues/955
42-
"SDKAppliedContentEncoding_awsJson1_0",
43-
"SDKAppliedContentEncoding_awsJson1_1",
44-
"SDKAppliedContentEncoding_awsQuery",
45-
"SDKAppliedContentEncoding_ec2Query",
46-
"SDKAppliedContentEncoding_restJson1",
47-
"SDKAppliedContentEncoding_restXml",
48-
"SDKAppendedGzipAfterProvidedEncoding_restJson1",
49-
"SDKAppendedGzipAfterProvidedEncoding_restXml",
50-
"SDKAppendsGzipAndIgnoresHttpProvidedEncoding_awsJson1_0",
51-
"SDKAppendsGzipAndIgnoresHttpProvidedEncoding_awsJson1_1",
52-
"SDKAppendsGzipAndIgnoresHttpProvidedEncoding_awsQuery",
53-
"SDKAppendsGzipAndIgnoresHttpProvidedEncoding_ec2Query",
54-
),
55-
)
56-
5739
// The following can be used to generate only a specific test by name.
5840
// val targetedTest = TestMemberDelta(setOf("RestJsonComplexErrorWithNoMessage"), TestContainmentMode.RUN_TESTS)
5941

@@ -66,7 +48,6 @@ abstract class AwsHttpBindingProtocolGenerator : HttpBindingProtocolGenerator()
6648
requestTestBuilder,
6749
responseTestBuilder,
6850
errorTestBuilder,
69-
ignoredTests,
7051
).generateProtocolTests()
7152
}
7253

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ coroutines-version = "1.7.3"
77
atomicfu-version = "0.23.1"
88

99
# smithy-kotlin codegen and runtime are versioned separately
10-
smithy-kotlin-runtime-version = "1.0.5"
10+
smithy-kotlin-runtime-version = "1.0.6"
1111
smithy-kotlin-codegen-version = "0.30.5"
1212

1313
# codegen

0 commit comments

Comments
 (0)