/
client.go
154 lines (141 loc) · 3.37 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package kucoin
import (
"crypto/hmac"
"crypto/sha256"
b64 "encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"path"
"strings"
"time"
)
type client struct {
apiKey string
apiSecret string
httpClient http.Client
debug bool
}
func newClient(apiKey, apiSecret string) (c *client) {
c = &client{
apiKey: apiKey,
apiSecret: apiSecret,
}
c.httpClient.Timeout = time.Second * 30
return
}
func (c client) dumpRequest(r *http.Request) {
if r == nil {
log.Println("dumpReq ok: <nil>")
} else {
dump, err := httputil.DumpRequest(r, true)
if err != nil {
log.Printf("dumpReq err: %s\n", err)
} else {
log.Printf("dumpReq ok: %s\n", dump)
}
}
}
func (c client) dumpResponse(r *http.Response) {
if r == nil {
log.Println("dumpResponse ok: <nil>")
} else {
dump, err := httputil.DumpResponse(r, true)
if err != nil {
log.Printf("dumpResponse err: %s\n", err)
} else {
log.Printf("dumpResponse ok: %s\n", dump)
}
}
}
// do prepare and process HTTP request to Kucoin API.
/*
* Example
* POST parameters:
* type: BUY
* amount: 10
* price: 1.1
* Arrange the parameters in ascending alphabetical order (lower cases first),
then combine them with & (don't urlencode them, don't add ?, don't add extra &),
e.g. amount=10&price=1.1&type=BUY
*/
func (c *client) do(method, resource string, payload map[string]string, authNeeded bool) ([]byte, error) {
var req *http.Request
Url, err := url.Parse(kucoinUrl)
if err != nil {
return nil, err
}
Url.Path = path.Join(Url.Path, resource)
queryString := ""
if method == "GET" {
q := Url.Query()
for key, value := range payload {
q.Set(key, value)
}
Url.RawQuery = q.Encode()
req, err = http.NewRequest("GET", Url.String(), nil)
queryString = Url.Query().Encode()
} else {
postValues := url.Values{}
for key, value := range payload {
postValues.Set(key, value)
}
queryString = postValues.Encode()
req, err = http.NewRequest(
method, Url.String(), strings.NewReader(
queryString,
),
)
}
if err != nil {
return nil, err
}
if method == "POST" || method == "PUT" {
req.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
}
req.Header.Add("Accept", "application/json")
// Auth
if authNeeded {
if len(c.apiKey) == 0 || len(c.apiSecret) == 0 {
return nil, errors.New("API Key and API Secret must be set")
}
nonce := time.Now().UnixNano() / int64(time.Millisecond)
req.Header.Add("KC-API-KEY", c.apiKey)
req.Header.Add("KC-API-NONCE", fmt.Sprintf("%v", nonce))
req.Header.Add(
"KC-API-SIGNATURE", c.sign(
Url.Path, queryString, nonce,
),
)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
err = errors.New(resp.Status)
}
return data, err
}
func computeHmac256(message, secret string) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
io.WriteString(h, message)
return fmt.Sprintf("%x", h.Sum(nil))
}
func (c *client) sign(path, queryString string, nonce int64) (signature string) {
strForSign := fmt.Sprintf("%s/%v/%s", path, nonce, queryString)
signatureStr := b64.StdEncoding.EncodeToString([]byte(strForSign))
signature = computeHmac256(signatureStr, c.apiSecret)
return
}