Search in sources :

Example 1 with BlockingStreamingHttpClient

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

the class ClientEffectiveStrategyTest method getResponse.

private String getResponse(ClientType clientType, StreamingHttpClient client) throws Exception {
    switch(clientType) {
        case Blocking:
            BlockingHttpClient blockingClient = client.asBlockingClient();
            return blockingClient.request(blockingClient.get("/")).payloadBody().toString(StandardCharsets.US_ASCII);
        case BlockingStreaming:
            BlockingStreamingHttpClient blockingStreamingClient = client.asBlockingStreamingClient();
            Supplier<CompositeBuffer> supplier = client.executionContext().bufferAllocator()::newCompositeBuffer;
            return StreamSupport.stream(blockingStreamingClient.request(blockingStreamingClient.get("/")).payloadBody().spliterator(), false).reduce((Buffer base, Buffer buffer) -> (base instanceof CompositeBuffer ? ((CompositeBuffer) base) : supplier.get().addBuffer(base)).addBuffer(buffer)).map(buffer -> buffer.toString(StandardCharsets.US_ASCII)).orElse("");
        case AsyncStreaming:
            return client.request(client.get("/")).flatMap(resp -> resp.payloadBody().collect(() -> client.executionContext().bufferAllocator().newCompositeBuffer(), CompositeBuffer::addBuffer)).toFuture().get().toString(StandardCharsets.US_ASCII);
        case Async:
            HttpClient httpClient = client.asClient();
            return httpClient.request(httpClient.get("/")).toFuture().get().payloadBody().toString(StandardCharsets.US_ASCII);
        default:
            fail("Unexpected client type " + clientType);
            /* NOTREACHED */
            return "failed";
    }
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) CompositeBuffer(io.servicetalk.buffer.api.CompositeBuffer) CoreMatchers.is(org.hamcrest.CoreMatchers.is) TestUtils.assertNoAsyncErrors(io.servicetalk.test.resources.TestUtils.assertNoAsyncErrors) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) ResponseData(io.servicetalk.http.netty.ClientEffectiveStrategyTest.ClientOffloadPoint.ResponseData) StreamingHttpConnectionFilterFactory(io.servicetalk.http.api.StreamingHttpConnectionFilterFactory) LoadBalancer(io.servicetalk.client.api.LoadBalancer) ServiceDiscovererEvent(io.servicetalk.client.api.ServiceDiscovererEvent) AfterAll(org.junit.jupiter.api.AfterAll) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) HttpExecutionStrategies.offloadNone(io.servicetalk.http.api.HttpExecutionStrategies.offloadNone) EnumSet(java.util.EnumSet) MethodSource(org.junit.jupiter.params.provider.MethodSource) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) ExecutionMode(org.junit.jupiter.api.parallel.ExecutionMode) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) StreamingHttpConnectionFilter(io.servicetalk.http.api.StreamingHttpConnectionFilter) Arguments(org.junit.jupiter.params.provider.Arguments) InetSocketAddress(java.net.InetSocketAddress) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) FilterableStreamingHttpLoadBalancedConnection(io.servicetalk.http.api.FilterableStreamingHttpLoadBalancedConnection) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Buffer(io.servicetalk.buffer.api.Buffer) Stream(java.util.stream.Stream) HttpExecutionStrategies.offloadAll(io.servicetalk.http.api.HttpExecutionStrategies.offloadAll) ResponseMeta(io.servicetalk.http.netty.ClientEffectiveStrategyTest.ClientOffloadPoint.ResponseMeta) RoundRobinLoadBalancerFactory(io.servicetalk.loadbalancer.RoundRobinLoadBalancerFactory) Queue(java.util.Queue) Assertions.fail(org.junit.jupiter.api.Assertions.fail) RequestPayloadSubscription(io.servicetalk.http.netty.ClientEffectiveStrategyTest.ClientOffloadPoint.RequestPayloadSubscription) LoadBalancerFactory(io.servicetalk.client.api.LoadBalancerFactory) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Publisher(io.servicetalk.concurrent.api.Publisher) StreamingHttpRequester(io.servicetalk.http.api.StreamingHttpRequester) FilterableStreamingHttpConnection(io.servicetalk.http.api.FilterableStreamingHttpConnection) HttpLoadBalancerFactory(io.servicetalk.http.api.HttpLoadBalancerFactory) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) IoThreadFactory(io.servicetalk.transport.api.IoThreadFactory) ExecutionStrategy(io.servicetalk.transport.api.ExecutionStrategy) HttpExecutionStrategies(io.servicetalk.http.api.HttpExecutionStrategies) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) HttpClient(io.servicetalk.http.api.HttpClient) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) StreamSupport(java.util.stream.StreamSupport) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) Nullable(javax.annotation.Nullable) ConnectionFactory(io.servicetalk.client.api.ConnectionFactory) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) Single(io.servicetalk.concurrent.api.Single) Matchers(org.hamcrest.Matchers) CompositeBuffer(io.servicetalk.buffer.api.CompositeBuffer) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) SingleAddressHttpClientBuilder(io.servicetalk.http.api.SingleAddressHttpClientBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) StreamingHttpClientFilterFactory(io.servicetalk.http.api.StreamingHttpClientFilterFactory) Execution(org.junit.jupiter.api.parallel.Execution) HostAndPort(io.servicetalk.transport.api.HostAndPort) HttpExecutionStrategies.offloadNever(io.servicetalk.http.api.HttpExecutionStrategies.offloadNever) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) CompositeBuffer(io.servicetalk.buffer.api.CompositeBuffer) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpClient(io.servicetalk.http.api.HttpClient) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient)

