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";
}
}
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());
}
}
}
}
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());
}
}
}
}
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());
}
}
}
}
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());
}
}
}
}
Aggregations