加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
client_service.go 4.02 KB
一键复制 编辑 原始数据 按行查看 历史
zhouqiusheng 提交于 2022-10-31 13:32 . 定义常量
package okex
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"gitee.com/zqs118/go-okx/common"
"io/ioutil"
"net/http"
"strings"
"time"
)
type RestApiResult struct {
Url string `json:"url"`
Param string `json:"param"`
Header string `json:"header"`
Code int `json:"code"`
Body string `json:"body"` // 原始返回信息
V5Response OkexApiResponse `json:"v5Response"` // okexV5返回的数据
ReqUsedTime time.Duration `json:"reqUsedTime"`
TotalUsedTime time.Duration `json:"totalUsedTime"`
}
// OkexApiResponse 解析结果
type OkexApiResponse struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data []map[string]interface{} `json:"data"`
}
func (r *Client) Run() (res *RestApiResult, err error) {
procStart := time.Now()
defer func() {
if res != nil {
res.TotalUsedTime = time.Since(procStart)
}
}()
client := &http.Client{
Timeout: r.timeout,
}
path, body, err := r.genReqInfo()
if err != nil {
return
}
url := string(r.BaseURL) + path
bodyBuf := new(bytes.Buffer)
bodyBuf.ReadFrom(strings.NewReader(body))
req, err := http.NewRequest(string(r.Method), url, bodyBuf)
if err != nil {
return
}
res = &RestApiResult{
Url: url,
Param: body,
}
// Sign and set request headers
timestamp := common.IsoTime()
preHash := common.PreHashString(timestamp, r.Method, path, body)
sign, err := common.HmacSha256Base64Signer(preHash, r.Config.SecretKey)
if err != nil {
return
}
headStr := r.setHeaders(req, timestamp, sign)
res.Header = headStr
resp, err := client.Do(req)
if err != nil {
fmt.Println("请求失败!", err)
return
}
defer resp.Body.Close()
res.ReqUsedTime = time.Since(procStart)
resBuff, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("获取请求结果失败!", err)
return
}
res.Body = string(resBuff)
res.Code = resp.StatusCode
// 解析结果
var v5rsp OkexApiResponse
err = json.Unmarshal(resBuff, &v5rsp)
if err != nil {
fmt.Println("解析v5返回失败!", err)
return
}
res.V5Response = v5rsp
return
}
/*
GenReqInfo
生成请求对应的参数
*/
func (r *Client) genReqInfo() (path string, body string, err error) {
path = r.Url
switch r.Method {
case common.RequestMethodGET:
getParam := []string{}
if len(r.Param) == 0 {
return
}
for k, v := range r.Param {
getParam = append(getParam, fmt.Sprintf("%v=%v", k, v))
}
path = path + "?" + strings.Join(getParam, "&")
case common.RequestMethodPOST:
var rawBody []byte
rawBody, err = json.Marshal(r.Param)
if err != nil {
return
}
body = string(rawBody)
default:
err = errors.New("request type unknown!")
return
}
return
}
/*
SetHeaders
Set http request headers:
Accept: application/json
Content-Type: application/json; charset=UTF-8 (default)
Cookie: locale=en_US (English)
OK-ACCESS-KEY: (Your setting)
OK-ACCESS-SIGN: (Use your setting, auto sign and add)
OK-ACCESS-TIMESTAMP: (Auto add)
OK-ACCESS-PASSPHRASE: Your setting
*/
func (r *Client) setHeaders(request *http.Request, timestamp string, sign string) (header string) {
request.Header.Add(Accept, ApplicationJson)
header += Accept + ":" + ApplicationJson + "\n"
request.Header.Add(ContentType, ApplicationJsonUtf8)
header += ContentType + ":" + ApplicationJsonUtf8 + "\n"
request.Header.Add(Cookie, Locale+English)
header += Cookie + ":" + Locale + English + "\n"
request.Header.Add(OkAccessKey, r.Config.ApiKey)
header += OkAccessKey + ":" + r.Config.ApiKey + "\n"
request.Header.Add(OkAccessSign, sign)
header += OkAccessSign + ":" + sign + "\n"
request.Header.Add(OkAccessTimeStamp, timestamp)
header += OkAccessTimeStamp + ":" + timestamp + "\n"
request.Header.Add(OkAccessPassphrase, r.Config.PassPhrase)
header += OkAccessPassphrase + ":" + r.Config.PassPhrase + "\n"
//模拟盘交易标记
if r.Config.IsSimulate {
request.Header.Add(XSimulatedTrading, "1")
header += XSimulatedTrading + ":1" + "\n"
}
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化