use of software.amazon.awssdk.http.async.AsyncExecuteRequest in project curiostack by curioswitch.
the class ArmeriaSdkHttpClient method execute.
@Override
public CompletableFuture<Void> execute(AsyncExecuteRequest executeRequest) {
SdkHttpRequest httpRequest = executeRequest.request();
SdkAsyncHttpResponseHandler handler = executeRequest.responseHandler();
RequestHeadersBuilder headersBuilder = RequestHeaders.builder(convert(httpRequest.method()), httpRequest.getUri().toString());
executeRequest.requestContentPublisher().contentLength().ifPresent(contentLength -> headersBuilder.add(HttpHeaderNames.CONTENT_LENGTH, contentLength.toString()));
for (Map.Entry<String, List<String>> header : httpRequest.headers().entrySet()) {
headersBuilder.add(header.getKey(), header.getValue());
}
Publisher<HttpData> requestStream = delegate -> executeRequest.requestContentPublisher().subscribe(new SdkToHttpDataSubscriber(delegate));
HttpRequest request = HttpRequest.of(headersBuilder.build(), requestStream);
HttpResponse response = client.execute(request);
response.subscribe(new ResponseSubscriber(handler));
CompletableFuture<Void> completionFuture = response.whenComplete();
completionFuture.whenComplete((unused, t) -> {
if (t != null) {
// Subscriber.onError, SdkAsyncHttpResponseHandler.onError, the returned future, and any
// thrown exception are all ways of communicating errors to the SDK. This seems like two
// too many but cover all the bases just in case.
handler.onError(t);
}
});
return completionFuture;
}
use of software.amazon.awssdk.http.async.AsyncExecuteRequest in project curiostack by curioswitch.
the class ArmeriaSdkHttpClientTest method converts_success.
@Test
void converts_success() {
StreamMessage<ByteBuffer> requestStream = StreamMessage.of(ByteBuffer.wrap("meow".getBytes(StandardCharsets.UTF_8)), ByteBuffer.wrap("woof".getBytes(StandardCharsets.UTF_8)));
AtomicReference<SdkHttpResponse> responseHeaders = new AtomicReference<>();
AtomicReference<Throwable> responseError = new AtomicReference<>();
AtomicReference<Throwable> responseSubscriberError = new AtomicReference<>();
AtomicBoolean responseComplete = new AtomicBoolean();
List<ByteBuffer> responsePayloads = new ArrayList<>();
AsyncExecuteRequest request = AsyncExecuteRequest.builder().request(SdkHttpRequest.builder().protocol("https").method(SdkHttpMethod.POST).host("github.com").appendHeader("X-Animals", "cat").appendHeader("X-Animals", "dog").appendHeader("Content-Type", "application/json").encodedPath("/foo").appendRawQueryParameter("bar", "baz").build()).responseHandler(new SdkAsyncHttpResponseHandler() {
@Override
public void onHeaders(SdkHttpResponse headers) {
responseHeaders.set(headers);
}
@Override
public void onStream(Publisher<ByteBuffer> stream) {
stream.subscribe(new Subscriber<ByteBuffer>() {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(ByteBuffer byteBuffer) {
responsePayloads.add(byteBuffer);
}
@Override
public void onError(Throwable t) {
responseSubscriberError.set(t);
}
@Override
public void onComplete() {
responseComplete.set(true);
}
});
}
@Override
public void onError(Throwable error) {
responseError.set(error);
}
}).requestContentPublisher(new SdkHttpContentPublisher() {
@Override
public Optional<Long> contentLength() {
return Optional.of((long) ("body1" + "body2").length());
}
@Override
public void subscribe(Subscriber<? super ByteBuffer> s) {
requestStream.subscribe(s);
}
}).fullDuplex(false).build();
AtomicReference<AggregatedHttpRequest> aggregatedRequest = new AtomicReference<>();
when(webClient.execute(any(HttpRequest.class))).thenAnswer((Answer<HttpResponse>) invocation -> {
HttpRequest httpRequest = invocation.getArgument(0);
aggregatedRequest.set(httpRequest.aggregate().join());
return HttpResponse.of(ResponseHeaders.of(HttpStatus.OK, HttpHeaderNames.CONTENT_TYPE, "application/json", HttpHeaderNames.of("X-Countries"), "Japan", HttpHeaderNames.of("X-Countries"), "US"), HttpData.ofUtf8("purr"), HttpData.ofUtf8("growl"));
});
CompletableFuture<Void> responseFuture = client.execute(request);
assertThat(responseFuture.join()).isNull();
assertThat(responseHeaders.get()).satisfies(headers -> {
assertThat(headers.statusCode()).isEqualTo(HttpStatus.OK.code());
assertThat(headers.statusText()).hasValue(HttpStatus.OK.reasonPhrase());
assertThat(headers.headers()).containsEntry("content-type", ImmutableList.of("application/json")).containsEntry("x-countries", ImmutableList.of("Japan", "US"));
});
assertThat(responsePayloads).extracting(buf -> Unpooled.wrappedBuffer(buf).toString(StandardCharsets.UTF_8)).containsExactly("purr", "growl");
assertThat(responseError.get()).isNull();
assertThat(responseSubscriberError.get()).isNull();
}
use of software.amazon.awssdk.http.async.AsyncExecuteRequest in project aws-xray-sdk-java by aws.
the class TracingInterceptorTest method mockSdkAsyncHttpClient.
private SdkAsyncHttpClient mockSdkAsyncHttpClient(SdkHttpResponse response) {
SdkAsyncHttpClient mockClient = Mockito.mock(SdkAsyncHttpClient.class);
when(mockClient.execute(Mockito.any(AsyncExecuteRequest.class))).thenAnswer((Answer<CompletableFuture<Void>>) invocationOnMock -> {
AsyncExecuteRequest request = invocationOnMock.getArgument(0);
SdkAsyncHttpResponseHandler handler = request.responseHandler();
handler.onHeaders(response);
handler.onStream(new EmptyPublisher<>());
return CompletableFuture.completedFuture(null);
});
return mockClient;
}
Aggregations