mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
okex: implement base rest client
This commit is contained in:
parent
36071d6649
commit
fe269fd93d
12
pkg/exchange/okex/exchange.go
Normal file
12
pkg/exchange/okex/exchange.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
package okex
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var log = logrus.WithFields(logrus.Fields{
|
||||
"exchange": "okex",
|
||||
})
|
||||
|
||||
type Exchange struct {
|
||||
}
|
117
pkg/exchange/okex/okexapi/client.go
Normal file
117
pkg/exchange/okex/okexapi/client.go
Normal file
|
@ -0,0 +1,117 @@
|
|||
package okexapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const RestBaseURL = "https://www.okex.com/"
|
||||
const PublicWebSocketURL = "wss://ws.okex.com:8443/ws/v5/public"
|
||||
const PrivateWebSocketURL = "wss://ws.okex.com:8443/ws/v5/private"
|
||||
|
||||
type RestClient struct {
|
||||
BaseURL *url.URL
|
||||
|
||||
Key, Secret, Passphrase string
|
||||
}
|
||||
|
||||
func NewClient() *RestClient {
|
||||
u, err := url.Parse(RestBaseURL)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &RestClient{
|
||||
BaseURL: u,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RestClient) Auth(key, secret, passphrase string) {
|
||||
c.Key = key
|
||||
c.Secret = secret
|
||||
c.Passphrase = passphrase
|
||||
}
|
||||
|
||||
// NewRequest create new API request. Relative url can be provided in refURL.
|
||||
func (c *RestClient) newRequest(method, refURL string, params url.Values, body []byte) (*http.Request, error) {
|
||||
rel, err := url.Parse(refURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if params != nil {
|
||||
rel.RawQuery = params.Encode()
|
||||
}
|
||||
|
||||
pathURL := c.BaseURL.ResolveReference(rel)
|
||||
|
||||
req, err := http.NewRequest(method, pathURL.String(), bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// newAuthenticatedRequest creates new http request for authenticated routes.
|
||||
func (c *RestClient) newAuthenticatedRequest(method, refURL string, params url.Values) (*http.Request, error) {
|
||||
if len(c.Key) == 0 {
|
||||
return nil, errors.New("empty api key")
|
||||
}
|
||||
|
||||
if len(c.Secret) == 0 {
|
||||
return nil, errors.New("empty api secret")
|
||||
}
|
||||
|
||||
rel, err := url.Parse(refURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if params != nil {
|
||||
rel.RawQuery = params.Encode()
|
||||
}
|
||||
|
||||
pathURL := c.BaseURL.ResolveReference(rel)
|
||||
path := pathURL.Path
|
||||
|
||||
// 2020-12-08T09:08:57.715Z
|
||||
t := time.Now()
|
||||
timestamp := t.Format("2006-01-02T15:04:05.999Z07:00")
|
||||
payload := timestamp + strings.ToUpper(method) + path
|
||||
sign := signPayload(payload, c.Secret)
|
||||
|
||||
var body []byte
|
||||
req, err := http.NewRequest(method, pathURL.String(), bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("OK-ACCESS-KEY", c.Key)
|
||||
req.Header.Add("OK-ACCESS-SIGN", sign)
|
||||
req.Header.Add("OK-ACCESS-TIMESTAMP", timestamp)
|
||||
req.Header.Add("OK-ACCESS-PASSPHRASE", c.Passphrase)
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func signPayload(payload string, secret string) string {
|
||||
var sig = hmac.New(sha256.New, []byte(secret))
|
||||
_, err := sig.Write([]byte(payload))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return base64.StdEncoding.EncodeToString(sig.Sum(nil))
|
||||
// return hex.EncodeToString(sig.Sum(nil))
|
||||
}
|
Loading…
Reference in New Issue
Block a user