Search in sources :

Example 1 with WebClient

use of io.helidon.webclient.WebClient in project helidon by oracle.

the class ClientMain method main.

/**
 * Executes WebClient examples.
 *
 * If no argument provided it will take server port from configuration server.port.
 *
 * User can override port from configuration by main method parameter with the specific port.
 *
 * @param args main method
 */
public static void main(String[] args) {
    Config config = Config.create();
    String url;
    if (args.length == 0) {
        ConfigValue<Integer> port = config.get("server.port").asInt();
        if (!port.isPresent() || port.get() == -1) {
            throw new IllegalStateException("Unknown port! Please specify port as a main method parameter " + "or directly to config server.port");
        }
        url = "http://localhost:" + port.get() + "/greet";
    } else {
        url = "http://localhost:" + Integer.parseInt(args[0]) + "/greet";
    }
    WebClient webClient = WebClient.builder().baseUri(url).config(config.get("client")).addMediaSupport(JsonpSupport.create()).build();
    performPutMethod(webClient).flatMapSingle(it -> performGetMethod(webClient)).flatMapSingle(it -> followRedirects(webClient)).flatMapSingle(it -> getResponseAsAnJsonObject(webClient)).flatMapSingle(it -> saveResponseToFile(webClient)).flatMapSingle(it -> clientMetricsExample(url, config)).await();
}
Also used : Arrays(java.util.Arrays) WebClient(io.helidon.webclient.WebClient) WebClientService(io.helidon.webclient.spi.WebClientService) Files(java.nio.file.Files) IoMulti(io.helidon.common.reactive.IoMulti) Config(io.helidon.config.Config) WebClientResponse(io.helidon.webclient.WebClientResponse) DataChunk(io.helidon.common.http.DataChunk) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) IOException(java.io.IOException) ConfigValue(io.helidon.config.ConfigValue) Json(jakarta.json.Json) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) Counter(org.eclipse.microprofile.metrics.Counter) Paths(java.nio.file.Paths) JsonObject(jakarta.json.JsonObject) Single(io.helidon.common.reactive.Single) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) Http(io.helidon.common.http.Http) Path(java.nio.file.Path) Collections(java.util.Collections) RegistryFactory(io.helidon.metrics.RegistryFactory) WebClientMetrics(io.helidon.webclient.metrics.WebClientMetrics) Config(io.helidon.config.Config) WebClient(io.helidon.webclient.WebClient)

Example 2 with WebClient

use of io.helidon.webclient.WebClient in project helidon by oracle.

the class ClientMainTest method createWebClient.

private void createWebClient(int port, WebClientService... services) {
    Config config = Config.create();
    WebClient.Builder builder = WebClient.builder().baseUri("http://localhost:" + port + "/greet").config(config.get("client")).addMediaSupport(JsonpSupport.create());
    for (WebClientService service : services) {
        builder.addService(service);
    }
    webClient = builder.build();
}
Also used : Config(io.helidon.config.Config) WebClientService(io.helidon.webclient.spi.WebClientService) WebClient(io.helidon.webclient.WebClient)

Example 3 with WebClient

use of io.helidon.webclient.WebClient in project helidon by oracle.

the class MutualTlsExampleTest method testBuilderAccessSuccessful.

@Test
public void testBuilderAccessSuccessful() {
    webServer = ServerBuilderMain.startServer(-1, -1).await();
    WebClient webClient = ClientBuilderMain.createWebClient();
    assertThat(ClientBuilderMain.callUnsecured(webClient, webServer.port()), is("Hello world unsecured!"));
    assertThat(ClientBuilderMain.callSecured(webClient, webServer.port("secured")), is("Hello Helidon-client!"));
}
Also used : WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 4 with WebClient

use of io.helidon.webclient.WebClient in project helidon by oracle.

the class LoadBalancedCoordinatorTest method waitForRecovery.

