@@ -13,6 +13,7 @@ import (
13
13
"fmt"
14
14
"io"
15
15
"net/http"
16
+ "net/http/httputil"
16
17
"net/url"
17
18
)
18
19
@@ -49,14 +50,41 @@ func New(network Network, APIKey string) *Client {
49
50
50
51
// call does almost all the dirty work.
51
52
func (c * Client ) call (module , action string , param map [string ]interface {}, outcome interface {}) (err error ) {
52
- // todo: fire hooks
53
- // todo: verbose mode
53
+ // fire hooks if in need
54
+ if c .BeforeRequest != nil {
55
+ err = c .BeforeRequest (module , action , param )
56
+ if err != nil {
57
+ err = wrapErr (err , "beforeRequest" )
58
+ return
59
+ }
60
+ }
61
+ if c .AfterRequest != nil {
62
+ defer c .AfterRequest (module , action , param , outcome , err )
63
+ }
54
64
55
- req , err := http .NewRequest (http .MethodGet , c .craftURL (param ), http .NoBody )
65
+ req , err := http .NewRequest (http .MethodGet , c .craftURL (module , action , param ), http .NoBody )
56
66
if err != nil {
57
67
err = wrapErr (err , "http.NewRequest" )
58
68
return
59
69
}
70
+ req .Header .Set ("User-Agent" , "etherscan-api(Go)" )
71
+
72
+ if c .Verbose {
73
+ var reqDump []byte
74
+ reqDump , err = httputil .DumpRequestOut (req , false )
75
+ if err != nil {
76
+ err = wrapErr (err , "verbose mode req dump failed" )
77
+ return
78
+ }
79
+
80
+ fmt .Printf ("\n %s\n " , reqDump )
81
+
82
+ defer func () {
83
+ if err != nil {
84
+ fmt .Printf ("[Error] %v\n " , err )
85
+ }
86
+ }()
87
+ }
60
88
61
89
res , err := c .coon .Do (req )
62
90
if err != nil {
@@ -65,6 +93,17 @@ func (c *Client) call(module, action string, param map[string]interface{}, outco
65
93
}
66
94
defer res .Body .Close ()
67
95
96
+ if c .Verbose {
97
+ var resDump []byte
98
+ resDump , err = httputil .DumpResponse (res , true )
99
+ if err != nil {
100
+ err = wrapErr (err , "verbose mode res dump failed" )
101
+ return
102
+ }
103
+
104
+ fmt .Printf ("%s\n " , resDump )
105
+ }
106
+
68
107
var content bytes.Buffer
69
108
if _ , err = io .Copy (& content , res .Body ); err != nil {
70
109
err = wrapErr (err , "reading response" )
@@ -76,18 +115,31 @@ func (c *Client) call(module, action string, param map[string]interface{}, outco
76
115
return
77
116
}
78
117
79
- err = json .Unmarshal (content .Bytes (), outcome )
118
+ var envelope Envelope
119
+ err = json .Unmarshal (content .Bytes (), & envelope )
120
+ if err != nil {
121
+ err = wrapErr (err , "json unmarshal envelope" )
122
+ return
123
+ }
124
+ if envelope .Status != 1 {
125
+ err = fmt .Errorf ("etherscan server: %s" , envelope .Message )
126
+ return
127
+ }
128
+
129
+ err = json .Unmarshal (envelope .Result , outcome )
80
130
if err != nil {
81
- err = wrapErr (err , "json unmarshal" )
131
+ err = wrapErr (err , "json unmarshal outcome " )
82
132
return
83
133
}
84
134
85
135
return
86
136
}
87
137
88
138
// craftURL returns desired URL via param provided
89
- func (c * Client ) craftURL (param map [string ]interface {}) (URL string ) {
139
+ func (c * Client ) craftURL (module , action string , param map [string ]interface {}) (URL string ) {
90
140
q := url.Values {
141
+ "module" : []string {module },
142
+ "action" : []string {action },
91
143
"apikey" : []string {c .key },
92
144
}
93
145
0 commit comments