Compare commits

..

9 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
zhangkun9038@dingtalk.com
3324e49678 remove proxy 2025-03-05 20:28:19 +08:00
zhangkun9038@dingtalk.com
cbbc4fc3c7 remove proxy 2025-03-05 20:23:49 +08:00
zhangkun9038@dingtalk.com
f6c7f278d7 remove proxy 2025-03-05 20:15:53 +08:00
15 changed files with 91 additions and 52 deletions

2
go.mod
View File

@ -1,4 +1,4 @@
module github.com/phyer/v5sdkgo
module gitea.zjmud.xyz/phyer/v5sdkgo
go 1.15

View File

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

View File

@ -6,7 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
. "github.com/phyer/v5sdkgo/utils"
"gitea.zjmud.xyz/phyer/v5sdkgo/utils"
"io/ioutil"
"net/http"
"strings"
@ -44,10 +44,49 @@ type RESTAPIResult struct {
TotalUsedTime time.Duration `json:"totalUsedTime"`
}
// ResponseData 定义统一的响应数据接口
type ResponseData interface {
GetCode() string
GetMsg() string
GetData() interface{}
}
type Okexv5APIResponse struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data []map[string]interface{} `json:"data"`
Code string `json:"code"`
Msg string `json:"msg"`
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
}
// GET请求
func (this *RESTAPI) Get(ctx context.Context, uri string, param *map[string]interface{}) (res *RESTAPIResult, err error) {
// GetCandles 获取蜡烛图数据
func (this *RESTAPI) GetCandles(ctx context.Context, uri string, param *map[string]interface{}) (res ResponseData, err error) {
this.Method = GET
this.Uri = uri
@ -133,7 +172,7 @@ func (this *RESTAPI) Get(ctx context.Context, uri string, param *map[string]inte
}
// 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.Uri = uri
@ -147,11 +186,16 @@ func (this *RESTAPI) Post(ctx context.Context, uri string, param *map[string]int
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 {
err = errors.New("APIKey不可为空")
return
if err != nil {
return nil, err
}
return res.V5Response, nil
}
procStart := time.Now()
@ -164,14 +208,11 @@ func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
client := &http.Client{
Timeout: this.Timeout,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
}
uri, body, err := this.GenReqInfo()
if err != nil {
return
return nil, err
}
url := this.EndPoint + uri
@ -180,7 +221,7 @@ func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
req, err := http.NewRequest(this.Method, url, bodyBuf)
if err != nil {
return
return nil, err
}
res = &RESTAPIResult{
@ -189,12 +230,12 @@ func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
}
// Sign and set request headers
timestamp := IsoTime()
preHash := PreHashString(timestamp, this.Method, uri, body)
timestamp := utils.IsoTime()
preHash := utils.PreHashString(timestamp, this.Method, uri, body)
//log.Println("preHash:", preHash)
sign, err := HmacSha256Base64Signer(preHash, this.ApiKeyInfo.SecKey)
sign, err := utils.HmacSha256Base64Signer(preHash, this.ApiKeyInfo.SecKey)
if err != nil {
return
return nil, err
}
//log.Println("sign:", sign)
headStr := this.SetHeaders(req, timestamp, sign)
@ -204,7 +245,7 @@ func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
resp, err := client.Do(req)
if err != nil {
fmt.Println("请求失败!", err)
return
return nil, err
}
defer resp.Body.Close()
@ -213,7 +254,7 @@ func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
resBuff, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("获取请求结果失败!", err)
return
return nil, err
}
res.Body = string(resBuff)
@ -224,12 +265,12 @@ func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
err = json.Unmarshal(resBuff, &v5rsp)
if err != nil {
fmt.Println("解析v5返回失败", err)
return
return nil, err
}
res.V5Response = v5rsp
return
return nil, err
}
/*
@ -315,7 +356,7 @@ func (this *RESTAPI) PrintRequest(request *http.Request, body string, preHash st
if 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("\tMethod: " + strings.ToUpper(request.Method))
if len(request.Header) > 0 {

View File

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

View File

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

View File

@ -6,7 +6,7 @@ package wImpl
import (
"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 {
data, err := Struct2JsonString(r)
data, err := utils.Struct2JsonString(r)
if err != nil {
return ""
}

View File

@ -1,11 +1,11 @@
package wInterface
import (
. "github.com/phyer/v5sdkgo/ws/wImpl"
"gitea.zjmud.xyz/phyer/v5sdkgo/ws/wImpl"
)
// 请求数据
type WSParam interface {
EventType() Event
EventType() wImpl.Event
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
import (
"fmt"
. "gitea.zjmud.xyz/phyer/v5sdkgo/wImpl"
"log"
"testing"
"time"
. "v5sdk_go/ws/wImpl"
)
const (

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -218,7 +218,7 @@ func TestWsClient_Jrpc(t *testing.T) {
}
/*
测试 添加全局消息回调函数
测试 添加全局消息回调函数
*/
func TestAddMessageHook(t *testing.T) {
@ -235,7 +235,7 @@ func TestAddMessageHook(t *testing.T) {
}
/*
普通推送数据回调函数
普通推送数据回调函数
*/
func TestAddBookedDataHook(t *testing.T) {
var r *WsClient
@ -313,7 +313,6 @@ func TestGetInfoFromErrMsg(t *testing.T) {
}
/*
*/
func TestParseMessage(t *testing.T) {
r := prework()
@ -329,7 +328,7 @@ func TestParseMessage(t *testing.T) {
}
/*
原始方式 深度订阅 测试
原始方式 深度订阅 测试
*/
func TestSubscribeTBT(t *testing.T) {
r := prework()
@ -361,7 +360,6 @@ func TestSubscribeTBT(t *testing.T) {
}
/*
*/
func TestSubscribeBalAndPos(t *testing.T) {
r := prework_pri(CROSS_ACCOUNT)