private void waitForRecovery(URI lraId) {
    URI coordinatorPath = coordinatorPath(lraId);
    for (int i = 0; i < 10; i++) {
        WebClient client = WebClient.builder().baseUri(coordinatorPath).build();
        WebClientResponse response = client.get().path("recovery").submit().await(TIMEOUT_SEC, TimeUnit.SECONDS);
        String recoveringLras = response.content().as(String.class).await(TIMEOUT_SEC, TimeUnit.SECONDS);
        response.close();
        if (!recoveringLras.contains(lraId.toASCIIString())) {
            LOGGER.fine("LRA is no longer among those recovering " + lraId.toASCIIString());
            // intended LRA is not longer among those recovering
            break;
        }
        LOGGER.fine("Waiting for recovery attempt #" + i + " LRA is still waiting: " + recoveringLras);
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) URI(java.net.URI) WebClient(io.helidon.webclient.WebClient)

Example 5 with WebClient

use of io.helidon.webclient.WebClient in project helidon by oracle.

the class MainTest method runMetricsAndHealthTest.

/**
 * Run some basic metrics and health operations on the server. The server supports
 * running with any of our three JSON libraries: jsonp, jsonb, jackson.
 * So we set a system property to select the library to use before starting
 * the server
 *
 * @param edition     "mp", "se"
 * @param jsonLibrary "jsonp", "jsonb" or "jackson"
 * @param useModules true to use modulepath, false to use classpath
 * @throws Exception on test failure
 */
private void runMetricsAndHealthTest(String edition, String jsonLibrary, boolean useModules) throws Exception {
    List<String> systemPropertyArgs = new LinkedList<>();
    if (jsonLibrary != null && !jsonLibrary.isEmpty()) {
        systemPropertyArgs.add("-Dapp.json-library=" + jsonLibrary);
    }
    HelidonApplication application;
    if (useModules) {
        application = startTheApplicationModule(editionToJarPath(edition), systemPropertyArgs, editionToModuleName(edition));
    } else {
        application = startTheApplication(editionToJarPath(edition), systemPropertyArgs);
    }
    WebClient webClient = WebClient.builder().baseUri(application.getBaseUrl()).addMediaSupport(JsonpSupport.create()).build();
    // Get Prometheus style metrics
    webClient.get().accept(MediaType.WILDCARD).path("/metrics").request(String.class).thenAccept(it -> assertThat("Making sure we got Prometheus format", it, startsWith("# TYPE"))).toCompletableFuture().get();
    // Get JSON encoded metrics
    webClient.get().accept(MediaType.APPLICATION_JSON).path("/metrics").request(JsonObject.class).thenAccept(it -> assertThat("Checking request count", it.getJsonObject("vendor").getInt("requests.count"), greaterThan(0))).toCompletableFuture().get();
    // Get JSON encoded metrics/base
    webClient.get().accept(MediaType.APPLICATION_JSON).path("/metrics/base").request(JsonObject.class).thenAccept(it -> assertThat("Checking request count", it.getInt("thread.count"), greaterThan(0))).toCompletableFuture().get();
    // Get JSON encoded health check
    webClient.get().accept(MediaType.APPLICATION_JSON).path("/health").request(JsonObject.class).thenAccept(it -> {
        assertThat("Checking health status", it.getString("status"), is("UP"));
        // 'microprofile-config.properties' setting in bookstore application
        if (edition.equals("mp")) {
            assertThat("Checking built-in health checks disabled", it.getJsonArray("checks").size(), is(0));
        }
    }).toCompletableFuture().get();
    application.stop();
}
Also used : JsonObject(jakarta.json.JsonObject) WebClient(io.helidon.webclient.WebClient) LinkedList(java.util.LinkedList)

Aggregations

WebClient (io.helidon.webclient.WebClient)58 Test (org.junit.jupiter.api.Test)42 WebClientResponse (io.helidon.webclient.WebClientResponse)21 JsonObject (jakarta.json.JsonObject)15 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)13 Http (io.helidon.common.http.Http)11 Config (io.helidon.config.Config)11 Collections (java.util.Collections)10 WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)9 WebClientService (io.helidon.webclient.spi.WebClientService)9 Json (jakarta.json.Json)9 JsonBuilderFactory (jakarta.json.JsonBuilderFactory)8 IOException (java.io.IOException)8 DataChunk (io.helidon.common.http.DataChunk)7 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)7 CompletionException (java.util.concurrent.CompletionException)7 Context (io.helidon.common.context.Context)6 List (java.util.List)6 TimeUnit (java.util.concurrent.TimeUnit)6 Counter (org.eclipse.microprofile.metrics.Counter)6