Search in sources :

Example 21 with HttpClient

use of io.servicetalk.http.api.HttpClient in project servicetalk by apple.

the class SingleAddressRedirectClient method main.

public static void main(String... args) throws Exception {
    try (HttpClient client = HttpClients.forSingleAddress("localhost", NON_SECURE_SERVER_PORT).appendClientFilter(new RedirectingHttpRequesterFilter(new RedirectConfigBuilder().allowedMethods(GET, // by default, POST requests don't follow redirects:
    POST).build())).build()) {
        System.out.println("- Simple GET request:");
        client.request(client.get("/relative")).whenOnSuccess(resp -> {
            System.out.println(resp.toString((name, value) -> value));
            System.out.println(resp.payloadBody(textSerializerAscii()));
            System.out.println();
        }).toFuture().get();
        System.out.println("- POST request with headers and payload body:");
        client.request(client.post("/relative").addHeader(CUSTOM_HEADER, "value").payloadBody(client.executionContext().bufferAllocator().fromAscii("some_content"))).whenOnSuccess(resp -> {
            System.out.println(resp.toString((name, value) -> value));
            System.out.println(resp.payloadBody(textSerializerAscii()));
            System.out.println();
        }).toFuture().get();
    }
}
Also used : NON_SECURE_SERVER_PORT(io.servicetalk.examples.http.redirects.RedirectingServer.NON_SECURE_SERVER_PORT) HttpSerializers.textSerializerAscii(io.servicetalk.http.api.HttpSerializers.textSerializerAscii) CUSTOM_HEADER(io.servicetalk.examples.http.redirects.RedirectingServer.CUSTOM_HEADER) HttpClient(io.servicetalk.http.api.HttpClient) POST(io.servicetalk.http.api.HttpRequestMethod.POST) RedirectingHttpRequesterFilter(io.servicetalk.http.utils.RedirectingHttpRequesterFilter) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) HttpClients(io.servicetalk.http.netty.HttpClients) GET(io.servicetalk.http.api.HttpRequestMethod.GET) HttpClient(io.servicetalk.http.api.HttpClient) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) RedirectingHttpRequesterFilter(io.servicetalk.http.utils.RedirectingHttpRequesterFilter)

Example 22 with HttpClient

use of io.servicetalk.http.api.HttpClient in project servicetalk by apple.

the class RetryClient method main.

public static void main(String[] args) throws Exception {
    try (HttpClient client = HttpClients.forSingleAddress("localhost", 8080).appendClientFilter(new RetryingHttpRequesterFilter.Builder().responseMapper(httpResponseMetaData -> SERVER_ERROR_5XX.contains(httpResponseMetaData.status()) ? // Response status is 500-599, we request a retry
    new RetryingHttpRequesterFilter.HttpResponseException("Retry 5XX", httpResponseMetaData) : // Not a 5XX response, we do not know whether retry is required
    null).retryResponses((meta, error) -> RetryingHttpRequesterFilter.BackOffPolicy.ofImmediate(2)).build()).build()) {
        client.request(client.get("/sayHello")).whenOnSuccess(resp -> {
            System.out.println(resp.toString((name, value) -> value));
            System.out.println(resp.payloadBody(textSerializerUtf8()));
        }).toFuture().get();
    }
}
Also used : RetryingHttpRequesterFilter(io.servicetalk.http.netty.RetryingHttpRequesterFilter) HttpSerializers.textSerializerUtf8(io.servicetalk.http.api.HttpSerializers.textSerializerUtf8) HttpClient(io.servicetalk.http.api.HttpClient) HttpClients(io.servicetalk.http.netty.HttpClients) SERVER_ERROR_5XX(io.servicetalk.http.api.HttpResponseStatus.StatusClass.SERVER_ERROR_5XX) HttpClient(io.servicetalk.http.api.HttpClient) RetryingHttpRequesterFilter(io.servicetalk.http.netty.RetryingHttpRequesterFilter)

Example 23 with HttpClient

use of io.servicetalk.http.api.HttpClient in project servicetalk by apple.

the class PojoUrlClient method main.

