generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
request.go
258 lines (223 loc) · 6.7 KB
/
request.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package nownodes
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
)
// RequestResponse is the response from a request
type RequestResponse struct {
BodyContents []byte `json:"body_contents"` // Raw body response
Error error `json:"error"` // If an error occurs
Method string `json:"method"` // Method is the HTTP method used
PostData string `json:"post_data"` // PostData is the post data submitted if POST/PUT request
StatusCode int `json:"status_code"` // StatusCode is the last code from the request
URL string `json:"url"` // URL is used for the request
}
// httpPayload is used for a httpRequest
type httpPayload struct {
APIKey string `json:"api_key"`
Data []byte `json:"data"`
Method string `json:"method"`
URL string `json:"url"`
}
// httpRequest is a generic request wrapper that can be used without constraints
func httpRequest(ctx context.Context, client *Client,
payload *httpPayload) (response *RequestResponse) {
// Set reader & response
var bodyReader io.Reader
response = new(RequestResponse)
// Add post data if applicable
if payload.Method == http.MethodPost {
bodyReader = bytes.NewBuffer(payload.Data)
response.PostData = string(payload.Data)
}
// Store for debugging purposes
response.Method = payload.Method
response.URL = payload.URL
// Start the request
var request *http.Request
if request, response.Error = http.NewRequestWithContext(
ctx, payload.Method, payload.URL, bodyReader,
); response.Error != nil {
return
}
// Change the header (user agent is in case they block default Go user agents)
request.Header.Set("User-Agent", client.options.userAgent)
// Set the content type on Method
if payload.Method == http.MethodPost {
request.Header.Set("Content-Type", "application/json")
}
// Set a token if supplied
if len(payload.APIKey) > 0 {
request.Header.Set(apiHeaderKey, payload.APIKey)
}
// Fire the http request
var resp *http.Response
if resp, response.Error = client.options.httpClient.Do(request); response.Error != nil {
if resp != nil {
response.StatusCode = resp.StatusCode
}
return
}
// Close the response body
defer func() {
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
}()
// Set the status
response.StatusCode = resp.StatusCode
// Read the body
if resp.Body != nil {
response.BodyContents, response.Error = ioutil.ReadAll(resp.Body)
}
// Check status code
if http.StatusOK == resp.StatusCode {
// Detect error message (it's not an error, and returns a 200)
if strings.Contains(string(response.BodyContents), `{"message":`) {
errBody := struct {
Error string `json:"message"`
}{}
if err := json.Unmarshal(
response.BodyContents, &errBody,
); err != nil {
response.Error = fmt.Errorf("failed to unmarshal error response: %w", err)
return
}
response.Error = errors.New(errBody.Error)
}
return
}
// There's no "body" present, so just echo status code.
if response.BodyContents == nil {
response.Error = fmt.Errorf(
"status code: %d does not match %d",
resp.StatusCode, http.StatusOK,
)
return
}
// Have a "body" so map to an error type and add to the error message.
if payload.Method == http.MethodGet {
errBody := struct {
Error string `json:"error"`
}{}
if err := json.Unmarshal(
response.BodyContents, &errBody,
); err != nil {
response.Error = fmt.Errorf("failed to unmarshal error response: %w", err)
return
}
response.Error = errors.New(errBody.Error)
} else {
errBody := new(NodeError)
if err := json.Unmarshal(
response.BodyContents, &errBody,
); err != nil {
response.Error = fmt.Errorf("failed to unmarshal error response: %w", err)
return
}
response.Error = fmt.Errorf(
"code [%d] error [%s]", errBody.Error.Code, errBody.Error.Message,
)
}
return
}
// blockBookRequest will make a BlockBook request and imbue the results into the given model
func blockBookRequest(ctx context.Context, client *Client, chains []Blockchain,
chain Blockchain, endpoint string, model interface{}) error {
resp, err := blockBookRequestInternal(
ctx, client, chains, chain, endpoint,
)
if err != nil {
return err
}
// Unmarshal the response
return json.Unmarshal(
resp.BodyContents, &model,
)
}
// blockBookRequestInternal will make a BlockBook request and return the result
func blockBookRequestInternal(ctx context.Context, client *Client, chains []Blockchain,
chain Blockchain, endpoint string) (*RequestResponse, error) {
// Are we using a supported blockchain?
if !isBlockchainSupported(chains, chain) {
return nil, ErrUnsupportedBlockchain
}
// Fire the HTTP request
resp := httpRequest(ctx, client, &httpPayload{
APIKey: client.options.apiKey,
Method: http.MethodGet,
URL: httpProtocol + chain.BlockBookURL() + "/api/" + apiVersion + endpoint,
})
if resp.Error != nil {
return nil, resp.Error
}
return resp, nil
}
/*
// blockBookRequestWithNoResponse will make a BlockBook request and only return an error if it fails
func blockBookRequestWithNoResponse(ctx context.Context, client *Client, chains []Blockchain,
chain Blockchain, endpoint string) error {
_, err := blockBookRequestInternal(
ctx, client, chains, chain, endpoint,
)
if err != nil {
return err
}
return nil
}
*/
// nodeRequest will make a NodeAPI request and return the result
func nodeRequest(ctx context.Context, client *Client, chains []Blockchain,
chain Blockchain, payload []byte, model interface{}) error {
// Are we using a supported blockchain?
if !isBlockchainSupported(chains, chain) {
return ErrUnsupportedBlockchain
}
// Fire the HTTP request
resp := httpRequest(ctx, client, &httpPayload{
APIKey: client.options.apiKey,
Data: payload,
Method: http.MethodPost,
URL: httpProtocol + chain.NodeAPIURL(),
})
if resp.Error != nil {
return resp.Error
}
// Unmarshal the response
return json.Unmarshal(
resp.BodyContents, &model,
)
}
// nodePayload is the internal raw node payload
type nodePayload struct {
APIKey string `json:"API_key"`
ID string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params []string `json:"params"`
}
// createPayload will create the JSON payload for the NodeAPI requests
func createPayload(apiKey, method, id string, params []string) []byte {
b, _ := json.Marshal(nodePayload{ //nolint:errchkjson // not going to produce an error
APIKey: apiKey,
JSONRPC: "2.0",
ID: id,
Method: method,
Params: params,
})
return b
}
// hashString will generate a hash of the given string
func hashString(data string) string {
hash := sha256.Sum256([]byte(data))
return hex.EncodeToString(hash[:])
}