0g-chain/client/grpc/query/connection.go
Draco ffd306ef52
Kava gRPC Client (#1784)
* add grpc client

* add e2e tests for grpc client

* add grpc client readme

* doc update

* fix more doc issues

* remove util namespace & move grpc client to chain

* rename GrpcClient to Grpc

* add 3rd party query clients

* fix invalid url in readme

* update e2e tests to use grpc client (#1787)
2023-12-13 12:17:37 -05:00

39 lines
933 B
Go

package query
import (
"context"
"crypto/tls"
"fmt"
"net/url"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)
// newGrpcConnection parses a GRPC endpoint and creates a connection to it
func newGrpcConnection(ctx context.Context, endpoint string) (*grpc.ClientConn, error) {
grpcUrl, err := url.Parse(endpoint)
if err != nil {
return nil, fmt.Errorf("failed to parse grpc connection \"%s\": %v", endpoint, err)
}
var creds credentials.TransportCredentials
switch grpcUrl.Scheme {
case "http":
creds = insecure.NewCredentials()
case "https":
creds = credentials.NewTLS(&tls.Config{})
default:
return nil, fmt.Errorf("unknown grpc url scheme: %s", grpcUrl.Scheme)
}
secureOpt := grpc.WithTransportCredentials(creds)
grpcConn, err := grpc.DialContext(ctx, grpcUrl.Host, secureOpt)
if err != nil {
return nil, err
}
return grpcConn, nil
}