frostfs-sdk-java/client/src/main/java/info/frostfs/sdk/tools/GrpcClient.java
Bruk Ori bf2f19f08d [#1] provide naming conventions
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
2024-07-24 15:49:12 +03:00

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()));
}
}
}