-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
55 lines (44 loc) · 1.16 KB
/
utils.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
package walletapi
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
// makeJSONString - converts a map[string]interface to a string
func makeJSONString(dict map[string]interface{}) string {
result, err := json.Marshal(dict)
if err != nil {
panic(err)
}
return string(result)
}
// sendRequest - helper function for sending http requests to wallet-api
func (wAPI WalletAPI) sendRequest(method, uri, data string) (*map[string]interface{}, *[]byte, error) {
var rawData []byte
var body map[string]interface{}
req, err := http.NewRequest(method, uri, bytes.NewBufferString(data))
if err != nil {
return nil, nil, err
}
req.Header.Add("X-API-KEY", wAPI.APIKey)
req.Header.Add("Content-type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, nil, err
}
req.Close = true
if resp.StatusCode > 400 {
return nil, nil, errors.New(ERRORS[resp.StatusCode])
}
defer resp.Body.Close()
rawData, err = ioutil.ReadAll(resp.Body)
if err == nil {
json.Unmarshal(rawData, &body)
}
if resp.StatusCode == 400 {
return nil, nil, errors.New(body["errorMessage"].(string))
}
return &body, &rawData, err
}