Connection
Establish the connection, either insecure (faster) or secured by passing plaintext flag.
import (
"context"
"time"
"crypto/x509"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
)
type ApiKeyCredentials struct {}
func (a ApiKeyCredentials) RequireTransportSecurity() bool {
return false
}
func (a ApiKeyCredentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{
"authorization": "<your api key here>",
}, nil
}
var kacp = keepalive.ClientParameters{
Time: time.Minute,
Timeout: 15 * time.Second,
PermitWithoutStream: true,
}
func connectToNextblock(address string, plaintext bool) *grpc.ClientConn {
var opts []grpc.DialOption
if plaintext {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
} else {
pool, _ := x509.SystemCertPool()
creds := credentials.NewClientTLSFromCert(pool, "")
opts = append(opts, grpc.WithTransportCredentials(creds))
}
opts = append(opts, grpc.WithKeepaliveParams(kacp))
opts = append(opts, grpc.WithPerRPCCredentials(&ApiKeyCredentials{}))
conn, err := grpc.NewClient(address, opts...)
if err != nil {
return nil
}
return conn
}
Pass the connection to the proto generated api client
nextblockApiClient := NewApiClient(con)
Last updated