-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.go
68 lines (58 loc) · 1.42 KB
/
base.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
package blocksdk_go
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
)
type Base struct {
ChainName string
ApiToken string
Endpoint string
Version string
}
func NewBase(chainName string, apiToken string, endpoint string, version string) *Base {
return &Base{
ChainName: chainName,
ApiToken: apiToken,
Endpoint: endpoint,
Version: version,
}
}
func (b *Base) Request(method string, path string, data map[string]interface{}) (map[string]interface{}, error) {
baseURL, _ := url.Parse(b.Endpoint)
baseURL.Path = b.Version + "/" + b.ChainName + path
if method == "GET" && len(data) > 0 {
params := url.Values{}
for key, value := range data {
strValue := ""
if value == true {
strValue = "true"
} else if value == false {
strValue = "false"
} else {
strValue = value.(string)
}
params.Add(key, strValue)
}
baseURL.RawQuery = params.Encode()
}
var jsonStr []byte
if method == "POST" || method == "DELETE" {
jsonStr, _ = json.Marshal(data)
}
req, _ := http.NewRequest(method, baseURL.String(), bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-token", b.ApiToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
return result, nil
}