io.vertx.rxjava3.grpc.client.GrpcClient Maven / Gradle / Ivy
/*
* Copyright 2014 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.vertx.rxjava3.grpc.client;
import io.vertx.rxjava3.RxHelper;
import io.vertx.rxjava3.ObservableHelper;
import io.vertx.rxjava3.FlowableHelper;
import io.vertx.rxjava3.impl.AsyncResultMaybe;
import io.vertx.rxjava3.impl.AsyncResultSingle;
import io.vertx.rxjava3.impl.AsyncResultCompletable;
import io.vertx.rxjava3.WriteStreamObserver;
import io.vertx.rxjava3.WriteStreamSubscriber;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.Iterator;
import java.util.function.Function;
import java.util.stream.Collectors;
import io.vertx.core.Handler;
import io.vertx.core.AsyncResult;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import io.vertx.lang.rx.RxGen;
import io.vertx.lang.rx.TypeArg;
import io.vertx.lang.rx.MappingIterator;
/**
* A gRPC client for Vert.x
*
* Unlike traditional gRPC clients, this client does not rely on a generated RPC interface to interact with the service.
*
* Instead, you can interact with the service with a request/response interfaces and gRPC messages, very much like
* a traditional client.
*
* The client exposes 2 levels of API
*
*
* - a Protobuf message {@link io.vertx.rxjava3.grpc.client.GrpcClient #request(SocketAddress) API}: {@link io.vertx.rxjava3.grpc.client.GrpcClientRequest}/{@link io.vertx.rxjava3.grpc.client.GrpcClientResponse} with Protobuf messages to call any gRPC service in a generic way
* - a gRPC message {@link io.vertx.rxjava3.grpc.client.GrpcClient#request}: {@link io.vertx.rxjava3.grpc.client.GrpcClientRequest}/{@link io.vertx.rxjava3.grpc.client.GrpcClientRequest} with gRPC messages to call a given method of a gRPC service
*
*
*
* NOTE: This class has been automatically generated from the {@link io.vertx.grpc.client.GrpcClient original} non RX-ified interface using Vert.x codegen.
*/
@RxGen(io.vertx.grpc.client.GrpcClient.class)
public class GrpcClient {
@Override
public String toString() {
return delegate.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GrpcClient that = (GrpcClient) o;
return delegate.equals(that.delegate);
}
@Override
public int hashCode() {
return delegate.hashCode();
}
public static final TypeArg __TYPE_ARG = new TypeArg<>( obj -> new GrpcClient((io.vertx.grpc.client.GrpcClient) obj),
GrpcClient::getDelegate
);
private final io.vertx.grpc.client.GrpcClient delegate;
public GrpcClient(io.vertx.grpc.client.GrpcClient delegate) {
this.delegate = delegate;
}
public GrpcClient(Object delegate) {
this.delegate = (io.vertx.grpc.client.GrpcClient)delegate;
}
public io.vertx.grpc.client.GrpcClient getDelegate() {
return delegate;
}
private static final TypeArg> TYPE_ARG_0 = new TypeArg>(o1 -> io.vertx.rxjava3.grpc.client.GrpcClientRequest.newInstance((io.vertx.grpc.client.GrpcClientRequest)o1, new TypeArg(o2 -> io.vertx.rxjava3.core.buffer.Buffer.newInstance((io.vertx.core.buffer.Buffer)o2), o2 -> o2.getDelegate()), new TypeArg(o2 -> io.vertx.rxjava3.core.buffer.Buffer.newInstance((io.vertx.core.buffer.Buffer)o2), o2 -> o2.getDelegate())), o1 -> o1.getDelegate());
/**
* Create a new client
* @param vertx the vertx instance
* @return the created client
*/
public static io.vertx.rxjava3.grpc.client.GrpcClient client(io.vertx.rxjava3.core.Vertx vertx) {
io.vertx.rxjava3.grpc.client.GrpcClient ret = io.vertx.rxjava3.grpc.client.GrpcClient.newInstance((io.vertx.grpc.client.GrpcClient)io.vertx.grpc.client.GrpcClient.client(vertx.getDelegate()));
return ret;
}
/**
* Create a new client
* @param vertx the vertx instance
* @param options the client options
* @return the created client
*/
public static io.vertx.rxjava3.grpc.client.GrpcClient client(io.vertx.rxjava3.core.Vertx vertx, io.vertx.core.http.HttpClientOptions options) {
io.vertx.rxjava3.grpc.client.GrpcClient ret = io.vertx.rxjava3.grpc.client.GrpcClient.newInstance((io.vertx.grpc.client.GrpcClient)io.vertx.grpc.client.GrpcClient.client(vertx.getDelegate(), options));
return ret;
}
/**
* Connect to the remote server
and create a request for any hosted gRPC service.
* @param server the server hosting the service
* @return a future request
*/
public io.reactivex.rxjava3.core.Single> request(io.vertx.rxjava3.core.net.SocketAddress server) {
io.reactivex.rxjava3.core.Single> ret = rxRequest(server);
ret = ret.cache();
ret.subscribe(io.vertx.rxjava3.SingleHelper.nullObserver());
return ret;
}
/**
* Connect to the remote server
and create a request for any hosted gRPC service.
* @param server the server hosting the service
* @return a future request
*/
public io.reactivex.rxjava3.core.Single> rxRequest(io.vertx.rxjava3.core.net.SocketAddress server) {
return AsyncResultSingle.toSingle(delegate.request(server.getDelegate()), __value -> io.vertx.rxjava3.grpc.client.GrpcClientRequest.newInstance((io.vertx.grpc.client.GrpcClientRequest)__value, new TypeArg(o0 -> io.vertx.rxjava3.core.buffer.Buffer.newInstance((io.vertx.core.buffer.Buffer)o0), o0 -> o0.getDelegate()), new TypeArg(o0 -> io.vertx.rxjava3.core.buffer.Buffer.newInstance((io.vertx.core.buffer.Buffer)o0), o0 -> o0.getDelegate())));
}
/**
* Close this client.
* @return
*/
public io.reactivex.rxjava3.core.Completable close() {
io.reactivex.rxjava3.core.Completable ret = rxClose();
ret = ret.cache();
ret.subscribe(io.vertx.rxjava3.CompletableHelper.nullObserver());
return ret;
}
/**
* Close this client.
* @return
*/
public io.reactivex.rxjava3.core.Completable rxClose() {
return AsyncResultCompletable.toCompletable(delegate.close());
}
/**
* Connect to the remote server
and create a request for given method
of a hosted gRPC service.
* @param server the server hosting the service
* @param service the service to be called
* @return a future request
*/
public io.reactivex.rxjava3.core.Single> request(io.vertx.rxjava3.core.net.SocketAddress server, io.grpc.MethodDescriptor service) {
io.reactivex.rxjava3.core.Single> ret = rxRequest(server, service);
ret = ret.cache();
ret.subscribe(io.vertx.rxjava3.SingleHelper.nullObserver());
return ret;
}
/**
* Connect to the remote server
and create a request for given method
of a hosted gRPC service.
* @param server the server hosting the service
* @param service the service to be called
* @return a future request
*/
public io.reactivex.rxjava3.core.Single> rxRequest(io.vertx.rxjava3.core.net.SocketAddress server, io.grpc.MethodDescriptor service) {
return AsyncResultSingle.toSingle(delegate.request(server.getDelegate(), service), __value -> io.vertx.rxjava3.grpc.client.GrpcClientRequest.newInstance((io.vertx.grpc.client.GrpcClientRequest)__value, TypeArg.unknown(), TypeArg.unknown()));
}
/**
* Call the service
gRPC service hosted by server
.
*
* The requestHandler
is called to send the request, e.g. req -> req.send(item)
*
* The responseFunction
extracts the result, e.g. resp -> resp.last()
*
* It can be used in various ways:
*
* Future fut = client.call(..., req -> req.send(item), resp -> resp.last());
* Future fut = client.call(..., req -> req.send(stream), resp -> resp.pipeTo(anotherService));
* Future> fut = client.call(..., req -> req.send(stream), resp -> resp.collecting(Collectors.toList()));
*
*
* @param server the server hosting the service
* @param service the service to call
* @param requestHandler the handler called to send the request
* @param resultFn the function applied to extract the result.
* @return a future of the result
*/
public io.reactivex.rxjava3.core.Single call(io.vertx.rxjava3.core.net.SocketAddress server, io.grpc.MethodDescriptor service, io.vertx.core.Handler> requestHandler, java.util.function.Function,io.reactivex.rxjava3.core.Single> resultFn) {
io.reactivex.rxjava3.core.Single ret = rxCall(server, service, requestHandler, resultFn);
ret = ret.cache();
ret.subscribe(io.vertx.rxjava3.SingleHelper.nullObserver());
return ret;
}
/**
* Call the service
gRPC service hosted by server
.
*
* The requestHandler
is called to send the request, e.g. req -> req.send(item)
*
* The responseFunction
extracts the result, e.g. resp -> resp.last()
*
* It can be used in various ways:
*
* Future fut = client.call(..., req -> req.send(item), resp -> resp.last());
* Future fut = client.call(..., req -> req.send(stream), resp -> resp.pipeTo(anotherService));
* Future> fut = client.call(..., req -> req.send(stream), resp -> resp.collecting(Collectors.toList()));
*
*
* @param server the server hosting the service
* @param service the service to call
* @param requestHandler the handler called to send the request
* @param resultFn the function applied to extract the result.
* @return a future of the result
*/
public io.reactivex.rxjava3.core.Single rxCall(io.vertx.rxjava3.core.net.SocketAddress server, io.grpc.MethodDescriptor service, io.vertx.core.Handler> requestHandler, java.util.function.Function,io.reactivex.rxjava3.core.Single> resultFn) {
return AsyncResultSingle.toSingle(delegate.call(server.getDelegate(), service, new io.vertx.lang.rx.DelegatingHandler<>(requestHandler, event -> io.vertx.rxjava3.grpc.client.GrpcClientRequest.newInstance((io.vertx.grpc.client.GrpcClientRequest)event, TypeArg.unknown(), TypeArg.unknown())), new Function,io.vertx.core.Future>() {
public io.vertx.core.Future apply(io.vertx.grpc.client.GrpcClientResponse arg) {
io.reactivex.rxjava3.core.Single ret = resultFn.apply(io.vertx.rxjava3.grpc.client.GrpcClientResponse.newInstance((io.vertx.grpc.client.GrpcClientResponse)arg, TypeArg.unknown(), TypeArg.unknown()));
return io.vertx.rxjava3.SingleHelper.toFuture(ret, obj -> obj);
}
}), __value -> (T) __value);
}
public static GrpcClient newInstance(io.vertx.grpc.client.GrpcClient arg) {
return arg != null ? new GrpcClient(arg) : null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy