-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
160 lines (136 loc) · 3.74 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (c) 2018 LI Zhennan
*
* Use of this work is governed by a MIT License.
* You may find a license copy in project root.
*/
package etherscan
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
)
// Client etherscan API client
// Clients are safe for concurrent use by multiple goroutines.
type Client struct {
coon *http.Client
network Network
key string
baseURL string
// Verbose when true, talks a lot
Verbose bool
// BeforeRequest runs before every client request, in the same goroutine.
// May be used in rate limit.
// Request will be aborted, if BeforeRequest returns non-nil err.
BeforeRequest func(module, action string, param map[string]interface{}) error
// AfterRequest runs after every client request, even when there is an error.
AfterRequest func(module, action string, param map[string]interface{}, outcome interface{}, requestErr error)
}
// New initialize a new etherscan API client
// please use pre-defined network value
func New(network Network, APIKey string) *Client {
return &Client{
coon: &http.Client{},
network: network,
key: APIKey,
baseURL: fmt.Sprintf(`https://%s.etherscan.io/api?`, network.SubDomain()),
}
}
// call does almost all the dirty work.
func (c *Client) call(module, action string, param map[string]interface{}, outcome interface{}) (err error) {
// fire hooks if in need
if c.BeforeRequest != nil {
err = c.BeforeRequest(module, action, param)
if err != nil {
err = wrapErr(err, "beforeRequest")
return
}
}
if c.AfterRequest != nil {
defer c.AfterRequest(module, action, param, outcome, err)
}
// recover if there shall be an panic
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("[ouch! panic recovered] please report this with what you did and what you expected, panic detail: %v", r)
}
}()
req, err := http.NewRequest(http.MethodGet, c.craftURL(module, action, param), http.NoBody)
if err != nil {
err = wrapErr(err, "http.NewRequest")
return
}
req.Header.Set("User-Agent", "etherscan-api(Go)")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
if c.Verbose {
var reqDump []byte
reqDump, err = httputil.DumpRequestOut(req, false)
if err != nil {
err = wrapErr(err, "verbose mode req dump failed")
return
}
fmt.Printf("\n%s\n", reqDump)
defer func() {
if err != nil {
fmt.Printf("[Error] %v\n", err)
}
}()
}
res, err := c.coon.Do(req)
if err != nil {
err = wrapErr(err, "sending request")
return
}
defer res.Body.Close()
if c.Verbose {
var resDump []byte
resDump, err = httputil.DumpResponse(res, true)
if err != nil {
err = wrapErr(err, "verbose mode res dump failed")
return
}
fmt.Printf("%s\n", resDump)
}
var content bytes.Buffer
if _, err = io.Copy(&content, res.Body); err != nil {
err = wrapErr(err, "reading response")
return
}
if res.StatusCode != http.StatusOK {
err = fmt.Errorf("response status %v %s, response body: %s", res.StatusCode, res.Status, content.String())
return
}
var envelope Envelope
err = json.Unmarshal(content.Bytes(), &envelope)
if err != nil {
err = wrapErr(err, "json unmarshal envelope")
return
}
if envelope.Status != 1 {
err = fmt.Errorf("etherscan server: %s", envelope.Message)
return
}
err = json.Unmarshal(envelope.Result, outcome)
if err != nil {
err = wrapErr(err, "json unmarshal outcome")
return
}
return
}
// craftURL returns desired URL via param provided
func (c *Client) craftURL(module, action string, param map[string]interface{}) (URL string) {
q := url.Values{
"module": []string{module},
"action": []string{action},
"apikey": []string{c.key},
}
for k, v := range param {
q[k] = extractValue(v)
}
URL = c.baseURL + q.Encode()
return
}