-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
152 lines (129 loc) · 3.38 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
/*
* Copyright (c) 2019. Dabank Authors
* All rights reserved.
*/
package nuls
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"time"
)
const (
rootPath = "/api"
)
// Client nuls API client
// Clients are safe for concurrent use by multiple goroutines.
type Client struct {
coon *http.Client
baseURL string
// Verbose when true, talks a lot
Verbose bool
// BeforeRequest fires before every request,
// can be used in rate limit.
// request is aborted if there is an error
BeforeRequest func(requestType string, option string, outcome interface{}) error
}
// New initializes a new nuls API client
func New(baseURL string) *Client {
return &Client{
coon: &http.Client{
Timeout: time.Second * 30,
},
baseURL: baseURL,
}
}
// call does almost all the dirty work.
func (c *Client) call(requestMethod, requestType, option string, param map[string]interface{}, outcome interface{}) (err error) {
// fire hooks if in need
if c.BeforeRequest != nil {
err = c.BeforeRequest(requestType, option, outcome)
if err != nil {
err = wrapErr(err, "BeforeRequest error")
return
}
}
// 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)
}
}()
URL, body := c.craftRequest(requestMethod, requestType, option, param)
req, err := http.NewRequest(requestMethod, URL, body)
if err != nil {
err = wrapErr(err, "http.NewRequest")
return
}
req.Header.Set("User-Agent", "nuls-api-client(Go)")
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
if c.Verbose {
var reqDump []byte
reqDump, err = httputil.DumpRequestOut(req, true)
if err != nil {
err = wrapErr(err, "verbose mode req dump failed")
return
}
fmt.Printf("\n%s\n\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 errRes ErrorResponse
err = json.Unmarshal(content.Bytes(), &errRes)
if err != nil {
err = wrapErr(err, "json unmarshal errRes")
return
}
if !errRes.Success && (errRes.Data.Code != "" || errRes.Data.Msg != "") {
err = fmt.Errorf("nuls server: [err code %v] %s", errRes.Data.Code, errRes.Data.Msg)
return
}
err = json.Unmarshal(content.Bytes(), outcome)
if err != nil {
err = wrapErr(err, "json unmarshal outcome")
return
}
return
}
// craftRequest returns desired URL via param provided
func (c *Client) craftRequest(requestMethod, requestType, option string, param map[string]interface{}) (URL string, body io.Reader) {
URL = c.baseURL + requestType + option
if len(param) == 0 {
return
}
if requestMethod == http.MethodPost {
jsonData, _ := json.Marshal(param)
body = bytes.NewReader(jsonData)
}
return
}