解决v5api返回报错问题

This commit is contained in:
zhangkun 2024-12-03 21:03:00 +08:00
parent 99cd468004
commit a3d7bd22a8
3 changed files with 36 additions and 17 deletions

View File

@ -9,7 +9,7 @@ import (
"strconv"
"strings"
"time"
"v5sdk_go/rest"
// "v5sdk_go/rest"
simple "github.com/bitly/go-simplejson"
"github.com/go-redis/redis"
@ -150,23 +150,12 @@ func IsModOf(curInt int64, duration time.Duration) bool {
return false
}
func (core *Core) SaveCandle(instId string, period string, rsp *rest.RESTAPIResult, dura time.Duration, withWs bool) {
js, err := simple.NewJson([]byte(rsp.Body))
if err != nil {
fmt.Println("restTicker err: ", err, rsp.Body)
return
}
if len(rsp.Body) == 0 {
fmt.Println("rsp body is null")
return
}
itemList := js.Get("data").MustArray()
Daoxu(itemList)
for _, v := range itemList {
func (core *Core) SaveCandle(instId string, period string, rsp *CandleData, dura time.Duration, withWs bool) {
for _, v := range rsp.Data {
candle := Candle{
InstId: instId,
Period: period,
Data: v.([]interface{}),
Data: v,
From: "rest",
}
candle.SetToKey(core)

View File

@ -6,6 +6,8 @@ import (
// "errors"
"fmt"
// "math/rand"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
@ -41,7 +43,11 @@ type RestQueue struct {
Duration time.Duration
WithWs bool
}
type CandleData struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data [][]interface{} `json:"data"` // 用二维数组来接收 candles 数据
}
type SubAction struct {
ActionName string
ForAll bool
@ -68,7 +74,7 @@ func (rst *RestQueue) Save(cr *Core) {
link := "/api/v5/market/candles?instId=" + rst.InstId + "&bar=" + rst.Bar + limitSec + afterSec + beforeSec
fmt.Println("restLink: ", link)
rsp, _ := cr.RestInvoke(link, rest.GET)
rsp, _ := cr.v5PublicInvoke(link)
cr.SaveCandle(rst.InstId, rst.Bar, rsp, rst.Duration, rst.WithWs)
}
@ -193,6 +199,22 @@ func (core *Core) SubscribeTicker(op string) error {
return nil
}
func (core *Core) v5PublicInvoke(subUrl string) (*CandleData, error) {
restUrl, _ := core.Cfg.Config.Get("connect").Get("restBaseUrl").String()
url := restUrl + subUrl
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var result CandleData
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
return &result, nil
}
func (core *Core) RestInvoke(subUrl string, method string) (*rest.RESTAPIResult, error) {
restUrl, _ := core.Cfg.Config.Get("connect").Get("restBaseUrl").String()
//ep, method, uri string, param *map[string]interface{}

8
demo.json Normal file
View File

@ -0,0 +1,8 @@
{
"code":"0",
"msg":"",
"data":[
["1733224500000","224.35","225.63","224.34","225.39","8181.5288","1841178.04602941","1841178.04602941","1"],
["1733223600000","223.25","224.74","222.95","224.36","4643.858629","1040747.41332915","1040747.41332915","1"]
]
}