Search in sources :

Example 26 with WebClient

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

the class ClientMain method saveResponseToFile.

static Single<Void> saveResponseToFile(WebClient webClient) {
    // We have to create file subscriber first. This subscriber will save the content of the response to the file.
    Path file = Paths.get("test.txt");
    try {
        Files.deleteIfExists(file);
        Files.createFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Downloading server response to file: " + file);
    return webClient.get().request().map(WebClientResponse::content).flatMapSingle(content -> content.map(DataChunk::data).flatMapIterable(Arrays::asList).to(IoMulti.writeToFile(file).build())).peek(path -> System.out.println("Download complete!"));
}
Also used : Path(java.nio.file.Path) 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) WebClientResponse(io.helidon.webclient.WebClientResponse) DataChunk(io.helidon.common.http.DataChunk) IOException(java.io.IOException)

Example 27 with WebClient

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

the class ClientMain method clientMetricsExample.

static Single<String> clientMetricsExample(String url, Config config) {
    // This part here is only for verification purposes, it is not needed to be done for actual usage.
    String counterName = "example.metric.GET.localhost";
    Counter counter = METRIC_REGISTRY.counter(counterName);
    System.out.println(counterName + ": " + counter.getCount());
    // Creates new metric which will count all GET requests and has format of example.metric.GET.<host-name>
    WebClientService clientService = WebClientMetrics.counter().methods(Http.Method.GET).nameFormat("example.metric.%1$s.%2$s").build();
    // This newly created metric now needs to be registered to WebClient.
    WebClient webClient = WebClient.builder().baseUri(url).config(config).addService(clientService).build();
    // Perform any GET request using this newly created WebClient instance.
    return performGetMethod(webClient).peek(s -> System.out.println(counterName + ": " + counter.getCount()));
}
Also used : Counter(org.eclipse.microprofile.metrics.Counter) WebClientService(io.helidon.webclient.spi.WebClientService) WebClient(io.helidon.webclient.WebClient)

Example 28 with WebClient

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

the class MutualTlsExampleTest method testConfigAccessSuccessful.

@Test
public void testConfigAccessSuccessful() {
    Config config = Config.just(() -> ConfigSources.classpath("application-test.yaml").build());
    webServer = ServerConfigMain.startServer(config.get("server")).await();
    WebClient webClient = WebClient.create(config.get("client"));
    assertThat(ClientConfigMain.callUnsecured(webClient, webServer.port()), is("Hello world unsecured!"));
    assertThat(ClientConfigMain.callSecured(webClient, webServer.port("secured")), is("Hello Helidon-client!"));
}
Also used : Config(io.helidon.config.Config) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 29 with WebClient

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

the class ClientBuilderMain method main.

/**
 * Start the example.
 * This example executes two requests by Helidon {@link WebClient} which are configured
 * by the {@link WebClient.Builder}.
 *
 * You have to execute either {@link ServerBuilderMain} or {@link ServerConfigMain} for this to work.
 *
 * If any of the ports has been changed, you have to update ports in this main method also.
 *
 * @param args start arguments are ignored
 */
public static void main(String[] args) {
    WebClient webClient = createWebClient();
    System.out.println("Contacting unsecured endpoint!");
    System.out.println("Response: " + callUnsecured(webClient, 8080));
    System.out.println("Contacting secured endpoint!");
    System.out.println("Response: " + callSecured(webClient, 443));
}
Also used : WebClient(io.helidon.webclient.WebClient)

Example 30 with WebClient

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

the class ClientConfigMain method main.

/**
 * Start the example.
 * This example executes two requests by Helidon {@link WebClient} which are configured
 * by the configuration.
 *
 * You have to execute either {@link ServerBuilderMain} or {@link ServerConfigMain} for this to work.
 *
 * If any of the ports has been changed, you have to update ports in this main method also.
 *
 * @param args start arguments are ignored
 */
public static void main(String[] args) {
    Config config = Config.create();
    WebClient webClient = WebClient.create(config.get("client"));
    System.out.println("Contacting unsecured endpoint!");
    System.out.println("Response: " + callUnsecured(webClient, 8080));
    System.out.println("Contacting secured endpoint!");
    System.out.println("Response: " + callSecured(webClient, 443));
}
Also used : Config(io.helidon.config.Config) WebClient(io.helidon.webclient.WebClient)

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