Search in sources :

Example 16 with BlockingStreamingHttpClient

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

the class BlockingStreamingHttpServiceTest method doNotWriteTheLastChunk.

@Test
void doNotWriteTheLastChunk() throws Exception {
    BlockingStreamingHttpClient client = context((ctx, request, response) -> {
        response.sendMetaData();
    // Do not close()
    });
    BlockingStreamingHttpResponse response = client.request(client.get("/"));
    assertResponse(response);
    final BlockingIterator<Buffer> iterator = response.payloadBody().iterator();
    assertThrows(TimeoutException.class, () -> iterator.hasNext(1, SECONDS));
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Example 17 with BlockingStreamingHttpClient

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

the class BlockingStreamingHttpServiceTest method echoServer.

private void echoServer(BlockingStreamingHttpService handler) throws Exception {
    BlockingStreamingHttpClient client = context(handler);
    BlockingStreamingHttpResponse response = client.request(client.post("/").payloadBody(asList("Hello\n", "World\n"), appSerializerUtf8FixLen()));
    assertResponse(response, HELLO_WORLD, true);
}
Also used : BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse)

Example 18 with BlockingStreamingHttpClient

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

the class BlockingStreamingHttpServiceTest method respondWithCustomMetaData.

@Test
void respondWithCustomMetaData() throws Exception {
    BlockingStreamingHttpClient client = context((ctx, request, response) -> {
        response.status(ACCEPTED);
        CharSequence auth = request.headers().get("X-User-Header");
        if (auth != null) {
            response.addHeader("X-User-Header", auth);
        }
        response.addHeader("X-Server-Header", "X-Server-Value");
        response.sendMetaData().close();
    });
    BlockingStreamingHttpResponse response = client.request(client.get("/").addHeader("X-User-Header", "X-User-Value"));
    assertThat(response.status(), is(ACCEPTED));
    assertThat(response.version(), is(HTTP_1_1));
    assertThat(response.headers().get("X-User-Header").toString(), is("X-User-Value"));
    assertThat(response.headers().get("X-Server-Header").toString(), is("X-Server-Value"));
    assertThat(response.toResponse().toFuture().get().payloadBody(), is(EMPTY_BUFFER));
}
Also used : BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Example 19 with BlockingStreamingHttpClient

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

the class BlockingStreamingHttpServiceTest method respondWithPayloadBodyAndTrailers.

private void respondWithPayloadBodyAndTrailers(BlockingStreamingHttpService handler, boolean useDeserializer) throws Exception {
    BlockingStreamingHttpClient client = context(handler);
    BlockingStreamingHttpResponse response = client.request(client.get("/"));
    assertResponse(response);
    assertThat(response.headers().get(TRAILER).toString(), is(X_TOTAL_LENGTH));
    final StringBuilder sb = new StringBuilder();
    final HttpHeaders trailers;
    if (useDeserializer) {
        HttpMessageBodyIterator<String> msgBody = response.messageBody(appSerializerUtf8FixLen()).iterator();
        while (msgBody.hasNext()) {
            sb.append(msgBody.next());
        }
        trailers = msgBody.trailers();
    } else {
        HttpMessageBodyIterator<Buffer> msgBody = response.messageBody().iterator();
        while (msgBody.hasNext()) {
            sb.append(requireNonNull(msgBody.next()).toString(UTF_8));
        }
        trailers = msgBody.trailers();
    }
    assertThat(sb.toString(), is(HELLO_WORLD));
    assertThat(trailers, notNullValue());
    assertThat(trailers.get(X_TOTAL_LENGTH).toString(), is(HELLO_WORLD_LENGTH));
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpHeaders(io.servicetalk.http.api.HttpHeaders) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse)

Example 20 with BlockingStreamingHttpClient

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

the class DefaultGrpcClientCallFactory method newBlockingStreamingCall.

