use of io.helidon.webclient.WebClientResponse in project helidon by oracle.
the class RestApiBase method errorResponse.
/**
* Create an error response.
* This method attempts to read the response entity as a string, parse it into a JsonObject and
* depending on result, calls methods to create a proper exception.
*
* @param path requested path
* @param request original request
* @param method HTTP method
* @param requestId ID of the request
* @param response actual response where we do not expect an entity
* @param <T> type of the response
*
* @return future with error
*/
protected <T extends ApiResponse> Single<T> errorResponse(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse response) {
if (response.headers().contentLength().orElse(-1L) == 0) {
// explicitly no content
return Single.error(readError(path, request, method, requestId, response));
} else {
AtomicBoolean processedError = new AtomicBoolean();
return response.content().as(String.class).flatMapSingle(string -> {
try {
JsonObject json = jsonReaderFactory.createReader(new StringReader(string)).readObject();
Single<T> error = Single.error(readError(path, request, method, requestId, response, json));
processedError.set(true);
return error;
} catch (Exception e) {
Single<T> error = Single.error(readError(path, request, method, requestId, response, string));
processedError.set(true);
return error;
}
}).onErrorResumeWithSingle(it -> {
if (processedError.get()) {
return Single.error(it);
}
return Single.error(readErrorFailedEntity(path, request, method, requestId, response, it));
});
}
}
use of io.helidon.webclient.WebClientResponse in project helidon by oracle.
the class MicrometerSimplePrometheusTest method checkNoMatch.
@Test
public void checkNoMatch() throws ExecutionException, InterruptedException {
WebClientResponse response = webClient.get().accept(MediaType.builder().type(MediaType.TEXT_PLAIN.type()).subtype("special").build()).path("/micrometer").request().get();
assertThat("Expected failed HTTP status", response.status(), is(Http.Status.NOT_ACCEPTABLE_406));
}
use of io.helidon.webclient.WebClientResponse in project helidon by oracle.
the class MicrometerSimplePrometheusTest method checkViaMediaType.
@Test
public void checkViaMediaType() throws ExecutionException, InterruptedException {
timer1.record(2L, TimeUnit.SECONDS);
counter1.increment(3);
gauge1.set(4);
WebClientResponse response = webClient.get().accept(MediaType.TEXT_PLAIN).path("/micrometer").request().get();
assertThat("Unexpected HTTP status", response.status(), is(Http.Status.OK_200));
String promOutput = response.content().as(String.class).get();
}
use of io.helidon.webclient.WebClientResponse in project helidon by oracle.
the class MicrometerSimplePrometheusTest method checkViaQueryParam.
@Test
public void checkViaQueryParam() throws ExecutionException, InterruptedException {
timer1.record(2L, TimeUnit.SECONDS);
counter1.increment(3);
gauge1.set(4);
WebClientResponse response = webClient.get().accept(MediaType.builder().type(MediaType.TEXT_PLAIN.type()).subtype("special").build()).path("/micrometer").queryParam("type", "prometheus").request().get();
assertThat("Unexpected HTTP status", response.status(), is(Http.Status.OK_200));
String promOutput = response.content().as(String.class).get();
}
use of io.helidon.webclient.WebClientResponse in project helidon by oracle.
the class OciRestApi method requestJsonPayload.
@Override
protected Supplier<Single<WebClientResponse>> requestJsonPayload(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientRequestBuilder requestBuilder, JsonObject jsonObject) {
requestBuilder.accept(request.responseMediaType().orElse(MediaType.APPLICATION_JSON));
requestBuilder.contentType(request.requestMediaType().orElse(MediaType.APPLICATION_JSON));
// common stuff
updateRequestBuilder(requestBuilder, request, requestId);
// signature requirement
// this requires a content length and hash
ByteArrayOutputStream baos = new ByteArrayOutputStream();
jsonWriterFactory().createWriter(baos).write(jsonObject);
byte[] requestBytes = baos.toByteArray();
String sha256 = HASH_DIGEST.digest(Base64Value.create(requestBytes)).toBase64();
requestBuilder.headers(headers -> {
headers.contentLength(requestBytes.length);
headers.add("x-content-sha256", sha256);
return headers;
}).contentType(request.requestMediaType().orElse(MediaType.APPLICATION_JSON));
return () -> requestBuilder.submit(requestBytes);
}
Aggregations