Skip to content

Commit c92f279

Browse files
authored
chore: upgrade smithy to 1.6.1; kotlin 1.4.31; gradle 6.8.3; coroutines 1.4.3 (#78)
1 parent b317896 commit c92f279

File tree

22 files changed

+248
-136
lines changed

22 files changed

+248
-136
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
55
plugins {
6-
kotlin("jvm") version "1.4.21" apply false
6+
kotlin("jvm") version "1.4.31" apply false
77
}
88

99
allprojects {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
package aws.sdk.kotlin.runtime.endpoint
7+
8+
/**
9+
* An [EndpointResolver] that returns a static endpoint
10+
*/
11+
public class StaticEndpointResolver(private val endpoint: Endpoint) : EndpointResolver {
12+
override suspend fun resolve(service: String, region: String): Endpoint {
13+
return endpoint
14+
}
15+
}

client-runtime/protocols/rest-json/build.gradle.kts renamed to client-runtime/protocols/json/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
55

6-
description = "JSON protocol support for AWS service clients"
7-
extra["displayName"] = "Software :: AWS :: Kotlin SDK :: RestJSON"
8-
extra["moduleName"] = "aws.sdk.kotlin.runtime.restjson"
6+
description = "Support for the JSON suite of AWS protocols"
7+
extra["displayName"] = "Software :: AWS :: Kotlin SDK :: JSON"
8+
extra["moduleName"] = "aws.sdk.kotlin.runtime.protocol.json"
99

1010
val smithyKotlinClientRtVersion: String by project
1111

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.protocol.json
6+
7+
import software.aws.clientrt.client.SdkClientOption
8+
import software.aws.clientrt.http.*
9+
import software.aws.clientrt.http.content.ByteArrayContent
10+
import software.aws.clientrt.http.operation.SdkHttpOperation
11+
import software.aws.clientrt.util.InternalAPI
12+
import software.aws.clientrt.util.get
13+
14+
/**
15+
* Http feature that handles AWS JSON protocol behaviors, see:
16+
* - https://awslabs.github.io/smithy/1.0/spec/aws/aws-json-1_0-protocol.html
17+
* - https://awslabs.github.io/smithy/1.0/spec/aws/aws-json-1_1-protocol.html
18+
*
19+
* Including:
20+
* - setting the `Content-Type` and `X-Amz-Target` headers
21+
* - providing an empty json {} body when no body is serialized
22+
*/
23+
@InternalAPI
24+
public class AwsJsonProtocol(config: Config) : Feature {
25+
private val version: String = requireNotNull(config.version) { "AWS JSON Protocol version must be specified" }
26+
27+
public class Config {
28+
/**
29+
* The protocol version e.g. "1.0"
30+
*/
31+
public var version: String? = null
32+
}
33+
34+
public companion object Feature : HttpClientFeatureFactory<Config, AwsJsonProtocol> {
35+
override val key: FeatureKey<AwsJsonProtocol> = FeatureKey("AwsJsonProtocol")
36+
37+
override fun create(block: Config.() -> Unit): AwsJsonProtocol {
38+
val config = Config().apply(block)
39+
return AwsJsonProtocol(config)
40+
}
41+
}
42+
43+
override fun <I, O> install(operation: SdkHttpOperation<I, O>) {
44+
operation.execution.mutate.intercept { req, next ->
45+
val context = req.context
46+
// required context elements
47+
val serviceName = context[SdkClientOption.ServiceName]
48+
val operationName = context[SdkClientOption.OperationName]
49+
50+
// see: https://awslabs.github.io/smithy/1.0/spec/aws/aws-json-1_0-protocol.html#protocol-behaviors
51+
req.builder.headers.append("X-Amz-Target", "$serviceName.$operationName")
52+
req.builder.headers.setMissing("Content-Type", "application/x-amz-json-$version")
53+
54+
if (req.builder.body is HttpBody.Empty) {
55+
// Empty body is required by AWS JSON 1.x protocols
56+
// https://awslabs.github.io/smithy/1.0/spec/aws/aws-json-1_0-protocol.html#empty-body-serialization
57+
// https://awslabs.github.io/smithy/1.0/spec/aws/aws-json-1_1-protocol.html#empty-body-serialization
58+
req.builder.body = ByteArrayContent("{}".encodeToByteArray())
59+
}
60+
61+
next.call(req)
62+
}
63+
}
64+
}

client-runtime/protocols/rest-json/common/src/aws/sdk/kotlin/runtime/restjson/RestJsonError.kt renamed to client-runtime/protocols/json/common/src/aws/sdk/kotlin/runtime/protocol/json/RestJsonError.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
5-
package aws.sdk.kotlin.runtime.restjson
5+
package aws.sdk.kotlin.runtime.protocol.json
66

77
import aws.sdk.kotlin.runtime.AwsServiceException
88
import aws.sdk.kotlin.runtime.ClientException

client-runtime/protocols/rest-json/common/src/aws/sdk/kotlin/runtime/restjson/RestJsonErrorDeserializer.kt renamed to client-runtime/protocols/json/common/src/aws/sdk/kotlin/runtime/protocol/json/RestJsonErrorDeserializer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
5-
package aws.sdk.kotlin.runtime.restjson
5+
package aws.sdk.kotlin.runtime.protocol.json
66

77
import software.aws.clientrt.http.response.HttpResponse
88
import software.aws.clientrt.serde.*
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
package aws.sdk.kotlin.runtime.protocol.json
7+
8+
import aws.sdk.kotlin.runtime.testing.runSuspendTest
9+
import software.aws.clientrt.client.ExecutionContext
10+
import software.aws.clientrt.http.*
11+
import software.aws.clientrt.http.content.ByteArrayContent
12+
import software.aws.clientrt.http.engine.HttpClientEngine
13+
import software.aws.clientrt.http.operation.*
14+
import software.aws.clientrt.http.request.HttpRequestBuilder
15+
import software.aws.clientrt.http.response.HttpResponse
16+
import kotlin.test.Test
17+
import kotlin.test.assertEquals
18+
19+
class AwsJsonProtocolTest {
20+
21+
@Test
22+
fun testSetJsonProtocolHeaders() = runSuspendTest {
23+
val mockEngine = object : HttpClientEngine {
24+
override suspend fun roundTrip(requestBuilder: HttpRequestBuilder): HttpResponse {
25+
return HttpResponse(HttpStatusCode.OK, Headers {}, HttpBody.Empty, requestBuilder.build())
26+
}
27+
}
28+
29+
val op = SdkHttpOperation.build<Unit, HttpResponse> {
30+
serializer = UnitSerializer
31+
deserializer = IdentityDeserializer
32+
context {
33+
service = "FooService"
34+
operationName = "Bar"
35+
}
36+
}
37+
val client = sdkHttpClient(mockEngine)
38+
op.install(AwsJsonProtocol) {
39+
version = "1.1"
40+
}
41+
42+
val response = op.roundTrip(client, Unit)
43+
44+
assertEquals("application/x-amz-json-1.1", response.request.headers["Content-Type"])
45+
assertEquals("FooService.Bar", response.request.headers["X-Amz-Target"])
46+
}
47+
48+
@Test
49+
fun testEmptyBody() = runSuspendTest {
50+
val mockEngine = object : HttpClientEngine {
51+
override suspend fun roundTrip(requestBuilder: HttpRequestBuilder): HttpResponse {
52+
return HttpResponse(HttpStatusCode.OK, Headers {}, HttpBody.Empty, requestBuilder.build())
53+
}
54+
}
55+
56+
val op = SdkHttpOperation.build<Unit, HttpResponse> {
57+
serializer = UnitSerializer
58+
deserializer = IdentityDeserializer
59+
context {
60+
service = "FooService"
61+
operationName = "Bar"
62+
}
63+
}
64+
val client = sdkHttpClient(mockEngine)
65+
op.install(AwsJsonProtocol) {
66+
version = "1.1"
67+
}
68+
69+
val response = op.roundTrip(client, Unit)
70+
val actual = response.request.body.readAll()?.decodeToString()
71+
72+
assertEquals("{}", actual)
73+
}
74+
75+
@Test
76+
fun testDoesNotOverride() = runSuspendTest {
77+
val mockEngine = object : HttpClientEngine {
78+
override suspend fun roundTrip(requestBuilder: HttpRequestBuilder): HttpResponse {
79+
return HttpResponse(HttpStatusCode.OK, Headers {}, HttpBody.Empty, requestBuilder.build())
80+
}
81+
}
82+
83+
val op = SdkHttpOperation.build<Unit, HttpResponse> {
84+
serializer = object : HttpSerialize<Unit> {
85+
override suspend fun serialize(context: ExecutionContext, input: Unit): HttpRequestBuilder {
86+
return HttpRequestBuilder().apply {
87+
headers["Content-Type"] = "application/xml"
88+
body = ByteArrayContent("foo".encodeToByteArray())
89+
}
90+
}
91+
}
92+
deserializer = IdentityDeserializer
93+
context {
94+
service = "FooService"
95+
operationName = "Bar"
96+
}
97+
}
98+
val client = sdkHttpClient(mockEngine)
99+
op.install(AwsJsonProtocol) {
100+
version = "1.1"
101+
}
102+
103+
val response = op.roundTrip(client, Unit)
104+
val actual = response.request.body.readAll()?.decodeToString()
105+
assertEquals("application/xml", response.request.headers["Content-Type"])
106+
assertEquals("foo", actual)
107+
}
108+
}

client-runtime/protocols/rest-json/common/test/aws/sdk/kotlin/runtime/restjson/RestJsonErrorDeserializerTest.kt renamed to client-runtime/protocols/json/common/test/aws/sdk/kotlin/runtime/protocol/json/RestJsonErrorDeserializerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
5-
package aws.sdk.kotlin.runtime.restjson
5+
package aws.sdk.kotlin.runtime.protocol.json
66

77
import aws.sdk.kotlin.runtime.testing.runSuspendTest
88
import software.aws.clientrt.http.Headers

client-runtime/protocols/rest-json/common/test/aws/sdk/kotlin/runtime/restjson/RestJsonErrorTest.kt renamed to client-runtime/protocols/json/common/test/aws/sdk/kotlin/runtime/protocol/json/RestJsonErrorTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
5-
package aws.sdk.kotlin.runtime.restjson
5+
package aws.sdk.kotlin.runtime.protocol.json
66

77
import aws.sdk.kotlin.runtime.AwsServiceException
88
import aws.sdk.kotlin.runtime.UnknownServiceErrorException

client-runtime/protocols/rest-json/common/src/aws/sdk/kotlin/runtime/restjson/AwsJsonTargetHeader.kt

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)