-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
67 lines (57 loc) · 1.17 KB
/
api.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
package trongrid
import (
"context"
"time"
"github.com/go-resty/resty/v2"
"github.com/gorilla/schema"
"github.com/rs/zerolog"
"golang.org/x/time/rate"
)
type API interface {
// ListTransactions
// Docs: https://developers.tron.network/reference/get-trc20-transaction-info-by-account-address
ListTransactions(
ctx context.Context,
req *ListTransactionsRequest,
) (resp *ListTransactionsResponse, err error)
}
type api struct {
encoder *schema.Encoder
decoder *schema.Decoder
logger *zerolog.Logger
cl *resty.Client
token string
uri string
debug bool
}
func NewAPI(opts ...Option) API {
x := &api{
encoder: NewEncoder(),
decoder: NewDecoder(),
logger: nil,
cl: nil,
token: "",
uri: "",
debug: false,
}
for _, opt := range opts {
opt(x)
}
if len(x.uri) == 0 {
x.uri = URI
}
cl := resty.New().
SetBaseURL(x.uri).
SetDebug(x.debug).
SetRateLimiter(rate.NewLimiter(rate.Every(time.Second), 1)).
SetRedirectPolicy(resty.NoRedirectPolicy()).
SetTimeout(timeout)
if x.logger != nil {
cl.SetLogger(NewLogger(x.logger))
}
if len(x.token) != 0 {
cl.SetHeader("TRON-PRO-API-KEY", x.token)
}
x.cl = cl
return x
}