-
Notifications
You must be signed in to change notification settings - Fork 0
/
kucoin.go
251 lines (229 loc) · 6.26 KB
/
kucoin.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
package kugo
import (
"bytes"
"errors"
"fmt"
"github.com/go-resty/resty/v2"
"log"
"net/http"
"net/url"
"strings"
"time"
)
// SpotEndpoint Base URL
const SpotEndpoint = "https://api.kucoin.com"
const FutureEndpoint = "https://api-futures.kucoin.com"
const ApiKeyVersionV2 = "2"
type Kucoin struct {
spotEndpoint string
futureEndpoint string
secretKey string
accessKey string
passphrase string
signer Signer
client *resty.Client
reqLog func(...interface{})
respLog func(...interface{})
debug bool
}
type Option func(kc *Kucoin) error
// NewKucoin Create a Kucoin API instance
func NewKucoin(opts ...Option) (*Kucoin, error) {
kc := &Kucoin{}
kc.spotEndpoint = SpotEndpoint
kc.futureEndpoint = FutureEndpoint
kc.reqLog = defaultLog
kc.respLog = defaultLog
kc.client = resty.NewWithClient(
&http.Client{Transport: &http.Transport{
DisableKeepAlives: true},
Timeout: 10 * time.Second})
err := kc.Set(opts...)
if err != nil {
return nil, err
}
kc.registerMiddleware()
return kc, nil
}
// Print the log to the console
func defaultLog(v ...interface{}) {
log.Println(v...)
}
// Set optional parameters
func (kc *Kucoin) Set(options ...Option) error {
for _, option := range options {
if err := option(kc); err != nil {
return err
}
}
kc.signer = NewKcSignerV2(kc.accessKey, kc.secretKey, kc.passphrase)
return nil
}
// SetApiKey Set the API key created in Kucoin
func SetApiKey(accessKey, secretKey, passphrase string) Option {
return func(kc *Kucoin) error {
if kc == nil {
return errors.New("instance is nil")
}
kc.accessKey = accessKey
kc.secretKey = secretKey
kc.passphrase = passphrase
return nil
}
}
// SetSpotEndpoint Use the specified base URL to access the Kucoin spot API interface
func SetSpotEndpoint(spotEndpoint string) Option {
return func(kc *Kucoin) error {
if kc == nil {
return errors.New("instance is nil")
}
kc.spotEndpoint = spotEndpoint
return nil
}
}
// SetFutureEndpoint Use the specified base URL to access the Kucoin future API interface
func SetFutureEndpoint(futureEndpoint string) Option {
return func(kc *Kucoin) error {
if kc == nil {
return errors.New("instance is nil")
}
kc.futureEndpoint = futureEndpoint
return nil
}
}
// SetClient Set HTTP client
func SetClient(client *http.Client) Option {
return func(kc *Kucoin) error {
if kc == nil {
return errors.New("instance is nil")
}
if client == nil {
return errors.New("client is nil")
}
kc.client = resty.NewWithClient(client)
return nil
}
}
// SetRequestLog Set the HTTP request log printing method. Set debug to true for it to work
func SetRequestLog(l func(...interface{})) Option {
return func(kc *Kucoin) error {
if kc == nil {
return errors.New("instance is nil")
}
if l == nil {
return errors.New("logger is nil")
}
kc.reqLog = l
return nil
}
}
// SetResponseLog Set the HTTP response log printing method. Set debug to true for it to work
func SetResponseLog(l func(...interface{})) Option {
return func(kc *Kucoin) error {
if kc == nil {
return errors.New("instance is nil")
}
if l == nil {
return errors.New("logger is nil")
}
kc.respLog = l
return nil
}
}
// SetDebug If set debug to ture, the http request and response logs will be output
func SetDebug(debug bool) Option {
return func(kc *Kucoin) error {
kc.debug = debug
return nil
}
}
// Register HTTP request middleware
func (kc *Kucoin) registerMiddleware() {
// Registering Request Middleware
kc.client.OnBeforeRequest(func(c *resty.Client, req *resty.Request) error {
// Now you have access to Client and current Request object
// manipulate it as per your need
req.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36")
req.Header.Add("Content-Type", "application/json")
if kc.client != nil && kc.debug {
body := ""
if req.Body != nil {
body = string(req.Body.([]byte))
}
kc.reqLog(fmt.Sprintf("info:request\turl:%s\tquery_param:%+v\tform_data:%+v\tbody:%+v\theader=%+v", req.URL, req.QueryParam, req.FormData, body, req.Header))
}
return nil
})
// Registering Response Middleware
kc.client.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {
if kc.respLog != nil && kc.debug {
body := ""
if resp.Request.Body != nil {
body = string(resp.Request.Body.([]byte))
}
kc.respLog(fmt.Sprintf("info:response\turl:%s\tquery_param:%+v\tform_data:%+v\tbody:%+v\tresponse:%s\theader=%+v", resp.Request.URL, resp.Request.QueryParam, resp.Request.FormData, body, string(resp.Body()), resp.Request.Header))
}
return nil
})
}
// do Send http request to Kucoin.
// When method is GET or DELETE, the type of params is map[string]string{}.
// When method is POST, the type of params is []byte.
// Examples:
// do("https://www.kucoin.com", "GET", "/api/v1/accounts", map[string]string{"currency":"BTC", "type":"trade"})
// do("https://www.kucoin.com", "POST", "/api/v1/orders", []byte("{\"price\":\"100\",...}"))
func (kc *Kucoin) do(endpoint string, method string, uri string, params interface{}) (resp *resty.Response, err error) {
us := fmt.Sprintf("%s%s", endpoint, uri)
header := make(map[string]string)
body := make([]byte, 0)
if kc.signer != nil {
var b bytes.Buffer
b.WriteString(method)
if method == http.MethodGet || method == http.MethodDelete {
if params != nil {
p, _ := params.(map[string]string)
qs := make([]string, 0, len(p))
for k, v := range p {
qs = append(qs, fmt.Sprintf("%s=%s", k, v))
}
us = fmt.Sprintf("%s?%s", us, strings.Join(qs, "&"))
}
u, e := url.Parse(us)
if e != nil {
err = e
return
}
us = u.String()
uri = u.RequestURI()
b.WriteString(uri)
} else {
b.WriteString(uri)
if params != nil {
body, _ = params.([]byte)
fmt.Println(string(body))
b.Write(body)
}
}
h := kc.signer.(*KcSigner).Headers(b.String())
for k, v := range h {
header[k] = v
}
}
req := kc.client.R().
SetHeaders(header)
switch method {
case http.MethodGet:
resp, err = req.Get(us)
return
case http.MethodDelete:
resp, err = req.Delete(us)
return
case http.MethodPost:
resp, err = req.SetBody(body).Post(us)
return
default:
err = errors.New("method error")
return
}
return
}