From a3d7bd22a8af7e1d851da4019178f364597f476a Mon Sep 17 00:00:00 2001 From: zhangkun Date: Tue, 3 Dec 2024 21:03:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3v5api=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/candle.go | 19 ++++--------------- core/core.go | 26 ++++++++++++++++++++++++-- demo.json | 8 ++++++++ 3 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 demo.json diff --git a/core/candle.go b/core/candle.go index 3f95037..68f0192 100644 --- a/core/candle.go +++ b/core/candle.go @@ -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) diff --git a/core/core.go b/core/core.go index 6aa3041..f04ce6c 100644 --- a/core/core.go +++ b/core/core.go @@ -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{} diff --git a/demo.json b/demo.json new file mode 100644 index 0000000..71b5636 --- /dev/null +++ b/demo.json @@ -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"] + ] +}