Search in sources :

Example 1 with PojoResponse

use of io.servicetalk.examples.http.serialization.json.PojoResponse 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 2 with PojoResponse

use of io.servicetalk.examples.http.serialization.json.PojoResponse 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 3 with PojoResponse

use of io.servicetalk.examples.http.serialization.json.PojoResponse in project servicetalk by apple.

the class BlockingPojoStreamingServer method main.

public static void main(String[] args) throws Exception {
    HttpServers.forPort(8080).listenBlockingStreamingAndAwait((ctx, request, response) -> {
        if (!"/pojos".equals(request.requestTarget())) {
            response.status(NOT_FOUND).sendMetaData().close();
        } else if (!POST.equals(request.method())) {
            response.status(METHOD_NOT_ALLOWED).addHeader(ALLOW, POST.name()).sendMetaData().close();
        } else {
            BlockingIterable<CreatePojoRequest> values = request.payloadBody(REQ_STREAMING_SERIALIZER);
            response.status(CREATED);
            try (HttpPayloadWriter<PojoResponse> writer = response.sendMetaData(RESP_STREAMING_SERIALIZER)) {
                for (CreatePojoRequest req : values) {
                    writer.write(new PojoResponse(ID_GENERATOR.getAndIncrement(), req.getValue()));
                }
            }
        }
    }).awaitShutdown();
}
Also used : CreatePojoRequest(io.servicetalk.examples.http.serialization.json.CreatePojoRequest) RESP_STREAMING_SERIALIZER(io.servicetalk.examples.http.serialization.json.SerializerUtils.RESP_STREAMING_SERIALIZER) PojoResponse(io.servicetalk.examples.http.serialization.json.PojoResponse) POST(io.servicetalk.http.api.HttpRequestMethod.POST) METHOD_NOT_ALLOWED(io.servicetalk.http.api.HttpResponseStatus.METHOD_NOT_ALLOWED) BlockingIterable(io.servicetalk.concurrent.BlockingIterable) CREATED(io.servicetalk.http.api.HttpResponseStatus.CREATED) REQ_STREAMING_SERIALIZER(io.servicetalk.examples.http.serialization.json.SerializerUtils.REQ_STREAMING_SERIALIZER) HttpPayloadWriter(io.servicetalk.http.api.HttpPayloadWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NOT_FOUND(io.servicetalk.http.api.HttpResponseStatus.NOT_FOUND) ALLOW(io.servicetalk.http.api.HttpHeaderNames.ALLOW) HttpServers(io.servicetalk.http.netty.HttpServers) CreatePojoRequest(io.servicetalk.examples.http.serialization.json.CreatePojoRequest) PojoResponse(io.servicetalk.examples.http.serialization.json.PojoResponse)

Aggregations

CreatePojoRequest (io.servicetalk.examples.http.serialization.json.CreatePojoRequest)3 PojoResponse (io.servicetalk.examples.http.serialization.json.PojoResponse)3 REQ_STREAMING_SERIALIZER (io.servicetalk.examples.http.serialization.json.SerializerUtils.REQ_STREAMING_SERIALIZER)3 RESP_STREAMING_SERIALIZER (io.servicetalk.examples.http.serialization.json.SerializerUtils.RESP_STREAMING_SERIALIZER)3 BlockingIterator (io.servicetalk.concurrent.BlockingIterator)2 BlockingStreamingHttpClient (io.servicetalk.http.api.BlockingStreamingHttpClient)2 BlockingStreamingHttpResponse (io.servicetalk.http.api.BlockingStreamingHttpResponse)2 HttpClients (io.servicetalk.http.netty.HttpClients)2 Arrays.asList (java.util.Arrays.asList)2 BlockingIterable (io.servicetalk.concurrent.BlockingIterable)1 ALLOW (io.servicetalk.http.api.HttpHeaderNames.ALLOW)1 HttpPayloadWriter (io.servicetalk.http.api.HttpPayloadWriter)1 POST (io.servicetalk.http.api.HttpRequestMethod.POST)1 CREATED (io.servicetalk.http.api.HttpResponseStatus.CREATED)1 METHOD_NOT_ALLOWED (io.servicetalk.http.api.HttpResponseStatus.METHOD_NOT_ALLOWED)1 NOT_FOUND (io.servicetalk.http.api.HttpResponseStatus.NOT_FOUND)1 HttpServers (io.servicetalk.http.netty.HttpServers)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1