Search in sources :

Example 6 with HttpClient

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);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClient(com.canoo.platform.core.http.HttpClient) HttpResponse(com.canoo.platform.core.http.HttpResponse) Test(org.testng.annotations.Test)

Example 7 with HttpClient

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);
}
Also used : AfterClass(org.testng.annotations.AfterClass) SC_HTTP_RESOURCE_NOTFOUND(com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_RESOURCE_NOTFOUND) ByteArrayProvider(com.canoo.platform.core.http.ByteArrayProvider) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ConnectionException(com.canoo.platform.core.http.ConnectionException) HttpResponse(com.canoo.platform.core.http.HttpResponse) BeforeClass(org.testng.annotations.BeforeClass) HttpClient(com.canoo.platform.core.http.HttpClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) CompletableFuture(java.util.concurrent.CompletableFuture) HttpHeaderConstants(com.canoo.dp.impl.platform.core.http.HttpHeaderConstants) BadResponseException(com.canoo.platform.core.http.BadResponseException) SC_HTTP_UNAUTHORIZED(com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_UNAUTHORIZED) ServerSocket(java.net.ServerSocket) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PlatformClient(com.canoo.platform.client.PlatformClient) Assert(org.testng.Assert) Gson(com.google.gson.Gson) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Spark(spark.Spark) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClient(com.canoo.platform.core.http.HttpClient) HttpResponse(com.canoo.platform.core.http.HttpResponse) ByteArrayProvider(com.canoo.platform.core.http.ByteArrayProvider) Test(org.testng.annotations.Test)

Example 8 with HttpClient

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);
}
Also used : HttpClient(com.canoo.platform.core.http.HttpClient) Security(com.canoo.platform.client.security.Security)

Example 9 with HttpClient

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");
}
Also used : HttpClient(com.canoo.platform.core.http.HttpClient)

Example 10 with HttpClient

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();
}
Also used : Application(javafx.application.Application) Button(javafx.scene.control.Button) Scene(javafx.scene.Scene) Alert(javafx.scene.control.Alert) HBox(javafx.scene.layout.HBox) TextField(javafx.scene.control.TextField) PlatformClient(com.canoo.platform.client.PlatformClient) Insets(javafx.geometry.Insets) RequestMethod(com.canoo.platform.core.http.RequestMethod) Stage(javafx.stage.Stage) HttpClient(com.canoo.platform.core.http.HttpClient) FxToolkit(com.canoo.platform.client.javafx.FxToolkit) HBox(javafx.scene.layout.HBox) Insets(javafx.geometry.Insets) Button(javafx.scene.control.Button) HttpClient(com.canoo.platform.core.http.HttpClient) TextField(javafx.scene.control.TextField) Scene(javafx.scene.Scene)

Aggregations

HttpClient (com.canoo.platform.core.http.HttpClient)15 PlatformClient (com.canoo.platform.client.PlatformClient)10 HttpResponse (com.canoo.platform.core.http.HttpResponse)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Test (org.testng.annotations.Test)10 CompletableFuture (java.util.concurrent.CompletableFuture)9 HttpHeaderConstants (com.canoo.dp.impl.platform.core.http.HttpHeaderConstants)8 SC_HTTP_RESOURCE_NOTFOUND (com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_RESOURCE_NOTFOUND)8 SC_HTTP_UNAUTHORIZED (com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_UNAUTHORIZED)8 BadResponseException (com.canoo.platform.core.http.BadResponseException)8 ByteArrayProvider (com.canoo.platform.core.http.ByteArrayProvider)8 ConnectionException (com.canoo.platform.core.http.ConnectionException)8 Gson (com.google.gson.Gson)8 ServerSocket (java.net.ServerSocket)8 ExecutionException (java.util.concurrent.ExecutionException)8 TimeUnit (java.util.concurrent.TimeUnit)8 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)8 Matchers.is (org.hamcrest.Matchers.is)8 Matchers.notNullValue (org.hamcrest.Matchers.notNullValue)8 Assert (org.testng.Assert)8