Skip to content

Commit a4b1b45

Browse files
committed
feature: add NewCustomized() so that client works with etherscan-family API like BscScan
Thanks for Kevin J. Qiu(nanmu42#23) for the idea.
1 parent 3ddd9ca commit a4b1b45

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

client.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
// Clients are safe for concurrent use by multiple goroutines.
2323
type Client struct {
2424
coon *http.Client
25-
network Network
2625
key string
2726
baseURL string
2827

@@ -41,13 +40,45 @@ type Client struct {
4140
// New initialize a new etherscan API client
4241
// please use pre-defined network value
4342
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 {
4473
return &Client{
4574
coon: &http.Client{
46-
Timeout: 30 * time.Second,
75+
Timeout: config.Timeout,
4776
},
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,
5182
}
5283
}
5384

0 commit comments

Comments
 (0)