use of com.canoo.platform.core.http.HttpResponse in project dolphin-platform by canoo.
the class HttpClientTests method testSimpleGetWithJsonContentType.
@Test
public void testSimpleGetWithJsonContentType() throws Exception {
// given:
final HttpClient client = PlatformClient.getService(HttpClient.class);
final AtomicBoolean actionCalled = new AtomicBoolean(false);
final AtomicBoolean doneCalled = new AtomicBoolean(false);
final AtomicBoolean errorCalled = new AtomicBoolean(false);
// when:
final CompletableFuture<HttpResponse<String>> future = client.get("http://localhost:" + freePort).withoutContent().readString(HttpHeaderConstants.JSON_MIME_TYPE).onDone(response -> {
actionCalled.set(true);
doneCalled.set(true);
}).onError(e -> {
actionCalled.set(true);
errorCalled.set(true);
}).execute();
// then:
final HttpResponse<String> response = future.get(1_000, TimeUnit.MILLISECONDS);
assertThat("response not defined", response, notNullValue());
assertThat("Wrong response code", response.getStatusCode(), is(200));
assertThat("Content should not be null", response.getRawContent(), notNullValue());
final String json = response.getContent();
final Gson gson = new Gson();
final DummyJson dummy = gson.fromJson(json, DummyJson.class);
assertThat("No JSON object created", dummy, notNullValue());
assertThat("Wrong name", dummy.getName(), is("Joe"));
assertThat("Wrong age", dummy.getAge(), is(33));
assertThat("Wrong isJavaChampion", dummy.isJavaChampion(), is(true));
assertThatDoneCalledAndErrorNotCalled(actionCalled, doneCalled, errorCalled);
}
use of com.canoo.platform.core.http.HttpResponse in project dolphin-platform by canoo.
the class HttpCallExecutorImpl method execute.
@Override
public CompletableFuture<HttpResponse<R>> execute() {
final CompletableFuture<HttpResponse<R>> completableFuture = new CompletableFuture<>();
executor.submit(() -> {
try {
final HttpResponse<R> result = provider.get();
final int statusCode = result.getStatusCode();
if (statusCode >= 300) {
final HttpException e = new BadResponseException(result, "Bad Response: " + statusCode);
if (errorHandler != null) {
uiExecutor.execute(() -> errorHandler.accept(e));
}
completableFuture.completeExceptionally(e);
} else {
if (onDone != null) {
uiExecutor.execute(() -> onDone.accept(result));
}
completableFuture.complete(result);
}
} catch (final HttpException e) {
if (errorHandler != null) {
uiExecutor.execute(() -> errorHandler.accept(e));
}
completableFuture.completeExceptionally(e);
}
});
return completableFuture;
}
use of com.canoo.platform.core.http.HttpResponse in project dolphin-platform by canoo.
the class HttpCallResponseBuilderImpl method readObject.
@Override
public <R> Promise<HttpResponse<R>, HttpException> readObject(final Class<R> responseType) {
Assert.requireNonNull(responseType, "responseType");
connection.addRequestHeader(new HttpHeaderImpl(ACCEPT_CHARSET_HEADER, CHARSET));
connection.addRequestHeader(new HttpHeaderImpl(ACCEPT_HEADER, JSON_MIME_TYPE));
final ResponseContentConverter<R> converter = b -> gson.fromJson(new String(b, CHARSET), responseType);
return createExecutor(converter);
}
Aggregations