public static void main(String[] args) throws Exception {
    try (HttpClient client = HttpClients.forMultiAddressUrl().build()) {
        client.request(client.post("http://localhost:8080/pojos").payloadBody(new CreatePojoRequest("value"), REQ_SERIALIZER)).whenOnSuccess(resp -> {
            System.out.println(resp.toString((name, value) -> value));
            System.out.println(resp.payloadBody(RESP_SERIALIZER));
        }).toFuture().get();
    }
}
Also used : CreatePojoRequest(io.servicetalk.examples.http.serialization.json.CreatePojoRequest) REQ_SERIALIZER(io.servicetalk.examples.http.serialization.json.SerializerUtils.REQ_SERIALIZER) HttpClient(io.servicetalk.http.api.HttpClient) RESP_SERIALIZER(io.servicetalk.examples.http.serialization.json.SerializerUtils.RESP_SERIALIZER) HttpClients(io.servicetalk.http.netty.HttpClients) CreatePojoRequest(io.servicetalk.examples.http.serialization.json.CreatePojoRequest) HttpClient(io.servicetalk.http.api.HttpClient)

Example 24 with HttpClient

use of io.servicetalk.http.api.HttpClient in project servicetalk by apple.

the class ProtobufClient method main.

public static void main(String[] args) throws Exception {
    try (HttpClient client = HttpClients.forSingleAddress("localhost", 8080).build()) {
        client.request(client.post("/protobuf").payloadBody(RequestMessage.newBuilder().setMessage("value").build(), REQ_SERIALIZER)).whenOnSuccess(resp -> {
            System.out.println(resp.toString((name, value) -> value));
            System.out.println(resp.payloadBody(RESP_SERIALIZER));
        }).toFuture().get();
    }
}
Also used : REQ_SERIALIZER(io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.REQ_SERIALIZER) HttpClient(io.servicetalk.http.api.HttpClient) RESP_SERIALIZER(io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.RESP_SERIALIZER) RequestMessage(io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.RequestMessage) HttpClients(io.servicetalk.http.netty.HttpClients) HttpClient(io.servicetalk.http.api.HttpClient)

Example 25 with HttpClient

use of io.servicetalk.http.api.HttpClient in project servicetalk by apple.

the class ProtobufUrlClient method main.

public static void main(String[] args) throws Exception {
    try (HttpClient client = HttpClients.forMultiAddressUrl().build()) {
        client.request(client.post("http://localhost:8080/protobuf").payloadBody(ExampleProtos.RequestMessage.newBuilder().setMessage("hello").build(), REQ_SERIALIZER)).whenOnSuccess(resp -> {
            System.out.println(resp.toString((name, value) -> value));
            System.out.println(resp.payloadBody(RESP_SERIALIZER));
        }).toFuture().get();
    }
}
Also used : ExampleProtos(io.servicetalk.examples.http.serialization.protobuf.ExampleProtos) REQ_SERIALIZER(io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.REQ_SERIALIZER) HttpClient(io.servicetalk.http.api.HttpClient) RESP_SERIALIZER(io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.RESP_SERIALIZER) HttpClients(io.servicetalk.http.netty.HttpClients) HttpClient(io.servicetalk.http.api.HttpClient)

Aggregations

HttpClient (io.servicetalk.http.api.HttpClient)34 HttpResponse (io.servicetalk.http.api.HttpResponse)14 HttpClients (io.servicetalk.http.netty.HttpClients)14 ServerContext (io.servicetalk.transport.api.ServerContext)12 Test (org.junit.jupiter.api.Test)11 HttpRequest (io.servicetalk.http.api.HttpRequest)10 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)9 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)9 HttpSerializers.textSerializerUtf8 (io.servicetalk.http.api.HttpSerializers.textSerializerUtf8)8 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)7 Single (io.servicetalk.concurrent.api.Single)6 SingleAddressHttpClientBuilder (io.servicetalk.http.api.SingleAddressHttpClientBuilder)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)5 BlockingStreamingHttpClient (io.servicetalk.http.api.BlockingStreamingHttpClient)5 CountingInMemorySpanEventListener (io.servicetalk.opentracing.http.TestUtils.CountingInMemorySpanEventListener)5 Completable (io.servicetalk.concurrent.api.Completable)4 Publisher (io.servicetalk.concurrent.api.Publisher)4 FilterableStreamingHttpClient (io.servicetalk.http.api.FilterableStreamingHttpClient)4 List (java.util.List)4