Compare commits

...

6 Commits

Author SHA1 Message Date
zhangkun9038@dingtalk.com
75b949a50e Get chagnes to GetCandles 2025-03-05 22:33:33 +08:00
zhangkun9038@dingtalk.com
f3e1506f2f Get chagnes to GetCandles 2025-03-05 22:26:30 +08:00
zhangkun9038@dingtalk.com
bd2354b81e Get chagnes to GetCandles 2025-03-05 22:21:00 +08:00
zhangkun9038@dingtalk.com
bd0154310b Get chagnes to GetCandles 2025-03-05 22:03:38 +08:00
zhangkun9038@dingtalk.com
95fc2695be transform to gitea.zjmud.xyz 2025-03-05 21:42:33 +08:00
zhangkun9038@dingtalk.com
9d5fb45249 transform to gitea.zjmud.xyz 2025-03-05 20:52:20 +08:00
14 changed files with 90 additions and 48 deletions

View File

@ -3,8 +3,8 @@ package v5sdkgo
import ( import (
"context" "context"
"fmt" "fmt"
. "github.com/phyer/v5sdkgo/rest" . "gitea.zjmud.xyz/phyer/v5sdkgo/rest"
. "github.com/phyer/v5sdkgo/ws" . "gitea.zjmud.xyz/phyer/v5sdkgo/ws"
"log" "log"
"time" "time"

View File

@ -6,7 +6,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
. "github.com/phyer/v5sdkgo/utils" "gitea.zjmud.xyz/phyer/v5sdkgo/utils"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
@ -44,10 +44,49 @@ type RESTAPIResult struct {
TotalUsedTime time.Duration `json:"totalUsedTime"` TotalUsedTime time.Duration `json:"totalUsedTime"`
} }
// ResponseData 定义统一的响应数据接口
type ResponseData interface {
GetCode() string
GetMsg() string
GetData() interface{}
}
type Okexv5APIResponse struct { type Okexv5APIResponse struct {
Code string `json:"code"` Code string `json:"code"`
Msg string `json:"msg"` Msg string `json:"msg"`
Data []map[string]interface{} `json:"data"` Data interface{} `json:"data"`
}
// 实现 ResponseData 接口
func (r Okexv5APIResponse) GetCode() string {
return r.Code
}
func (r Okexv5APIResponse) GetMsg() string {
return r.Msg
}
func (r Okexv5APIResponse) GetData() interface{} {
return r.Data
}
type CandleDataResponse struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data [][]interface{} `json:"data"` // 用二维数组来接收 candles 数据
}
// 实现 ResponseData 接口
func (r CandleDataResponse) GetCode() string {
return r.Code
}
func (r CandleDataResponse) GetMsg() string {
return r.Msg
}
func (r CandleDataResponse) GetData() interface{} {
return r.Data
} }
/* /*
@ -118,8 +157,8 @@ func (this *RESTAPI) SetTimeOut(timeout time.Duration) *RESTAPI {
return this return this
} }
// GET请求 // GetCandles 获取蜡烛图数据
func (this *RESTAPI) Get(ctx context.Context, uri string, param *map[string]interface{}) (res *RESTAPIResult, err error) { func (this *RESTAPI) GetCandles(ctx context.Context, uri string, param *map[string]interface{}) (res ResponseData, err error) {
this.Method = GET this.Method = GET
this.Uri = uri this.Uri = uri
@ -133,7 +172,7 @@ func (this *RESTAPI) Get(ctx context.Context, uri string, param *map[string]inte
} }
// POST请求 // POST请求
func (this *RESTAPI) Post(ctx context.Context, uri string, param *map[string]interface{}) (res *RESTAPIResult, err error) { func (this *RESTAPI) Post(ctx context.Context, uri string, param *map[string]interface{}) (res ResponseData, err error) {
this.Method = POST this.Method = POST
this.Uri = uri this.Uri = uri
@ -147,11 +186,16 @@ func (this *RESTAPI) Post(ctx context.Context, uri string, param *map[string]int
return this.Run(ctx) return this.Run(ctx)
} }
func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) { func (this *RESTAPI) Run(ctx context.Context) (ResponseData, error) {
var res *RESTAPIResult
var err error
if this.ApiKeyInfo == nil { if this.ApiKeyInfo == nil {
err = errors.New("APIKey不可为空") err = errors.New("APIKey不可为空")
return if err != nil {
return nil, err
}
return res.V5Response, nil
} }
procStart := time.Now() procStart := time.Now()
@ -168,7 +212,7 @@ func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
uri, body, err := this.GenReqInfo() uri, body, err := this.GenReqInfo()
if err != nil { if err != nil {
return return nil, err
} }
url := this.EndPoint + uri url := this.EndPoint + uri
@ -177,7 +221,7 @@ func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
req, err := http.NewRequest(this.Method, url, bodyBuf) req, err := http.NewRequest(this.Method, url, bodyBuf)
if err != nil { if err != nil {
return return nil, err
} }
res = &RESTAPIResult{ res = &RESTAPIResult{
@ -186,12 +230,12 @@ func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
} }
// Sign and set request headers // Sign and set request headers
timestamp := IsoTime() timestamp := utils.IsoTime()
preHash := PreHashString(timestamp, this.Method, uri, body) preHash := utils.PreHashString(timestamp, this.Method, uri, body)
//log.Println("preHash:", preHash) //log.Println("preHash:", preHash)
sign, err := HmacSha256Base64Signer(preHash, this.ApiKeyInfo.SecKey) sign, err := utils.HmacSha256Base64Signer(preHash, this.ApiKeyInfo.SecKey)
if err != nil { if err != nil {
return return nil, err
} }
//log.Println("sign:", sign) //log.Println("sign:", sign)
headStr := this.SetHeaders(req, timestamp, sign) headStr := this.SetHeaders(req, timestamp, sign)
@ -201,7 +245,7 @@ func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
fmt.Println("请求失败!", err) fmt.Println("请求失败!", err)
return return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
@ -210,7 +254,7 @@ func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
resBuff, err := ioutil.ReadAll(resp.Body) resBuff, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
fmt.Println("获取请求结果失败!", err) fmt.Println("获取请求结果失败!", err)
return return nil, err
} }
res.Body = string(resBuff) res.Body = string(resBuff)
@ -221,12 +265,12 @@ func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
err = json.Unmarshal(resBuff, &v5rsp) err = json.Unmarshal(resBuff, &v5rsp)
if err != nil { if err != nil {
fmt.Println("解析v5返回失败", err) fmt.Println("解析v5返回失败", err)
return return nil, err
} }
res.V5Response = v5rsp res.V5Response = v5rsp
return return nil, err
} }
/* /*
@ -312,7 +356,7 @@ func (this *RESTAPI) PrintRequest(request *http.Request, body string, preHash st
if this.ApiKeyInfo.SecKey != "" { if this.ApiKeyInfo.SecKey != "" {
fmt.Println(" Secret-Key: " + this.ApiKeyInfo.SecKey) fmt.Println(" Secret-Key: " + this.ApiKeyInfo.SecKey)
} }
fmt.Println(" Request(" + IsoTime() + "):") fmt.Println(" Request(" + utils.IsoTime() + "):")
fmt.Println("\tUrl: " + request.URL.String()) fmt.Println("\tUrl: " + request.URL.String())
fmt.Println("\tMethod: " + strings.ToUpper(request.Method)) fmt.Println("\tMethod: " + strings.ToUpper(request.Method))
if len(request.Header) > 0 { if len(request.Header) > 0 {

View File

@ -2,8 +2,8 @@ package ws
import ( import (
"errors" "errors"
. "github.com/phyer/v5sdkgo/ws/wImpl" . "gitea.zjmud.xyz/phyer/v5sdkgo/ws/wImpl"
. "github.com/phyer/v5sdkgo/ws/wInterface" . "gitea.zjmud.xyz/phyer/v5sdkgo/ws/wInterface"
"log" "log"
"runtime/debug" "runtime/debug"
) )

View File

@ -5,7 +5,7 @@ package wImpl
import ( import (
"encoding/json" "encoding/json"
. "github.com/phyer/v5sdkgo/utils" "gitea.zjmud.xyz/phyer/v5sdkgo/utils"
) )
// jrpc请求结构体 // jrpc请求结构体
@ -20,7 +20,7 @@ func (r JRPCReq) GetType() int {
} }
func (r JRPCReq) ToString() string { func (r JRPCReq) ToString() string {
data, err := Struct2JsonString(r) data, err := utils.Struct2JsonString(r)
if err != nil { if err != nil {
return "" return ""
} }

View File

@ -6,7 +6,7 @@ package wImpl
import ( import (
"encoding/json" "encoding/json"
. "github.com/phyer/v5sdkgo/utils" "gitea.zjmud.xyz/phyer/v5sdkgo/utils"
) )
// 客户端请求消息格式 // 客户端请求消息格式
@ -20,7 +20,7 @@ func (r ReqData) GetType() int {
} }
func (r ReqData) ToString() string { func (r ReqData) ToString() string {
data, err := Struct2JsonString(r) data, err := utils.Struct2JsonString(r)
if err != nil { if err != nil {
return "" return ""
} }

View File

@ -1,11 +1,11 @@
package wInterface package wInterface
import ( import (
. "github.com/phyer/v5sdkgo/ws/wImpl" "gitea.zjmud.xyz/phyer/v5sdkgo/ws/wImpl"
) )
// 请求数据 // 请求数据
type WSParam interface { type WSParam interface {
EventType() Event EventType() wImpl.Event
ToMap() *map[string]string ToMap() *map[string]string
} }

View File

@ -4,10 +4,10 @@ package ws
// go test ws_cli.go ws_op.go ws_contants.go utils.go ws_pub_channel.go ws_pub_channel_test.go ws_priv_channel.go ws_priv_channel_test.go ws_jrpc.go ws_jrpc_test.go ws_test_AddBookedDataHook.go -v // go test ws_cli.go ws_op.go ws_contants.go utils.go ws_pub_channel.go ws_pub_channel_test.go ws_priv_channel.go ws_priv_channel_test.go ws_jrpc.go ws_jrpc_test.go ws_test_AddBookedDataHook.go -v
import ( import (
"fmt" "fmt"
. "gitea.zjmud.xyz/phyer/v5sdkgo/wImpl"
"log" "log"
"testing" "testing"
"time" "time"
. "v5sdk_go/ws/wImpl"
) )
const ( const (

View File

@ -5,9 +5,9 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
. "github.com/phyer/v5sdkgo/config" . "gitea.zjmud.xyz/phyer/v5sdkgo/config"
. "github.com/phyer/v5sdkgo/utils" . "gitea.zjmud.xyz/phyer/v5sdkgo/utils"
. "github.com/phyer/v5sdkgo/ws/wImpl" . "gitea.zjmud.xyz/phyer/v5sdkgo/ws/wImpl"
"log" "log"
"regexp" "regexp"
"runtime/debug" "runtime/debug"

View File

@ -2,7 +2,7 @@ package ws
import ( import (
"context" "context"
. "github.com/phyer/v5sdkgo/ws/wImpl" . "gitea.zjmud.xyz/phyer/v5sdkgo/ws/wImpl"
"log" "log"
"time" "time"
) )

View File

@ -3,11 +3,11 @@ package ws
import ( import (
"context" "context"
"errors" "errors"
. "github.com/phyer/v5sdkgo/config" . "gitea.zjmud.xyz/phyer/v5sdkgo/config"
. "github.com/phyer/v5sdkgo/rest" . "gitea.zjmud.xyz/phyer/v5sdkgo/rest"
. "github.com/phyer/v5sdkgo/utils" . "gitea.zjmud.xyz/phyer/v5sdkgo/utils"
. "github.com/phyer/v5sdkgo/ws/wImpl" . "gitea.zjmud.xyz/phyer/v5sdkgo/ws/wImpl"
. "github.com/phyer/v5sdkgo/ws/wInterface" . "gitea.zjmud.xyz/phyer/v5sdkgo/ws/wInterface"
"log" "log"
"sync" "sync"
"time" "time"

View File

@ -1,7 +1,7 @@
package ws package ws
import ( import (
. "github.com/phyer/v5sdkgo/ws/wImpl" . "gitea.zjmud.xyz/phyer/v5sdkgo/ws/wImpl"
) )
/* /*

View File

@ -2,7 +2,7 @@ package ws
import ( import (
"errors" "errors"
. "github.com/phyer/v5sdkgo/ws/wImpl" . "gitea.zjmud.xyz/phyer/v5sdkgo/ws/wImpl"
) )
/* /*

View File

@ -3,7 +3,7 @@ package ws
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
. "github.com/phyer/v5sdkgo/ws/wImpl" . "gitea.zjmud.xyz/phyer/v5sdkgo/ws/wImpl"
"log" "log"
"strings" "strings"
"testing" "testing"

View File

@ -313,7 +313,6 @@ func TestGetInfoFromErrMsg(t *testing.T) {
} }
/* /*
*/ */
func TestParseMessage(t *testing.T) { func TestParseMessage(t *testing.T) {
r := prework() r := prework()
@ -361,7 +360,6 @@ func TestSubscribeTBT(t *testing.T) {
} }
/* /*
*/ */
func TestSubscribeBalAndPos(t *testing.T) { func TestSubscribeBalAndPos(t *testing.T) {
r := prework_pri(CROSS_ACCOUNT) r := prework_pri(CROSS_ACCOUNT)