/
allcoin.go
95 lines (79 loc) · 1.87 KB
/
allcoin.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
// Package allcoin allcoin rest api package
package allcoin
import (
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/Akagi201/cryptotrader/model"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
const (
API = "https://api.allcoin.com/api/v1/"
)
// Allcoin API data
type Allcoin struct {
AccessKey string
SecretKey string
}
// New create new Allcoin API data
func New(accessKey string, secretKey string) *Allcoin {
return &Allcoin{
AccessKey: accessKey,
SecretKey: secretKey,
}
}
// GetTicker 行情
func (ac *Allcoin) GetTicker(base string, quote string) (*model.Ticker, error) {
url := API + "ticker?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))
buyRes := gjson.GetBytes(body, "ticker.buy").String()
buy, err := strconv.ParseFloat(buyRes, 64)
if err != nil {
return nil, err
}
sellRes := gjson.GetBytes(body, "ticker.sell").String()
sell, err := strconv.ParseFloat(sellRes, 64)
if err != nil {
return nil, err
}
lastRes := gjson.GetBytes(body, "ticker.last").String()
last, err := strconv.ParseFloat(lastRes, 64)
if err != nil {
return nil, err
}
lowRes := gjson.GetBytes(body, "ticker.low").String()
low, err := strconv.ParseFloat(lowRes, 64)
if err != nil {
return nil, err
}
highRes := gjson.GetBytes(body, "ticker.high").String()
high, err := strconv.ParseFloat(highRes, 64)
if err != nil {
return nil, err
}
volRes := gjson.GetBytes(body, "ticker.vol").String()
vol, err := strconv.ParseFloat(volRes, 64)
if err != nil {
return nil, err
}
return &model.Ticker{
Buy: buy,
Sell: sell,
Last: last,
Low: low,
High: high,
Vol: vol,
}, nil
}