forked from amir-the-h/okex
/
client.go
137 lines (130 loc) · 3.22 KB
/
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
package rest
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/Creator1024/okex"
requests "github.com/Creator1024/okex/requests/rest/public"
responses "github.com/Creator1024/okex/responses/public_data"
"net/http"
"strings"
"time"
)
// ClientRest is the rest api client
type ClientRest struct {
Account *Account
SubAccount *SubAccount
Trade *Trade
Funding *Funding
Market *Market
PublicData *PublicData
TradeData *TradeData
apiKey string
secretKey []byte
passphrase string
destination okex.Destination
baseURL okex.BaseURL
client *http.Client
}
// NewClient returns a pointer to a fresh ClientRest
func NewClient(apiKey, secretKey, passphrase string, baseURL okex.BaseURL, destination okex.Destination) *ClientRest {
c := &ClientRest{
apiKey: apiKey,
secretKey: []byte(secretKey),
passphrase: passphrase,
baseURL: baseURL,
destination: destination,
client: http.DefaultClient,
}
c.Account = NewAccount(c)
c.SubAccount = NewSubAccount(c)
c.Trade = NewTrade(c)
c.Funding = NewFunding(c)
c.Market = NewMarket(c)
c.PublicData = NewPublicData(c)
c.TradeData = NewTradeData(c)
return c
}
// Do the http request to the server
func (c *ClientRest) Do(method, path string, private bool, params ...map[string]string) (*http.Response, error) {
u := fmt.Sprintf("%s%s", c.baseURL, path)
var (
r *http.Request
err error
j []byte
body string
)
if method == http.MethodGet {
r, err = http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
}
if len(params) > 0 {
q := r.URL.Query()
for k, v := range params[0] {
q.Add(k, strings.ReplaceAll(v, "\"", ""))
}
r.URL.RawQuery = q.Encode()
if len(params[0]) > 0 {
path += "?" + r.URL.RawQuery
}
}
} else {
j, err = json.Marshal(params[0])
if err != nil {
return nil, err
}
body = string(j)
if body == "{}" {
body = ""
}
r, err = http.NewRequest(method, u, bytes.NewBuffer(j))
if err != nil {
return nil, err
}
r.Header.Add("Content-Type", "application/json")
}
if err != nil {
return nil, err
}
if private {
timestamp, sign := c.sign(method, path, body)
r.Header.Add("OK-ACCESS-KEY", c.apiKey)
r.Header.Add("OK-ACCESS-PASSPHRASE", c.passphrase)
r.Header.Add("OK-ACCESS-SIGN", sign)
r.Header.Add("OK-ACCESS-TIMESTAMP", timestamp)
}
if c.destination == okex.DemoServer {
r.Header.Add("x-simulated-trading", "1")
}
return c.client.Do(r)
}
// Status
// Get event status of system upgrade
//
// https://www.okex.com/docs-v5/en/#rest-api-status
func (c *ClientRest) Status(req requests.Status) (response responses.Status, err error) {
p := "/api/v5/system/status"
m := okex.S2M(req)
res, err := c.Do(http.MethodGet, p, false, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
func (c *ClientRest) sign(method, path, body string) (string, string) {
format := "2006-01-02T15:04:05.999Z07:00"
t := time.Now().UTC().Format(format)
ts := fmt.Sprint(t)
s := ts + method + path + body
p := []byte(s)
h := hmac.New(sha256.New, c.secretKey)
h.Write(p)
return ts, base64.StdEncoding.EncodeToString(h.Sum(nil))
}