Example 2 with BlockingStreamingHttpClient

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

the class BlockingProtobufStreamingUrlClient method main.

public static void main(String[] args) throws Exception {
    try (BlockingStreamingHttpClient client = HttpClients.forMultiAddressUrl().buildBlockingStreaming()) {
        BlockingStreamingHttpResponse response = client.request(client.post("http://localhost:8080/protobuf").payloadBody(asList(RequestMessage.newBuilder().setMessage("value1").build(), RequestMessage.newBuilder().setMessage("value22").build(), RequestMessage.newBuilder().setMessage("value333").build()), REQ_STREAMING_SERIALIZER));
        System.out.println(response.toString((name, value) -> value));
        // the full response payload body is drained in case of exceptions
        try (BlockingIterator<ResponseMessage> payload = response.payloadBody(RESP_STREAMING_SERIALIZER).iterator()) {
            while (payload.hasNext()) {
                System.out.println(payload.next());
            }
        }
    }
}
Also used : BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) Arrays.asList(java.util.Arrays.asList) RESP_STREAMING_SERIALIZER(io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.RESP_STREAMING_SERIALIZER) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) REQ_STREAMING_SERIALIZER(io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.REQ_STREAMING_SERIALIZER) ResponseMessage(io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.ResponseMessage) BlockingIterator(io.servicetalk.concurrent.BlockingIterator) RequestMessage(io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.RequestMessage) HttpClients(io.servicetalk.http.netty.HttpClients) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) ResponseMessage(io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.ResponseMessage)

Example 3 with BlockingStreamingHttpClient

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

the class BlockingProtobufStreamingClient method main.

public static void main(String[] args) throws Exception {
    try (BlockingStreamingHttpClient client = HttpClients.forSingleAddress("localhost", 8080).buildBlockingStreaming()) {
        BlockingStreamingHttpResponse response = client.request(client.post("/protobuf").payloadBody(asList(RequestMessage.newBuilder().setMessage("value1").build(), RequestMessage.newBuilder().setMessage("value22").build(), RequestMessage.newBuilder().setMessage("value333").build()), REQ_STREAMING_SERIALIZER));
        System.out.println(response.toString((name, value) -> value));
        // the full response payload body is drained in case of exceptions
        try (BlockingIterator<ResponseMessage> payload = response.payloadBody(RESP_STREAMING_SERIALIZER).iterator()) {
            while (payload.hasNext()) {
                System.out.println(payload.next());
            }
        }
    }
}
Also used : BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) Arrays.asList(java.util.Arrays.asList) RESP_STREAMING_SERIALIZER(io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.RESP_STREAMING_SERIALIZER) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) REQ_STREAMING_SERIALIZER(io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.REQ_STREAMING_SERIALIZER) ResponseMessage(io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.ResponseMessage) BlockingIterator(io.servicetalk.concurrent.BlockingIterator) RequestMessage(io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.RequestMessage) HttpClients(io.servicetalk.http.netty.HttpClients) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) ResponseMessage(io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.ResponseMessage)

