-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
376 lines (342 loc) · 8.95 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package okex
/*
utils
@author Tony Tian
@date 2018-03-17
@version 1.0.0
*/
import (
"bytes"
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
/*
signing a message
using: hmac sha256 + base64
eg:
message = Pre_hash function comment
secretKey = E65791902180E9EF4510DB6A77F6EBAE
return signed string = TO6uwdqz+31SIPkd4I+9NiZGmVH74dXi+Fd5X0EzzSQ=
*/
func HmacSha256Base64Signer(message string, secretKey string) (string, error) {
mac := hmac.New(sha256.New, []byte(secretKey))
_, err := mac.Write([]byte(message))
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(mac.Sum(nil)), nil
}
/*
the pre hash string
eg:
timestamp = 2018-03-08T10:59:25.789Z
method = POST
request_path = /orders?before=2&limit=30
body = {"product_id":"BTC-USD-0309","order_id":"377454671037440"}
return pre hash string = 2018-03-08T10:59:25.789ZPOST/orders?before=2&limit=30{"product_id":"BTC-USD-0309","order_id":"377454671037440"}
*/
func PreHashString(timestamp string, method string, requestPath string, body string) string {
return timestamp + strings.ToUpper(method) + requestPath + body
}
/*
md5 sign
*/
func Md5Signer(message string) string {
data := []byte(message)
has := md5.Sum(data)
return fmt.Sprintf("%x", has)
}
/*
int convert string
*/
func Int2String(arg int) string {
return strconv.Itoa(arg)
}
/*
int64 convert string
*/
func Int642String(arg int64) string {
return strconv.FormatInt(int64(arg), 10)
}
/*
json string convert struct
*/
func JsonString2Struct(jsonString string, result interface{}) error {
jsonBytes := []byte(jsonString)
err := json.Unmarshal(jsonBytes, result)
return err
}
/*
json byte array convert struct
*/
func JsonBytes2Struct(jsonBytes []byte, result interface{}) error {
err := json.Unmarshal(jsonBytes, result)
return err
}
/*
struct convert json string
*/
func Struct2JsonString(structt interface{}) (jsonString string, err error) {
data, err := json.Marshal(structt)
if err != nil {
return "", err
}
return string(data), nil
}
/*
ternary operator replace language: a == b ? c : d
*/
func T3O(condition bool, trueValue, falseValue interface{}) interface{} {
if condition {
return trueValue
}
return falseValue
}
/*
Get a epoch time
eg: 1521221737.376
*/
func EpochTime() string {
millisecond := time.Now().UnixNano() / 1000000
epoch := strconv.Itoa(int(millisecond))
epochBytes := []byte(epoch)
epoch = string(epochBytes[:10]) + "." + string(epochBytes[10:])
return epoch
}
/*
Get a iso time
eg: 2018-03-16T18:02:48.284Z
*/
func IsoTime() string {
utcTime := time.Now().UTC()
iso := utcTime.String()
isoBytes := []byte(iso)
iso = string(isoBytes[:10]) + "T" + string(isoBytes[11:23]) + "Z"
return iso
}
/*
Get utc +8 -- 1540365300000 -> 2018-10-24 15:15:00 +0800 CST
*/
func LongTimeToUTC8(longTime int64) time.Time {
timeString := Int64ToString(longTime)
sec := timeString[0:10]
nsec := timeString[10:len(timeString)]
return time.Unix(StringToInt64(sec), StringToInt64(nsec))
}
/*
1540365300000 -> 2018-10-24 15:15:00
*/
func LongTimeToUTC8Format(longTime int64) string {
return LongTimeToUTC8(longTime).Format("2006-01-02 15:04:05")
}
/*
iso time change to time.Time
eg: "2018-11-18T16:51:55.933Z" -> 2018-11-18 16:51:55.000000933 +0000 UTC
*/
func IsoToTime(iso string) (time.Time, error) {
nilTime := time.Now()
if iso == "" {
return nilTime, errors.New("illegal parameter")
}
// "2018-03-18T06:51:05.933Z"
isoBytes := []byte(iso)
year, err := strconv.Atoi(string(isoBytes[0:4]))
if err != nil {
return nilTime, errors.New("illegal year")
}
month, err := strconv.Atoi(string(isoBytes[5:7]))
if err != nil {
return nilTime, errors.New("illegal month")
}
day, err := strconv.Atoi(string(isoBytes[8:10]))
if err != nil {
return nilTime, errors.New("illegal day")
}
hour, err := strconv.Atoi(string(isoBytes[11:13]))
if err != nil {
return nilTime, errors.New("illegal hour")
}
min, err := strconv.Atoi(string(isoBytes[14:16]))
if err != nil {
return nilTime, errors.New("illegal min")
}
sec, err := strconv.Atoi(string(isoBytes[17:19]))
if err != nil {
return nilTime, errors.New("illegal sec")
}
nsec, err := strconv.Atoi(string(isoBytes[20 : len(isoBytes)-1]))
if err != nil {
return nilTime, errors.New("illegal nsec")
}
return time.Date(year, time.Month(month), day, hour, min, sec, nsec, time.UTC), 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
}
/*
Set http request headers:
Accept: application/json
Content-Type: application/json; charset=UTF-8 (default)
Cookie: locale=en_US (English)
OK-ACCESS-KEY: (Your setting)
OK-ACCESS-SIGN: (Use your setting, auto sign and add)
OK-ACCESS-TIMESTAMP: (Auto add)
OK-ACCESS-PASSPHRASE: Your setting
*/
func Headers(request *http.Request, config Config, timestamp string, sign string) {
request.Header.Add(ACCEPT, APPLICATION_JSON)
request.Header.Add(CONTENT_TYPE, APPLICATION_JSON_UTF8)
request.Header.Add(COOKIE, LOCALE+config.I18n)
request.Header.Add(OK_ACCESS_KEY, config.ApiKey)
request.Header.Add(OK_ACCESS_SIGN, sign)
request.Header.Add(OK_ACCESS_TIMESTAMP, timestamp)
request.Header.Add(OK_ACCESS_PASSPHRASE, config.Passphrase)
}
/*
Get a new map.eg: {string:string}
*/
func NewParams() map[string]string {
return make(map[string]string)
}
/*
build http get request params, and order
eg:
params := make(map[string]string)
params["bb"] = "222"
params["aa"] = "111"
params["cc"] = "333"
return string: eg: aa=111&bb=222&cc=333
*/
func BuildOrderParams(params map[string]string) string {
var keys []string
for k := range params {
keys = append(keys, k)
}
sort.Strings(keys)
urlParams := url.Values{}
for k := range params {
urlParams.Add(k, params[k])
}
return urlParams.Encode()
}
/*
Get api requestPath + requestParams
params := NewParams()
params["depth"] = "200"
params["conflated"] = "0"
url := BuildParams("/api/futures/v3/products/BTC-USD-0310/book", params)
return eg:/api/futures/v3/products/BTC-USD-0310/book?conflated=0&depth=200
*/
func BuildParams(requestPath string, params map[string]string) string {
urlParams := url.Values{}
for k := range params {
urlParams.Add(k, params[k])
}
return requestPath + "?" + urlParams.Encode()
}
/*
Get api v1 requestPath + requestParams
params := okex.NewParams()
params["symbol"] = "btc_usd"
params["contract_type"] = "this_week"
params["status"] = "1"
requestPath := "/api/v1/future_explosive.do"
return eg: /api/v1/future_explosive.do?api_key=88af5759-61f2-47e9-b2e9-17ce3a390488&contract_type=this_week&status=1&symbol=btc_usd&sign=966ACD0DE5F729BC9C9C03D92ABBEB68
*/
func BuildAPIV1Params(requestPath string, params map[string]string, config Config) string {
params["api_key"] = config.ApiKey
sortParams := BuildOrderParams(params)
preMd5Params := sortParams + "&secret_key=" + config.SecretKey
md5Sign := Md5Signer(preMd5Params)
requestParams := sortParams + "&sign=" + strings.ToUpper(md5Sign)
return requestPath + "?" + requestParams
}
func GetResponseDataJsonString(response *http.Response) string {
return response.Header.Get(ResultDataJsonString)
}
func GetResponsePageJsonString(response *http.Response) string {
return response.Header.Get(ResultPageJsonString)
}
/*
ternary operator biz extension
*/
func T3Ox(err error, value interface{}) (interface{}, error) {
if err != nil {
return nil, err
}
return value, nil
}
/*
return decimalism string 9223372036854775807 -> "9223372036854775807"
*/
func Int64ToString(arg int64) string {
return strconv.FormatInt(arg, 10)
}
func IntToString(arg int) string {
return strconv.Itoa(arg)
}
func StringToInt64(arg string) int64 {
value, err := strconv.ParseInt(arg, 10, 64)
if err != nil {
return 0
} else {
return value
}
}
func StringToInt(arg string) int {
value, err := strconv.Atoi(arg)
if err != nil {
return 0
} else {
return value
}
}
/*
call fmt.Println(...)
*/
func FmtPrintln(flag string, info interface{}) {
fmt.Print(flag)
if info != nil {
jsonString, err := Struct2JsonString(info)
if err != nil {
fmt.Println(err)
}
fmt.Println(jsonString)
} else {
fmt.Println("{}")
}
}
func GetInstrumentIdUri(uri, instrumentId string) string {
return strings.Replace(uri, "{instrument_id}", instrumentId, -1)
}
func GetCurrencyUri(uri, currency string) string {
return strings.Replace(uri, "{currency}", currency, -1)
}
func GetInstrumentIdOrdersUri(uri, instrumentId string, orderId int64) string {
uri = strings.Replace(uri, "{instrument_id}", instrumentId, -1)
uri = strings.Replace(uri, "{order_id}", Int64ToString(orderId), -1)
return uri
}