v2_rest.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package yu_bitget
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/json"
  6. yu_base64 "gogs.qqck.cn/s/tools/base64"
  7. yu_curl "gogs.qqck.cn/s/tools/curl"
  8. yu_fast "gogs.qqck.cn/s/tools/fast"
  9. yu_json "gogs.qqck.cn/s/tools/json"
  10. "hash"
  11. "time"
  12. )
  13. type V2Rest struct {
  14. host string
  15. key struct {
  16. APIKey string
  17. SecretKey string
  18. Passphrase string
  19. SecretKey_hash hash.Hash
  20. }
  21. curl *yu_curl.Request
  22. }
  23. func NewV2Rest() (t *V2Rest) {
  24. t = &V2Rest{}
  25. t.host = "https://api.bitget.com"
  26. t.curl = yu_curl.NewRequest()
  27. // t.curl.SetHeader("Accept-Language", "zh-CN,zh;q=0.9")
  28. t.curl.SetHeader("Locale", "zh-CN,zh;q=0.9")
  29. return
  30. }
  31. func (t *V2Rest) Close() {
  32. t.curl.Close()
  33. }
  34. // SetProxy 设置代理
  35. //
  36. // @Description:
  37. // @param proxy 格式 ProxyType://user:pass@host:port,ProxyType: http, socks4, socks4a, socks5, socks5h,设置为 "" 以取消代理
  38. func (t *V2Rest) SetProxy(proxy string) *V2Rest {
  39. t.curl.SetProxy(proxy)
  40. return t
  41. }
  42. func (t *V2Rest) SetKey(APIKey string, SecretKey string, Passphrase string) *V2Rest {
  43. t.key.APIKey, t.key.SecretKey, t.key.Passphrase = APIKey, SecretKey, Passphrase
  44. t.key.SecretKey_hash = hmac.New(sha256.New, yu_fast.S2B(t.key.SecretKey))
  45. t.curl.SetHeader("ACCESS-KEY", t.key.APIKey)
  46. t.curl.SetHeader("ACCESS-PASSPHRASE", t.key.Passphrase)
  47. return t
  48. }
  49. func (t *V2Rest) request(method string, path string, post any, resp any) bool {
  50. j_timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z")
  51. t.curl.SetHeader("ACCESS-TIMESTAMP", j_timestamp)
  52. switch method {
  53. case "GET":
  54. t.getAccessSign(j_timestamp, method, path, "")
  55. t.curl.Get(t.host + path)
  56. case "POST":
  57. j_json, j_err := json.Marshal(post)
  58. if j_err != nil {
  59. return false
  60. }
  61. t.getAccessSign(j_timestamp, method, path, yu_fast.B2S(j_json))
  62. t.curl.SetContentType("application/json")
  63. t.curl.Post(t.host+path, j_json)
  64. }
  65. if t.curl.RespStatusCode() != 200 {
  66. return false
  67. }
  68. if resp == nil {
  69. return true
  70. }
  71. // Code string `json:"code"`
  72. // Msg string `json:"msg"`
  73. // RequestTime int64 `json:"requestTime"`
  74. // Data any `json:"data"`
  75. j_data := yu_json.GetBytes(t.curl.RespBody(), "data").String()
  76. if j_data == "" {
  77. return false
  78. }
  79. return json.Unmarshal(yu_fast.S2B(j_data), resp) == nil
  80. }
  81. func (t *V2Rest) getAccessSign(timestamp string, method string, path string, body string) {
  82. if t.key.APIKey == "" || t.key.SecretKey == "" {
  83. t.curl.SetHeader("ACCESS-SIGN", "")
  84. return
  85. }
  86. // OK-ACCESS-SIGN的请求头是对timestamp + method + requestPath + body字符串(+表示字符串连接),以及SecretKey,使用HMAC SHA256方法加密,通过Base-64编码输出而得到的。
  87. //
  88. // 如:sign=CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(timestamp + 'GET' + '/api/v5/account/balance?ccy=BTC', SecretKey))
  89. //
  90. // 其中,timestamp的值与OK-ACCESS-TIMESTAMP请求头相同,为ISO格式,如2020-12-08T09:08:57.715Z。
  91. //
  92. // method是请求方法,字母全部大写:GET/POST。
  93. //
  94. // requestPath是请求接口路径。如:/api/v5/account/balance
  95. //
  96. // body是指请求主体的字符串,如果请求没有主体(通常为GET请求)则body可省略。如:{"instId":"BTC-USDT","lever":"5","mgnMode":"isolated"}
  97. // t.key.SecretKey_hash.Reset()
  98. j_data := timestamp + method + path + body
  99. t.key.SecretKey_hash.Reset()
  100. t.key.SecretKey_hash.Write(yu_fast.S2B(j_data))
  101. t.curl.SetHeader("ACCESS-SIGN", yu_base64.Std.Encode2S(t.key.SecretKey_hash.Sum(nil)))
  102. }