Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0e06cf
commit ca258ae
Showing
8 changed files
with
159 additions
and
6 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.http4k.aws | ||
|
||
import org.http4k.core.Body | ||
import org.http4k.core.Body.Companion.EMPTY | ||
import org.http4k.core.HttpHandler | ||
import org.http4k.core.Method | ||
import org.http4k.core.Request | ||
import org.http4k.core.Response | ||
import org.http4k.core.Uri | ||
import software.amazon.awssdk.http.AbortableInputStream.create | ||
import software.amazon.awssdk.http.ExecutableHttpRequest | ||
import software.amazon.awssdk.http.HttpExecuteRequest | ||
import software.amazon.awssdk.http.HttpExecuteResponse | ||
import software.amazon.awssdk.http.SdkHttpClient | ||
import software.amazon.awssdk.http.SdkHttpFullRequest | ||
import software.amazon.awssdk.http.SdkHttpFullResponse.builder | ||
|
||
class AwsSdkClient(private val http: HttpHandler) : SdkHttpClient { | ||
override fun close() { | ||
} | ||
|
||
override fun prepareRequest(request: HttpExecuteRequest) = object : ExecutableHttpRequest { | ||
override fun call() = http(request.fromAws()).asAws() | ||
override fun abort() {} | ||
} | ||
} | ||
|
||
private fun HttpExecuteRequest.fromAws() = with(httpRequest()) { | ||
val init = Request(Method.valueOf(method().name), Uri.of(uri.toString())) | ||
.headers(headers().entries.flatMap { (name, values) -> values.map { name to it } }) | ||
|
||
when (this) { | ||
is SdkHttpFullRequest -> | ||
init.body(contentStreamProvider().map { Body(it.newStream()) }.orElse(EMPTY)) | ||
else -> init | ||
} | ||
} | ||
|
||
private fun Response.asAws() = HttpExecuteResponse.builder() | ||
.response(builder() | ||
.statusCode(status.code) | ||
.statusText(status.description) | ||
.headers(headers.groupBy { it.first }.mapValues { it.value.map { it.second } }) | ||
.content(create(body.stream)) | ||
.build()) | ||
.build() |
71 changes: 71 additions & 0 deletions
71
http4k-aws/src/test/kotlin/org/http4k/aws/AwsSdkClientTest.kt
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,71 @@ | ||
package org.http4k.aws | ||
|
||
import com.natpryce.hamkrest.MatchResult | ||
import com.natpryce.hamkrest.Matcher | ||
import com.natpryce.hamkrest.and | ||
import com.natpryce.hamkrest.assertion.assertThat | ||
import com.natpryce.hamkrest.equalTo | ||
import com.natpryce.hamkrest.has | ||
import org.http4k.core.Method.POST | ||
import org.http4k.core.Request | ||
import org.http4k.core.Response | ||
import org.http4k.core.Status.Companion.I_M_A_TEAPOT | ||
import org.junit.jupiter.api.Test | ||
import software.amazon.awssdk.http.AbortableInputStream | ||
import software.amazon.awssdk.http.HttpExecuteRequest.builder | ||
import software.amazon.awssdk.http.SdkHttpFullRequest | ||
import software.amazon.awssdk.http.SdkHttpFullResponse | ||
import software.amazon.awssdk.http.SdkHttpMethod | ||
import software.amazon.awssdk.http.SdkHttpResponse | ||
import java.net.URL | ||
import java.util.Optional | ||
|
||
class AwsSdkClientTest { | ||
|
||
@Test | ||
fun `converts formats correctly`() { | ||
val headers = listOf("bar" to null, "foo" to "bar1") | ||
val request = Request(POST, "https://foobar/123") | ||
.query("foo", "bar1") | ||
.query("foo", "bar2") | ||
.headers(headers) | ||
.body("hello") | ||
|
||
val response = Response(I_M_A_TEAPOT).headers(headers).body("world") | ||
|
||
val client = AwsSdkClient { | ||
assertThat(it.toString(), equalTo(request.toString())) | ||
response | ||
} | ||
|
||
val out = client.prepareRequest( | ||
builder().request( | ||
SdkHttpFullRequest.builder() | ||
.method(SdkHttpMethod.POST) | ||
.headers(headers.groupBy { it.first } | ||
.mapValues { it.value.map { it.second } }) | ||
.uri(URL(request.uri.toString()).toURI()) | ||
.putRawQueryParameter("foo", listOf("bar1", "bar2")) | ||
.contentStreamProvider { request.body.stream }.build() | ||
).build() | ||
).call() | ||
|
||
assertThat(out.httpResponse() as SdkHttpFullResponse, | ||
has(SdkHttpResponse::statusCode, equalTo(I_M_A_TEAPOT.code)) | ||
.and( | ||
has(SdkHttpFullResponse::headers, equalTo(mapOf( | ||
"bar" to listOf(null), | ||
"foo" to listOf("bar1") | ||
))) | ||
) | ||
.and(has(SdkHttpFullResponse::content, object : Matcher<Optional<AbortableInputStream>> { | ||
override val description = "same content" | ||
|
||
override fun invoke(actual: Optional<AbortableInputStream>): MatchResult { | ||
val content = actual.get().reader().readText() | ||
return if (content == response.bodyString()) MatchResult.Match else MatchResult.Mismatch(content) | ||
} | ||
})) | ||
) | ||
} | ||
} |
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,18 @@ | ||
package guide.modules.aws | ||
|
||
import org.http4k.aws.AwsSdkClient | ||
import org.http4k.client.OkHttp | ||
import org.http4k.core.then | ||
import org.http4k.filter.DebuggingFilters | ||
import software.amazon.awssdk.services.s3.S3Client | ||
import software.amazon.awssdk.services.s3.model.CreateBucketRequest | ||
|
||
fun main() { | ||
val fakeS3 = DebuggingFilters.PrintRequestAndResponse().then(OkHttp()) | ||
|
||
val s3 = S3Client.builder() | ||
.httpClient(AwsSdkClient(fakeS3)) | ||
.build() | ||
|
||
s3.createBucket(CreateBucketRequest.builder().bucket("hello").build()) | ||
} |