bbgo_origin/pkg/exchange/bitget/bitgetapi/client.go

128 lines
2.7 KiB
Go
Raw Normal View History

2023-05-16 09:14:23 +00:00
package bitgetapi
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"net/http"
"net/url"
"strings"
"time"
2023-05-17 06:26:25 +00:00
"github.com/c9s/requestgen"
2023-05-16 09:14:23 +00:00
"github.com/pkg/errors"
)
const defaultHTTPTimeout = time.Second * 15
const RestBaseURL = "https://api.bitget.com"
const PublicWebSocketURL = "wss://ws.bitget.com/spot/v1/stream"
const PrivateWebSocketURL = "wss://ws.bitget.com/spot/v1/stream"
type RestClient struct {
2023-05-17 06:26:25 +00:00
requestgen.BaseAPIClient
2023-05-16 09:14:23 +00:00
Key, Secret, Passphrase string
}
func NewClient() *RestClient {
u, err := url.Parse(RestBaseURL)
if err != nil {
panic(err)
}
return &RestClient{
2023-05-17 06:26:25 +00:00
BaseAPIClient: requestgen.BaseAPIClient{
BaseURL: u,
HttpClient: &http.Client{
Timeout: defaultHTTPTimeout,
},
2023-05-16 09:14:23 +00:00
},
}
}
func (c *RestClient) Auth(key, secret, passphrase string) {
c.Key = key
c.Secret = secret
c.Passphrase = passphrase
}
// newAuthenticatedRequest creates new http request for authenticated routes.
func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, method, refURL string, params url.Values, payload interface{}) (*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
if rel.RawQuery != "" {
path += "?" + rel.RawQuery
}
// set location to UTC so that it outputs "2020-12-08T09:08:57.715Z"
t := time.Now().In(time.UTC)
timestamp := t.Format("2006-01-02T15:04:05.999Z07:00")
2023-05-17 06:26:25 +00:00
body, err := castPayload(payload)
if err != nil {
return nil, err
2023-05-16 09:14:23 +00:00
}
signKey := timestamp + strings.ToUpper(method) + path + string(body)
2023-05-17 06:26:25 +00:00
signature := sign(signKey, c.Secret)
2023-05-16 09:14:23 +00:00
req, err := http.NewRequestWithContext(ctx, 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("ACCESS-KEY", c.Key)
req.Header.Add("ACCESS-SIGN", signature)
req.Header.Add("ACCESS-TIMESTAMP", timestamp)
req.Header.Add("ACCESS-PASSPHRASE", c.Passphrase)
return req, nil
}
2023-05-17 06:26:25 +00:00
func sign(payload string, secret string) string {
2023-05-16 09:14:23 +00:00
var sig = hmac.New(sha256.New, []byte(secret))
_, err := sig.Write([]byte(payload))
if err != nil {
return ""
}
return base64.StdEncoding.EncodeToString(sig.Sum(nil))
2023-05-17 06:26:25 +00:00
}
func castPayload(payload interface{}) ([]byte, error) {
if payload == nil {
return nil, nil
}
switch v := payload.(type) {
case string:
return []byte(v), nil
case []byte:
return v, nil
}
return json.Marshal(payload)
2023-05-16 09:14:23 +00:00
}