github.com/aergoio/aergo@v1.3.1/cmd/aergocli/util/grpccommon.go (about) 1 /** 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package util 7 8 import ( 9 "fmt" 10 11 "github.com/aergoio/aergo/cmd/aergocli/util/encoding/json" 12 "github.com/aergoio/aergo/types" 13 protobuf "github.com/golang/protobuf/proto" 14 "google.golang.org/grpc" 15 ) 16 17 type ConnClient struct { 18 types.AergoRPCServiceClient 19 conn *grpc.ClientConn 20 } 21 22 func GetClient(serverAddr string, opts []grpc.DialOption) interface{} { 23 conn, err := grpc.Dial(serverAddr, opts...) 24 if err != nil || conn == nil { 25 fmt.Println(err) 26 panic("connection failed") 27 } 28 29 connClient := &ConnClient{ 30 AergoRPCServiceClient: types.NewAergoRPCServiceClient(conn), 31 conn: conn, 32 } 33 34 return connClient 35 } 36 37 func (c *ConnClient) Close() { 38 c.conn.Close() 39 c.conn = nil 40 } 41 42 // JSON converts protobuf message(struct) to json notation 43 func JSON(pb protobuf.Message) string { 44 jsonout, err := json.MarshalIndent(pb, "", " ") 45 if err != nil { 46 fmt.Printf("Failed: %s\n", err.Error()) 47 return "" 48 } 49 return string(jsonout) 50 } 51 52 func B58JSON(i interface{}) string { 53 jsonout, err := json.MarshalIndent(i, "", " ") 54 if err != nil { 55 fmt.Printf("Failed: %s\n", err.Error()) 56 return "" 57 } 58 return string(jsonout) 59 }