kucoin: add bullet service

This commit is contained in:
c9s 2021-12-22 23:35:39 +08:00
parent 7f92588883
commit 2230b484a8
2 changed files with 104 additions and 1 deletions

View File

@ -0,0 +1,102 @@
package kucoinapi
import (
"context"
"net/http"
"net/url"
"github.com/c9s/bbgo/pkg/util"
)
// ApiClient defines the request builder method and request method for the API service
type ApiClient interface {
// newAuthenticatedRequest builds up the http request for authentication-required endpoints
newAuthenticatedRequest(method, refURL string, params url.Values, payload interface{}) (*http.Request, error)
// newRequest builds up the http request for public endpoints
newRequest(method, refURL string, params url.Values, body []byte) (*http.Request, error)
// sendRequest sends the request object to the api gateway
sendRequest(req *http.Request) (*util.Response, error)
}
type BulletService struct {
client *RestClient
}
func (s *BulletService) NewGetPublicBulletRequest() *GetPublicBulletRequest {
return &GetPublicBulletRequest{client: s.client}
}
func (s *BulletService) NewGetPrivateBulletRequest() *GetPrivateBulletRequest {
return &GetPrivateBulletRequest{client: s.client}
}
//go:generate requestgen -type GetPublicBulletRequest
type GetPublicBulletRequest struct {
client ApiClient
}
type Bullet struct {
InstanceServers []struct {
Endpoint string `json:"endpoint"`
Protocol string `json:"protocol"`
Encrypt bool `json:"encrypt"`
PingInterval int `json:"pingInterval"`
PingTimeout int `json:"pingTimeout"`
} `json:"instanceServers"`
Token string `json:"token"`
}
func (r *GetPublicBulletRequest) Do(ctx context.Context) (*Bullet, error) {
req, err := r.client.newRequest("POST", "/api/v1/bullet-public", nil, nil)
if err != nil {
return nil, err
}
response, err := r.client.sendRequest(req)
if err != nil {
return nil, err
}
var apiResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
Data *Bullet `json:"data"`
}
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
return apiResponse.Data, nil
}
//go:generate requestgen -type GetPrivateBulletRequest
type GetPrivateBulletRequest struct {
client ApiClient
}
func (r *GetPrivateBulletRequest) Do(ctx context.Context) (*Bullet, error) {
req, err := r.client.newAuthenticatedRequest("POST", "/api/v1/bullet-private", nil, nil)
if err != nil {
return nil, err
}
response, err := r.client.sendRequest(req)
if err != nil {
return nil, err
}
var apiResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
Data *Bullet `json:"data"`
}
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
return apiResponse.Data, nil
}

View File

@ -31,6 +31,7 @@ type RestClient struct {
AccountService *AccountService
MarketDataService *MarketDataService
TradeService *TradeService
BulletService *BulletService
}
func NewClient() *RestClient {
@ -50,6 +51,7 @@ func NewClient() *RestClient {
client.AccountService = &AccountService{client: client}
client.MarketDataService = &MarketDataService{client: client}
client.TradeService = &TradeService{client: client}
client.BulletService = &BulletService{client: client}
return client
}
@ -138,7 +140,6 @@ func (c *RestClient) newAuthenticatedRequest(method, refURL string, params url.V
return req, nil
}
func (c *RestClient) attachAuthHeaders(req *http.Request, method string, path string, body []byte) {
// Set location to UTC so that it outputs "2020-12-08T09:08:57.715Z"
t := time.Now().In(time.UTC)