mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
kucoinapi: refactor api client
This commit is contained in:
parent
c8dd02335b
commit
7f92588883
|
@ -120,25 +120,11 @@ func (c *RestClient) newAuthenticatedRequest(method, refURL string, params url.V
|
|||
path += "?" + rel.RawQuery
|
||||
}
|
||||
|
||||
var body []byte
|
||||
|
||||
if payload != nil {
|
||||
switch v := payload.(type) {
|
||||
case string:
|
||||
body = []byte(v)
|
||||
|
||||
case []byte:
|
||||
body = v
|
||||
|
||||
default:
|
||||
body, err = json.Marshal(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
body, err := castPayload(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
req, err := http.NewRequest(method, pathURL.String(), bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -152,6 +138,7 @@ func (c *RestClient) newAuthenticatedRequest(method, refURL string, params url.V
|
|||
return req, nil
|
||||
}
|
||||
|
||||
|
||||
func (c *RestClient) attachAuthHeaders(req *http.Request, method string, path string, body []byte) {
|
||||
// Set location to UTC so that it outputs "2020-12-08T09:08:57.715Z"
|
||||
t := time.Now().In(time.UTC)
|
||||
|
@ -167,6 +154,7 @@ func (c *RestClient) attachAuthHeaders(req *http.Request, method string, path st
|
|||
req.Header.Add("KC-API-KEY-VERSION", c.KeyVersion)
|
||||
}
|
||||
|
||||
// sign uses sha256 to sign the payload with the given secret
|
||||
func sign(secret, payload string) string {
|
||||
var sig = hmac.New(sha256.New, []byte(secret))
|
||||
_, err := sig.Write([]byte(payload))
|
||||
|
@ -176,3 +164,21 @@ func sign(secret, payload string) string {
|
|||
|
||||
return base64.StdEncoding.EncodeToString(sig.Sum(nil))
|
||||
}
|
||||
|
||||
func castPayload(payload interface{}) ([]byte, error) {
|
||||
if payload != nil {
|
||||
switch v := payload.(type) {
|
||||
case string:
|
||||
return []byte(v), nil
|
||||
|
||||
case []byte:
|
||||
return v, nil
|
||||
|
||||
default:
|
||||
body, err := json.Marshal(v)
|
||||
return body, err
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user