|
7 | 7 |
|
8 | 8 | package etherscan
|
9 | 9 |
|
10 |
| -import "net/http" |
| 10 | +import ( |
| 11 | + "bytes" |
| 12 | + "encoding/json" |
| 13 | + "fmt" |
| 14 | + "io" |
| 15 | + "net/http" |
| 16 | + "net/url" |
| 17 | +) |
11 | 18 |
|
12 | 19 | // Client etherscan API client
|
13 | 20 | // Clients are safe for concurrent use by multiple goroutines.
|
14 | 21 | type Client struct {
|
15 |
| - network Network |
16 | 22 | coon *http.Client
|
| 23 | + network Network |
| 24 | + key string |
| 25 | + baseURL string |
| 26 | + |
| 27 | + // Verbose when true, talks a lot |
| 28 | + Verbose bool |
| 29 | + |
| 30 | + // BeforeRequest runs before every client request, in the same goroutine. |
| 31 | + // May be used in rate limit. |
| 32 | + // Request will be aborted, if BeforeRequest returns non-nil err. |
| 33 | + BeforeRequest func(module, action string, param map[string]interface{}) error |
| 34 | + |
| 35 | + // AfterRequest runs after every client request, even when there is an error. |
| 36 | + AfterRequest func(module, action string, param map[string]interface{}, outcome interface{}, requestErr error) |
17 | 37 | }
|
18 | 38 |
|
19 | 39 | // New initialize a new etherscan API client
|
20 | 40 | // please use pre-defined network value
|
21 |
| -func New(network Network) *Client { |
| 41 | +func New(network Network, APIKey string) *Client { |
22 | 42 | return &Client{
|
23 |
| - network: network, |
24 | 43 | coon: &http.Client{},
|
| 44 | + network: network, |
| 45 | + key: APIKey, |
| 46 | + baseURL: fmt.Sprintf(`https://%s.etherscan.io/api?`, network.SubDomain()), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// call does almost all the dirty work. |
| 51 | +func (c *Client) call(module, action string, param map[string]interface{}, outcome interface{}) (err error) { |
| 52 | + // todo: fire hooks |
| 53 | + // todo: verbose mode |
| 54 | + |
| 55 | + req, err := http.NewRequest(http.MethodGet, c.craftURL(param), http.NoBody) |
| 56 | + if err != nil { |
| 57 | + err = wrapErr(err, "http.NewRequest") |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + res, err := c.coon.Do(req) |
| 62 | + if err != nil { |
| 63 | + err = wrapErr(err, "sending request") |
| 64 | + return |
25 | 65 | }
|
| 66 | + defer res.Body.Close() |
| 67 | + |
| 68 | + var content bytes.Buffer |
| 69 | + if _, err = io.Copy(&content, res.Body); err != nil { |
| 70 | + err = wrapErr(err, "reading response") |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + if res.StatusCode != http.StatusOK { |
| 75 | + err = fmt.Errorf("response status %v %s, response body: %s", res.StatusCode, res.Status, content.String()) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + err = json.Unmarshal(content.Bytes(), outcome) |
| 80 | + if err != nil { |
| 81 | + err = wrapErr(err, "json unmarshal") |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + return |
| 86 | +} |
| 87 | + |
| 88 | +// craftURL returns desired URL via param provided |
| 89 | +func (c *Client) craftURL(param map[string]interface{}) (URL string) { |
| 90 | + q := url.Values{ |
| 91 | + "apikey": []string{c.key}, |
| 92 | + } |
| 93 | + |
| 94 | + for k, v := range param { |
| 95 | + q[k] = extractValue(v) |
| 96 | + } |
| 97 | + |
| 98 | + URL = c.baseURL + q.Encode() |
| 99 | + return |
26 | 100 | }
|
0 commit comments