use of com.canoo.platform.core.http.HttpClient in project dolphin-platform by canoo.
the class HttpClientTests method testSimpleGet.
@Test
public void testSimpleGet() 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<Void>> future = getHttpResponseCompletableFuture(client, actionCalled, doneCalled, errorCalled);
// then:
future.get(1_000, TimeUnit.MILLISECONDS);
final HttpResponse<Void> 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());
assertThatDoneCalledAndErrorNotCalled(actionCalled, doneCalled, errorCalled);
}
use of com.canoo.platform.core.http.HttpClient in project dolphin-platform by canoo.
the class HttpClientTests method testSimpleGetWithByteContent.
@Test
public void testSimpleGetWithByteContent() 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<ByteArrayProvider>> future = client.get("http://localhost:" + freePort).withoutContent().readBytes().onDone(response -> {
actionCalled.set(true);
doneCalled.set(true);
}).onError(e -> {
actionCalled.set(true);
errorCalled.set(true);
}).execute();
// then:
final HttpResponse<ByteArrayProvider> 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 byte[] bytes = response.getContent().get();
assertThat("Byte content does not match", bytes, is("Spark Server for HTTP client integration tests".getBytes()));
assertThatDoneCalledAndErrorNotCalled(actionCalled, doneCalled, errorCalled);
}
use of com.canoo.platform.core.http.HttpClient in project dolphin-platform-examples by canoo.
the class Client method main.
public static void main(String[] args) throws Exception {
final HttpClient client = PlatformClient.getService(HttpClient.class);
final String message = client.get("http://localhost:8080/api/message").withoutContent().readString().execute().get().getContent();
System.out.println(message);
final Security security = PlatformClient.getService(Security.class);
security.login("admin", "admin").get();
final String message2 = client.get("http://localhost:8080/api/secure/message").withoutContent().readString().execute().get().getContent();
System.out.println(message2);
}
use of com.canoo.platform.core.http.HttpClient in project dolphin-platform-examples by canoo.
the class RestClient method main.
public static void main(String[] args) throws Exception {
final String endpoint = "http://localhost:8080/openid-connect";
final HttpClient httpClient = PlatformClient.getService(HttpClient.class);
final City city = new City("Dortmund", "Germany");
final CityDetails details = httpClient.post(endpoint).withContent(city).readObject(CityDetails.class).execute().get().getContent();
System.out.println("City " + details.getName() + " has " + details.getPopulation() + " citizens");
}
use of com.canoo.platform.core.http.HttpClient in project dolphin-platform-examples by canoo.
the class RestClient method start.
@Override
public void start(Stage primaryStage) throws Exception {
final String endpoint = "http://localhost:8080/simple-rest/api/city";
final HttpClient httpClient = PlatformClient.getService(HttpClient.class);
final TextField cityField = new TextField();
cityField.setPromptText("Enter city name");
final Button button = new Button("Find details");
button.setOnAction(e -> {
final City city = new City(cityField.getText(), "Germany");
httpClient.request(endpoint, RequestMethod.POST).withContent(city).readObject(CityDetails.class).onDone(d -> showResult(d.getContent())).onError(ex -> showError(city)).execute();
});
final HBox pane = new HBox(cityField, button);
pane.setSpacing(24);
pane.setPadding(new Insets(24));
primaryStage.setScene(new Scene(pane));
primaryStage.show();
}
Aggregations