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

167 lines
3.5 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"
2023-05-17 08:23:39 +00:00
"strconv"
2023-05-16 09:14:23 +00:00
"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
2023-05-16 09:14:23 +00:00
}
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
2023-05-16 09:14:23 +00:00
}
// newAuthenticatedRequest creates new http request for authenticated routes.
2023-11-10 23:59:44 +00:00
func (c *RestClient) NewAuthenticatedRequest(
ctx context.Context, method, refURL string, params url.Values, payload interface{},
) (*http.Request, error) {
if len(c.key) == 0 {
2023-05-16 09:14:23 +00:00
return nil, errors.New("empty api key")
}
if len(c.secret) == 0 {
2023-05-16 09:14:23 +00:00
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
}
2023-05-17 08:23:39 +00:00
// See https://bitgetlimited.github.io/apidoc/en/spot/#signature
// sign(
// timestamp +
// method.toUpperCase() +
// requestPath + "?" + queryString +
// body **string
// )
// (+ means string concat) encrypt by **HMAC SHA256 **algorithm, and encode the encrypted result through **BASE64.
2023-05-16 09:14:23 +00:00
// set location to UTC so that it outputs "2020-12-08T09:08:57.715Z"
t := time.Now().In(time.UTC)
2023-05-17 08:23:39 +00:00
timestamp := strconv.FormatInt(t.UnixMilli(), 10)
2023-05-16 09:14:23 +00: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)
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)
2023-05-16 09:14:23 +00:00
req.Header.Add("ACCESS-SIGN", signature)
req.Header.Add("ACCESS-TIMESTAMP", timestamp)
req.Header.Add("ACCESS-PASSPHRASE", c.passphrase)
2023-11-10 23:59:44 +00:00
req.Header.Add("X-CHANNEL-API-CODE", "7575765263")
2023-05-16 09:14:23 +00:00
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
}
2023-05-17 08:39:10 +00:00
/*
sample:
{
"code": "00000",
"msg": "success",
"data": {
"user_id": "714229403",
"inviter_id": "682221498",
"ips": "172.23.88.91",
"authorities": [
"trade",
"readonly"
],
"parentId":"566624801",
"trader":false
}
}
*/
type APIResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
Data json.RawMessage `json:"data"`
}