Compare commits

..

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

View File

@ -44,49 +44,10 @@ 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 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
Code string `json:"code"`
Msg string `json:"msg"`
Data []map[string]interface{} `json:"data"`
}
/*
@ -157,8 +118,8 @@ func (this *RESTAPI) SetTimeOut(timeout time.Duration) *RESTAPI {
return this
}
// GetCandles 获取蜡烛图数据
func (this *RESTAPI) GetCandles(ctx context.Context, uri string, param *map[string]interface{}) (res ResponseData, err error) {
// GET请求
func (this *RESTAPI) Get(ctx context.Context, uri string, param *map[string]interface{}) (res *RESTAPIResult, err error) {
this.Method = GET
this.Uri = uri
@ -172,7 +133,7 @@ func (this *RESTAPI) GetCandles(ctx context.Context, uri string, param *map[stri
}
// 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.Uri = uri
@ -186,16 +147,11 @@ func (this *RESTAPI) Post(ctx context.Context, uri string, param *map[string]int
return this.Run(ctx)
}
func (this *RESTAPI) Run(ctx context.Context) (ResponseData, error) {
var res *RESTAPIResult
var err error
func (this *RESTAPI) Run(ctx context.Context) (res *RESTAPIResult, err error) {
if this.ApiKeyInfo == nil {
err = errors.New("APIKey不可为空")
if err != nil {
return nil, err
}
return res.V5Response, nil
return
}
procStart := time.Now()
@ -212,7 +168,7 @@ func (this *RESTAPI) Run(ctx context.Context) (ResponseData, error) {
uri, body, err := this.GenReqInfo()
if err != nil {
return nil, err
return
}
url := this.EndPoint + uri
@ -221,7 +177,7 @@ func (this *RESTAPI) Run(ctx context.Context) (ResponseData, error) {
req, err := http.NewRequest(this.Method, url, bodyBuf)
if err != nil {
return nil, err
return
}
res = &RESTAPIResult{
@ -235,7 +191,7 @@ func (this *RESTAPI) Run(ctx context.Context) (ResponseData, error) {
//log.Println("preHash:", preHash)
sign, err := utils.HmacSha256Base64Signer(preHash, this.ApiKeyInfo.SecKey)
if err != nil {
return nil, err
return
}
//log.Println("sign:", sign)
headStr := this.SetHeaders(req, timestamp, sign)
@ -245,7 +201,7 @@ func (this *RESTAPI) Run(ctx context.Context) (ResponseData, error) {
resp, err := client.Do(req)
if err != nil {
fmt.Println("请求失败!", err)
return nil, err
return
}
defer resp.Body.Close()
@ -254,7 +210,7 @@ func (this *RESTAPI) Run(ctx context.Context) (ResponseData, error) {
resBuff, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("获取请求结果失败!", err)
return nil, err
return
}
res.Body = string(resBuff)
@ -265,12 +221,12 @@ func (this *RESTAPI) Run(ctx context.Context) (ResponseData, error) {
err = json.Unmarshal(resBuff, &v5rsp)
if err != nil {
fmt.Println("解析v5返回失败", err)
return nil, err
return
}
res.V5Response = v5rsp
return nil, err
return
}
/*