Search in sources :

Example 21 with BlockingStreamingHttpClient

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

the class BlockingPojoStreamingClient method main.

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

Example 22 with BlockingStreamingHttpClient

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

the class BlockingPojoStreamingUrlClient 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/pojos").payloadBody(asList(new CreatePojoRequest("value1"), new CreatePojoRequest("value2"), new CreatePojoRequest("value3")), REQ_STREAMING_SERIALIZER));
        System.out.println(response.toString((name, value) -> value));
        // the full response payload body is drained in case of exceptions
        try (BlockingIterator<PojoResponse> payload = response.payloadBody(RESP_STREAMING_SERIALIZER).iterator()) {
            while (payload.hasNext()) {
                System.out.println(payload.next());
            }
        }
    }
}
Also used : BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) CreatePojoRequest(io.servicetalk.examples.http.serialization.json.CreatePojoRequest) RESP_STREAMING_SERIALIZER(io.servicetalk.examples.http.serialization.json.SerializerUtils.RESP_STREAMING_SERIALIZER) REQ_STREAMING_SERIALIZER(io.servicetalk.examples.http.serialization.json.SerializerUtils.REQ_STREAMING_SERIALIZER) PojoResponse(io.servicetalk.examples.http.serialization.json.PojoResponse) Arrays.asList(java.util.Arrays.asList) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) BlockingIterator(io.servicetalk.concurrent.BlockingIterator) HttpClients(io.servicetalk.http.netty.HttpClients) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) CreatePojoRequest(io.servicetalk.examples.http.serialization.json.CreatePojoRequest) PojoResponse(io.servicetalk.examples.http.serialization.json.PojoResponse) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse)

Example 23 with BlockingStreamingHttpClient

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

the class ServiceTalkContentCodingTest method assertSuccessful.

protected void assertSuccessful(final ContentCodec encoding) throws Throwable {
    assertResponse(client().request(client().get("/").encoding(encoding).payloadBody(payloadAsString((byte) 'a'), textSerializerUtf8())).toStreamingResponse());
    final BlockingStreamingHttpClient blockingStreamingHttpClient = client().asBlockingStreamingClient();
    assertResponse(blockingStreamingHttpClient.request(blockingStreamingHttpClient.get("/").encoding(encoding).payloadBody(singletonList(payloadAsString((byte) 'a')), textSerializer())).toStreamingResponse());
    final StreamingHttpClient streamingHttpClient = client().asStreamingClient();
    assertResponse(streamingHttpClient.request(streamingHttpClient.get("/").encoding(encoding).payloadBody(from(payloadAsString((byte) 'a')), textSerializer())).toFuture().get());
}
Also used : BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient)

Example 24 with BlockingStreamingHttpClient

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

the class AsyncContextHttpFilterVerifier method verifyServerFilterAsyncContextVisibility.

/**
 * Verify that all interactions with the request/response and message-body from a request that goes through
 * the provided {@link StreamingHttpServiceFilterFactory} filter, have valid visibility of the {@link AsyncContext}.
 *
 * @param filter The {@link StreamingHttpServiceFilterFactory} filter to verify.
 */
public static void verifyServerFilterAsyncContextVisibility(final StreamingHttpServiceFilterFactory filter) throws Exception {
    final BlockingQueue<Throwable> errors = new LinkedBlockingDeque<>();
    final List<String> payload = singletonList("Hello World");
    final ServerContext serverContext = forAddress(localAddress(0)).appendServiceFilter(asyncContextAssertionFilter(errors)).appendServiceFilter(filter).listenStreamingAndAwait(asyncContextRequestHandler(errors));
    final BlockingStreamingHttpClient client = forSingleAddress(serverHostAndPort(serverContext)).buildBlockingStreaming();
    final BlockingStreamingHttpRequest request = client.post("/test").payloadBody(payload, appSerializerUtf8FixLen());
    final BlockingStreamingHttpResponse resp = client.request(request);
    assertThat(resp.status(), is(OK));
    Iterator<String> itr = resp.payloadBody(appSerializerUtf8FixLen()).iterator();
    assertThat(itr.hasNext(), is(true));
    assertThat(itr.next(), is(payload.get(0)));
    assertThat(itr.hasNext(), is(false));
    assertNoAsyncErrors(errors);
}
Also used : BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingStreamingHttpRequest(io.servicetalk.http.api.BlockingStreamingHttpRequest) 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