forked from fbsobreira/gotron-sdk
/
client.go
113 lines (98 loc) · 2.43 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package client
import (
"context"
"fmt"
"time"
"github.com/Robin-Luooo/gotron-sdk/pkg/proto/api"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
// GrpcClient controller structure
type GrpcClient struct {
Address string
Conn *grpc.ClientConn
Client api.WalletClient
grpcTimeout time.Duration
opts []grpc.DialOption
apiKey string
}
// NewGrpcClient create grpc controller
func NewGrpcClient(address string) *GrpcClient {
client := &GrpcClient{
Address: address,
grpcTimeout: 5 * time.Second,
}
return client
}
// NewGrpcClientWithTimeout create grpc controller
func NewGrpcClientWithTimeout(address string, timeout time.Duration) *GrpcClient {
client := &GrpcClient{
Address: address,
grpcTimeout: timeout,
}
return client
}
// SetTimeout for Client connections
func (g *GrpcClient) SetTimeout(timeout time.Duration) {
g.grpcTimeout = timeout
}
// Start initiate grpc connection
func (g *GrpcClient) Start(opts ...grpc.DialOption) error {
var err error
if len(g.Address) == 0 {
g.Address = "grpc.trongrid.io:50051"
}
g.opts = opts
g.Conn, err = grpc.Dial(g.Address, opts...)
if err != nil {
return fmt.Errorf("Connecting GRPC Client: %v", err)
}
g.Client = api.NewWalletClient(g.Conn)
return nil
}
// SetAPIKey enable API on connection
func (g *GrpcClient) SetAPIKey(apiKey string) error {
g.apiKey = apiKey
return nil
}
func (g *GrpcClient) getContext() (context.Context, context.CancelFunc) {
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
if len(g.apiKey) > 0 {
ctx = metadata.AppendToOutgoingContext(ctx, "TRON-PRO-API-KEY", g.apiKey)
}
return ctx, cancel
}
// Stop GRPC Connection
func (g *GrpcClient) Stop() {
if g.Conn != nil {
g.Conn.Close()
}
}
// Reconnect GRPC
func (g *GrpcClient) Reconnect(url string) error {
g.Stop()
if len(url) > 0 {
g.Address = url
}
g.Start(g.opts...)
return nil
}
// GetMessageBytes return grpc message from bytes
func GetMessageBytes(m []byte) *api.BytesMessage {
message := new(api.BytesMessage)
message.Value = m
return message
}
// GetMessageNumber return grpc message number
func GetMessageNumber(n int64) *api.NumberMessage {
message := new(api.NumberMessage)
message.Num = n
return message
}
// GetPaginatedMessage return grpc message number
func GetPaginatedMessage(offset int64, limit int64) *api.PaginatedMessage {
return &api.PaginatedMessage{
Offset: offset,
Limit: limit,
}
}