-
Notifications
You must be signed in to change notification settings - Fork 11
/
client.go
56 lines (47 loc) · 1.14 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
package swap
import (
"bytes"
"context"
"encoding/json"
"net/http"
"github.com/NadiaSama/ccexgo/exchange/huobi"
"github.com/pkg/errors"
)
type (
RestClient struct {
*huobi.RestClient
}
Serializer interface {
Serialize() ([]byte, error)
}
)
const (
SwapHost = "api.hbdm.com"
SwapProHost = "api.huobi.pro"
)
func NewRestClient(key string, secret string) *RestClient {
return NewRestClientWithHost(key, secret, SwapHost)
}
func NewRestClientWithHost(key, secret, host string) *RestClient {
return &RestClient{
RestClient: huobi.NewRestClient(key, secret, host),
}
}
//PrivatePostReq send post request to huobi swap api. the request body is generate from req param
//vai json.Marshal() or Serialize()
func (rc *RestClient) PrivatePostReq(ctx context.Context, endPoint string, req interface{}, dst interface{}) error {
var (
raw []byte
err error
)
if sr, ok := req.(Serializer); ok {
raw, err = sr.Serialize()
} else {
raw, err = json.Marshal(req)
}
if err != nil {
return errors.WithMessage(err, "serialize fail")
}
buf := bytes.NewBuffer(raw)
return rc.Request(ctx, http.MethodPost, endPoint, nil, buf, true, dst)
}