forked from Akagi201/cryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters