@@ -22,7 +22,6 @@ import (
22
22
// Clients are safe for concurrent use by multiple goroutines.
23
23
type Client struct {
24
24
coon * http.Client
25
- network Network
26
25
key string
27
26
baseURL string
28
27
@@ -41,13 +40,45 @@ type Client struct {
41
40
// New initialize a new etherscan API client
42
41
// please use pre-defined network value
43
42
func New (network Network , APIKey string ) * Client {
43
+ return NewCustomized (Customization {
44
+ Timeout : 30 * time .Second ,
45
+ Key : APIKey ,
46
+ BaseURL : fmt .Sprintf (`https://%s.etherscan.io/api?` , network .SubDomain ()),
47
+ })
48
+ }
49
+
50
+ // Customization is used in NewCustomized()
51
+ type Customization struct {
52
+ // Timeout for API call
53
+ Timeout time.Duration
54
+ // API key applied from Etherscan
55
+ Key string
56
+ // Base URL like `https://api.etherscan.io/api?`
57
+ BaseURL string
58
+ // When true, talks a lot
59
+ Verbose bool
60
+
61
+ // BeforeRequest runs before every client request, in the same goroutine.
62
+ // May be used in rate limit.
63
+ // Request will be aborted, if BeforeRequest returns non-nil err.
64
+ BeforeRequest func (module , action string , param map [string ]interface {}) error
65
+
66
+ // AfterRequest runs after every client request, even when there is an error.
67
+ AfterRequest func (module , action string , param map [string ]interface {}, outcome interface {}, requestErr error )
68
+ }
69
+
70
+ // NewCustomized initialize a customized API client,
71
+ // useful when calling against etherscan-family API like BscScan.
72
+ func NewCustomized (config Customization ) * Client {
44
73
return & Client {
45
74
coon : & http.Client {
46
- Timeout : 30 * time . Second ,
75
+ Timeout : config . Timeout ,
47
76
},
48
- network : network ,
49
- key : APIKey ,
50
- baseURL : fmt .Sprintf (`https://%s.etherscan.io/api?` , network .SubDomain ()),
77
+ key : config .Key ,
78
+ baseURL : config .BaseURL ,
79
+ Verbose : config .Verbose ,
80
+ BeforeRequest : config .BeforeRequest ,
81
+ AfterRequest : config .AfterRequest ,
51
82
}
52
83
}
53
84
0 commit comments