/
huobi.go
65 lines (54 loc) · 1.34 KB
/
huobi.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
// Package huobi huobi rest api package
package huobi
import (
"io/ioutil"
"net/http"
"strings"
"github.com/Akagi201/cryptotrader/model"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
const (
API = "https://api.huobi.pro/"
)
// Huobi API data
type Huobi struct {
AccessKey string
SecretKey string
}
// New create new Huobi API data
func New(accessKey string, secretKey string) *Huobi {
return &Huobi{
AccessKey: accessKey,
SecretKey: secretKey,
}
}
// GetTicker 行情
func (hb *Huobi) GetTicker(base string, quote string) (*model.Ticker, error) {
url := API + "market/detail/merged?symbol=" + strings.ToLower(quote) + strings.ToLower(base)
log.Debugf("Request url: %v", url)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
log.Debugf("Response body: %v", string(body))
buy := gjson.GetBytes(body, "tick.bid").Array()[0].Float()
sell := gjson.GetBytes(body, "tick.ask").Array()[0].Float()
last := gjson.GetBytes(body, "tick.close").Float()
low := gjson.GetBytes(body, "tick.low").Float()
high := gjson.GetBytes(body, "tick.high").Float()
vol := gjson.GetBytes(body, "tick.vol").Float()
return &model.Ticker{
Buy: buy,
Sell: sell,
Last: last,
Low: low,
High: high,
Vol: vol,
}, nil
}