Compare commits

..

No commits in common. "master" and "v0.1.5" have entirely different histories.

15 changed files with 52 additions and 91 deletions

2
go.mod

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

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

@ -6,7 +6,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"gitea.zjmud.xyz/phyer/v5sdkgo/utils" . "github.com/phyer/v5sdkgo/utils"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
@ -44,49 +44,10 @@ 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 interface{} `json:"data"` Data []map[string]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
} }
/* /*
@ -157,8 +118,8 @@ func (this *RESTAPI) SetTimeOut(timeout time.Duration) *RESTAPI {
return this return this
} }
// GetCandles 获取蜡烛图数据 // GET请求
func (this *RESTAPI) GetCandles(ctx context.Context, uri string, param *map[string]interface{}) (res ResponseData, err error) { func (this *RESTAPI) Get(ctx context.Context, uri string, param *map[string]interface{}) (res *RESTAPIResult, err error) {
this.Method = GET this.Method = GET
this.Uri = uri this.Uri = uri
@ -172,7 +133,7 @@ func (this *RESTAPI) GetCandles(ctx context.Context, uri string, param *map[stri
} }
// POST请求 // POST请求
func (this *RESTAPI) Post(ctx context.Context, uri string, param *map[string]interface{}) (res ResponseData, err error) { func (this *RESTAPI) Post(ctx context.Context, uri string, param *map[string]interface{}) (res *RESTAPIResult, err error) {
this.Method = POST this.Method = POST
this.Uri = uri this.Uri = uri
@ -186,16 +147,11 @@ 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) (ResponseData, error) { func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
var res *RESTAPIResult
var err error
if this.ApiKeyInfo == nil { if this.ApiKeyInfo == nil {
err = errors.New("APIKey不可为空") err = errors.New("APIKey不可为空")
if err != nil { return
return nil, err
}
return res.V5Response, nil
} }
procStart := time.Now() procStart := time.Now()
@ -208,11 +164,14 @@ func (this *RESTAPI) Run(ctx context.Context) (ResponseData, error) {
client := &http.Client{ client := &http.Client{
Timeout: this.Timeout, Timeout: this.Timeout,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
} }
uri, body, err := this.GenReqInfo() uri, body, err := this.GenReqInfo()
if err != nil { if err != nil {
return nil, err return
} }
url := this.EndPoint + uri url := this.EndPoint + uri
@ -221,7 +180,7 @@ func (this *RESTAPI) Run(ctx context.Context) (ResponseData, error) {
req, err := http.NewRequest(this.Method, url, bodyBuf) req, err := http.NewRequest(this.Method, url, bodyBuf)
if err != nil { if err != nil {
return nil, err return
} }
res = &RESTAPIResult{ res = &RESTAPIResult{
@ -230,12 +189,12 @@ func (this *RESTAPI) Run(ctx context.Context) (ResponseData, error) {
} }
// Sign and set request headers // Sign and set request headers
timestamp := utils.IsoTime() timestamp := IsoTime()
preHash := utils.PreHashString(timestamp, this.Method, uri, body) preHash := PreHashString(timestamp, this.Method, uri, body)
//log.Println("preHash:", preHash) //log.Println("preHash:", preHash)
sign, err := utils.HmacSha256Base64Signer(preHash, this.ApiKeyInfo.SecKey) sign, err := HmacSha256Base64Signer(preHash, this.ApiKeyInfo.SecKey)
if err != nil { if err != nil {
return nil, err return
} }
//log.Println("sign:", sign) //log.Println("sign:", sign)
headStr := this.SetHeaders(req, timestamp, sign) headStr := this.SetHeaders(req, timestamp, sign)
@ -245,7 +204,7 @@ func (this *RESTAPI) Run(ctx context.Context) (ResponseData, error) {
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
fmt.Println("请求失败!", err) fmt.Println("请求失败!", err)
return nil, err return
} }
defer resp.Body.Close() defer resp.Body.Close()
@ -254,7 +213,7 @@ func (this *RESTAPI) Run(ctx context.Context) (ResponseData, 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 nil, err return
} }
res.Body = string(resBuff) res.Body = string(resBuff)
@ -265,12 +224,12 @@ func (this *RESTAPI) Run(ctx context.Context) (ResponseData, 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 nil, err return
} }
res.V5Response = v5rsp res.V5Response = v5rsp
return nil, err return
} }
/* /*
@ -356,7 +315,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(" + utils.IsoTime() + "):") fmt.Println(" Request(" + 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 {

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

@ -5,7 +5,7 @@ package wImpl
import ( import (
"encoding/json" "encoding/json"
"gitea.zjmud.xyz/phyer/v5sdkgo/utils" . "github.com/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 := utils.Struct2JsonString(r) data, err := Struct2JsonString(r)
if err != nil { if err != nil {
return "" return ""
} }

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

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

@ -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 (

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

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

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

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

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

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

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