forked from bitmartexchange/bitmart-go-sdk-api
/
cloud_client.go
161 lines (135 loc) · 3.74 KB
/
cloud_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
153
154
155
156
157
158
159
160
161
package bitmart
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
type CloudClient struct {
Config Config
HttpClient *http.Client
}
type ApiMessage struct {
Code int `json:"code"`
Message string `json:"message"`
}
/*
Get a http client
*/
func NewClient(config Config) *CloudClient {
var client CloudClient
client.Config = config
timeout := config.TimeoutSecond
if timeout <= 0 {
timeout = 30
}
client.HttpClient = &http.Client{
Timeout: time.Duration(timeout) * time.Second,
}
return &client
}
/*
Send a GET http request without params
*/
func (cloudClient *CloudClient) requestWithoutParams(method string, path string, auth Auth) (*CloudResponse, error) {
var cloudResponse CloudResponse
if _, err := cloudClient.Request(method, path, nil, auth, &cloudResponse); err != nil {
return nil, err
}
return &cloudResponse, nil
}
/*
Send a GET http request with params
*/
func (cloudClient *CloudClient) requestWithParams(method string, path string, params map[string]interface{}, auth Auth) (*CloudResponse, error) {
var cloudResponse CloudResponse
if _, err := cloudClient.Request(method, path, params, auth, &cloudResponse); err != nil {
return nil, err
}
return &cloudResponse, nil
}
/*
Send a http request to remote server and get a response data
*/
func (cloudClient *CloudClient) Request(method string, requestPath string, params map[string]interface{}, auth Auth, cloudResponse *CloudResponse) (response *http.Response, err error) {
config := cloudClient.Config
// uri
endpoint := config.Url
if strings.HasSuffix(config.Url, "/") {
endpoint = config.Url[0 : len(config.Url)-1]
}
url := endpoint + requestPath
// set queryString
var binBody = bytes.NewReader(make([]byte, 0))
var jsonBody string
if GET == method || DELETE == method {
url = url + CreateQueryString(params)
} else {
// get json and bin styles request body
if params != nil {
jsonBody, binBody, err = ParseRequestParams(params)
if err != nil {
return response, err
}
}
}
// get a http request
request, err := http.NewRequest(method, url, binBody)
if err != nil {
return response, err
}
// set header
if auth == NONE {
Headers(request, "", "", "")
} else if auth == KEYED {
Headers(request, config.ApiKey, "", "")
} else if auth == SIGNED {
timestamp := UTCTime()
sign, err := HmacSha256Base64Signer(
PreHashString(timestamp, config.Memo, jsonBody), config.SecretKey)
if err != nil {
return response, err
}
Headers(request, config.ApiKey, timestamp, sign)
}
if config.IsPrint {
fmt.Println("---------------------------------------------")
PrintRequest(request, jsonBody)
}
// send a request to remote server, and get a response
response, err = cloudClient.HttpClient.Do(request)
if err != nil {
return response, err
}
defer response.Body.Close()
// get a response results and parse
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return response, err
}
cloudResponse.httpStatus = response.StatusCode
cloudResponse.response = string(body)
cloudResponse.limit.limit = StringToInt(response.Header.Get("X-BM-RateLimit-Limit"))
cloudResponse.limit.remaining = StringToInt(response.Header.Get("X-BM-RateLimit-Remaining"))
cloudResponse.limit.reset = StringToInt(response.Header.Get("X-BM-RateLimit-Reset"))
return response, nil
}
/*
Get a http request body is a json string and a byte array.
*/
func ParseRequestParams(params interface{}) (string, *bytes.Reader, error) {
if params == nil {
return "", nil, errors.New("illegal parameter")
}
data, err := json.Marshal(params)
if err != nil {
return "", nil, errors.New("json convert string error")
}
jsonBody := string(data)
binBody := bytes.NewReader(data)
return jsonBody, binBody, nil
}