Example 4 with BlockingStreamingHttpClient

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

the class BlockingHelloWorldStreamingClient method main.

public static void main(String[] args) throws Exception {
    try (BlockingStreamingHttpClient client = HttpClients.forSingleAddress("localhost", 8080).buildBlockingStreaming()) {
        BlockingStreamingHttpResponse response = client.request(client.get("/sayHello"));
        System.out.println(response.toString((name, value) -> value));
        // the full response payload body is drained in case of exceptions
        try (BlockingIterator<String> payload = response.payloadBody(appSerializerUtf8FixLen()).iterator()) {
            while (payload.hasNext()) {
                System.out.println(payload.next());
            }
        }
    }
}
Also used : BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) HttpSerializers.appSerializerUtf8FixLen(io.servicetalk.http.api.HttpSerializers.appSerializerUtf8FixLen) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) BlockingIterator(io.servicetalk.concurrent.BlockingIterator) HttpClients(io.servicetalk.http.netty.HttpClients) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse)

Example 5 with BlockingStreamingHttpClient

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

the class BlockingHelloWorldStreamingUrlClient method main.

public static void main(String[] args) throws Exception {
    try (BlockingStreamingHttpClient client = HttpClients.forMultiAddressUrl().buildBlockingStreaming()) {
        BlockingStreamingHttpResponse response = client.request(client.get("http://localhost:8080/sayHello"));
        System.out.println(response.toString((name, value) -> value));
        // the full response payload body is drained in case of exceptions
        try (BlockingIterator<String> payload = response.payloadBody(appSerializerUtf8FixLen()).iterator()) {
            while (payload.hasNext()) {
                System.out.println(payload.next());
            }
        }
    }
}
Also used : BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) HttpSerializers.appSerializerUtf8FixLen(io.servicetalk.http.api.HttpSerializers.appSerializerUtf8FixLen) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) BlockingIterator(io.servicetalk.concurrent.BlockingIterator) HttpClients(io.servicetalk.http.netty.HttpClients) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse)

Aggregations

BlockingStreamingHttpClient (io.servicetalk.http.api.BlockingStreamingHttpClient)24 BlockingStreamingHttpResponse (io.servicetalk.http.api.BlockingStreamingHttpResponse)20 Test (org.junit.jupiter.api.Test)10 BlockingIterator (io.servicetalk.concurrent.BlockingIterator)7 HttpClients (io.servicetalk.http.netty.HttpClients)6 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)5 Buffer (io.servicetalk.buffer.api.Buffer)4 BlockingStreamingHttpRequest (io.servicetalk.http.api.BlockingStreamingHttpRequest)4 HttpClient (io.servicetalk.http.api.HttpClient)4 Arrays.asList (java.util.Arrays.asList)4 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)3 Nullable (javax.annotation.Nullable)3 Publisher (io.servicetalk.concurrent.api.Publisher)2 Single (io.servicetalk.concurrent.api.Single)2 CreatePojoRequest (io.servicetalk.examples.http.serialization.json.CreatePojoRequest)2 PojoResponse (io.servicetalk.examples.http.serialization.json.PojoResponse)2 REQ_STREAMING_SERIALIZER (io.servicetalk.examples.http.serialization.json.SerializerUtils.REQ_STREAMING_SERIALIZER)2 RESP_STREAMING_SERIALIZER (io.servicetalk.examples.http.serialization.json.SerializerUtils.RESP_STREAMING_SERIALIZER)2 RequestMessage (io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.RequestMessage)2 ResponseMessage (io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.ResponseMessage)2