Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
75b949a50e | ||
![]() |
f3e1506f2f | ||
![]() |
bd2354b81e | ||
![]() |
bd0154310b | ||
![]() |
95fc2695be | ||
![]() |
9d5fb45249 | ||
![]() |
3324e49678 | ||
![]() |
cbbc4fc3c7 |
2
go.mod
2
go.mod
@ -1,4 +1,4 @@
|
||||
module github.com/phyer/v5sdkgo
|
||||
module gitea.zjmud.xyz/phyer/v5sdkgo
|
||||
|
||||
go 1.15
|
||||
|
||||
|
4
main.go
4
main.go
@ -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"
|
||||
|
80
rest/rest.go
80
rest/rest.go
@ -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"`
|
||||
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()
|
||||
@ -168,7 +212,7 @@ func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
|
||||
|
||||
uri, body, err := this.GenReqInfo()
|
||||
if err != nil {
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res = &RESTAPIResult{
|
||||
@ -186,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)
|
||||
@ -201,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()
|
||||
|
||||
@ -210,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)
|
||||
@ -221,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
|
||||
}
|
||||
|
||||
/*
|
||||
@ -312,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 {
|
||||
|
@ -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"
|
||||
)
|
||||
|
@ -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 ""
|
||||
}
|
||||
|
@ -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 ""
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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 (
|
||||
|
@ -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"
|
||||
|
@ -2,7 +2,7 @@ package ws
|
||||
|
||||
import (
|
||||
"context"
|
||||
. "github.com/phyer/v5sdkgo/ws/wImpl"
|
||||
. "gitea.zjmud.xyz/phyer/v5sdkgo/ws/wImpl"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
10
ws/ws_op.go
10
ws/ws_op.go
@ -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"
|
||||
|
@ -1,7 +1,7 @@
|
||||
package ws
|
||||
|
||||
import (
|
||||
. "github.com/phyer/v5sdkgo/ws/wImpl"
|
||||
. "gitea.zjmud.xyz/phyer/v5sdkgo/ws/wImpl"
|
||||
)
|
||||
|
||||
/*
|
||||
|
@ -2,7 +2,7 @@ package ws
|
||||
|
||||
import (
|
||||
"errors"
|
||||
. "github.com/phyer/v5sdkgo/ws/wImpl"
|
||||
. "gitea.zjmud.xyz/phyer/v5sdkgo/ws/wImpl"
|
||||
)
|
||||
|
||||
/*
|
||||
|
@ -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"
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user