forked from TrueCloudLab/frostfs-sdk-java
26 lines
935 B
Java
26 lines
935 B
Java
package info.frostfs.sdk.tools;
|
|
|
|
import io.grpc.Channel;
|
|
import io.grpc.ChannelCredentials;
|
|
import io.grpc.netty.NettyChannelBuilder;
|
|
|
|
import java.net.URI;
|
|
import java.net.URISyntaxException;
|
|
|
|
import static java.util.Objects.isNull;
|
|
|
|
public class GrpcClient {
|
|
private static final String ERROR_INVALID_HOST_TEMPLATE = "Host %s has invalid format. Error: %s";
|
|
|
|
public static Channel initGrpcChannel(String host, ChannelCredentials creds) {
|
|
try {
|
|
URI uri = new URI(host);
|
|
var channelBuilder = isNull(creds) ? NettyChannelBuilder.forAddress(uri.getHost(), uri.getPort())
|
|
: NettyChannelBuilder.forAddress(uri.getHost(), uri.getPort(), creds);
|
|
|
|
return channelBuilder.usePlaintext().build();
|
|
} catch (URISyntaxException exp) {
|
|
throw new IllegalArgumentException(String.format(ERROR_INVALID_HOST_TEMPLATE, host, exp.getMessage()));
|
|
}
|
|
}
|
|
}
|