@Override
public <Req, Resp> BlockingStreamingClientCall<Req, Resp> newBlockingStreamingCall(final MethodDescriptor<Req, Resp> methodDescriptor, final BufferDecoderGroup decompressors) {
    GrpcStreamingSerializer<Req> serializerIdentity = streamingSerializer(methodDescriptor);
    GrpcStreamingDeserializer<Resp> deserializerIdentity = streamingDeserializer(methodDescriptor);
    List<GrpcStreamingDeserializer<Resp>> deserializers = streamingDeserializers(methodDescriptor, decompressors.decoders());
    CharSequence acceptedEncoding = decompressors.advertisedMessageEncoding();
    CharSequence requestContentType = grpcContentType(methodDescriptor.requestDescriptor().serializerDescriptor().contentType());
    CharSequence responseContentType = grpcContentType(methodDescriptor.responseDescriptor().serializerDescriptor().contentType());
    final BlockingStreamingHttpClient client = streamingHttpClient.asBlockingStreamingClient();
    return (metadata, request) -> {
        Duration timeout = timeoutForRequest(metadata.timeout());
        GrpcStreamingSerializer<Req> serializer = streamingSerializer(methodDescriptor, serializerIdentity, metadata.requestCompressor());
        String mdPath = methodDescriptor.httpPath();
        BlockingStreamingHttpRequest httpRequest = client.post(UNKNOWN_PATH.equals(mdPath) ? metadata.path() : mdPath);
        initRequest(httpRequest, requestContentType, serializer.messageEncoding(), acceptedEncoding, timeout);
        httpRequest.payloadBody(serializer.serialize(request, streamingHttpClient.executionContext().bufferAllocator()));
        try {
            assignStrategy(httpRequest, metadata);
            final BlockingStreamingHttpResponse response = client.request(httpRequest);
            return validateResponseAndGetPayload(response.toStreamingResponse(), responseContentType, client.executionContext().bufferAllocator(), readGrpcMessageEncodingRaw(response.headers(), deserializerIdentity, deserializers, GrpcStreamingDeserializer::messageEncoding)).toIterable();
        } catch (Throwable cause) {
            throw toGrpcException(cause);
        }
    };
}
Also used : GrpcUtils.serializerDeserializer(io.servicetalk.grpc.api.GrpcUtils.serializerDeserializer) Publisher(io.servicetalk.concurrent.api.Publisher) DefaultMethodDescriptor(io.servicetalk.grpc.api.GrpcUtils.DefaultMethodDescriptor) GrpcUtils.decompressors(io.servicetalk.grpc.api.GrpcUtils.decompressors) GrpcUtils.readGrpcMessageEncodingRaw(io.servicetalk.grpc.api.GrpcUtils.readGrpcMessageEncodingRaw) GrpcUtils.defaultToInt(io.servicetalk.grpc.api.GrpcUtils.defaultToInt) AsyncContext(io.servicetalk.concurrent.api.AsyncContext) HttpRequest(io.servicetalk.http.api.HttpRequest) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) BufferDecoderGroup(io.servicetalk.encoding.api.BufferDecoderGroup) GRPC_CONTENT_TYPE_PROTO_SUFFIX(io.servicetalk.grpc.api.GrpcHeaderValues.GRPC_CONTENT_TYPE_PROTO_SUFFIX) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) HttpClient(io.servicetalk.http.api.HttpClient) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) GrpcUtils.toGrpcException(io.servicetalk.grpc.api.GrpcUtils.toGrpcException) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) Nullable(javax.annotation.Nullable) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) Completable(io.servicetalk.concurrent.api.Completable) HttpResponse(io.servicetalk.http.api.HttpResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Identity.identityEncoder(io.servicetalk.encoding.api.Identity.identityEncoder) GrpcUtils.grpcContentType(io.servicetalk.grpc.api.GrpcUtils.grpcContentType) GrpcUtils.validateResponseAndGetPayload(io.servicetalk.grpc.api.GrpcUtils.validateResponseAndGetPayload) HTTP_EXECUTION_STRATEGY_KEY(io.servicetalk.http.api.HttpContextKeys.HTTP_EXECUTION_STRATEGY_KEY) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) GrpcUtils.initRequest(io.servicetalk.grpc.api.GrpcUtils.initRequest) BlockingIterables.singletonBlockingIterable(io.servicetalk.concurrent.internal.BlockingIterables.singletonBlockingIterable) GRPC_DEADLINE_KEY(io.servicetalk.grpc.internal.DeadlineUtils.GRPC_DEADLINE_KEY) BlockingIterator(io.servicetalk.concurrent.BlockingIterator) BufferDecoder(io.servicetalk.encoding.api.BufferDecoder) BlockingStreamingHttpRequest(io.servicetalk.http.api.BlockingStreamingHttpRequest) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) BlockingStreamingHttpRequest(io.servicetalk.http.api.BlockingStreamingHttpRequest) Duration(java.time.Duration) 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