2021-12-22 15:35:39 +00:00
|
|
|
package kucoinapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
2021-12-22 16:41:10 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-12-22 15:35:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ApiClient defines the request builder method and request method for the API service
|
|
|
|
type ApiClient interface {
|
2021-12-22 15:52:44 +00:00
|
|
|
// NewAuthenticatedRequest builds up the http request for authentication-required endpoints
|
|
|
|
NewAuthenticatedRequest(method, refURL string, params url.Values, payload interface{}) (*http.Request, error)
|
2021-12-22 15:35:39 +00:00
|
|
|
|
2021-12-22 15:52:44 +00:00
|
|
|
// NewRequest builds up the http request for public endpoints
|
|
|
|
NewRequest(method, refURL string, params url.Values, body []byte) (*http.Request, error)
|
2021-12-22 15:35:39 +00:00
|
|
|
|
2021-12-22 15:52:44 +00:00
|
|
|
// SendRequest sends the request object to the api gateway
|
|
|
|
SendRequest(req *http.Request) (*util.Response, error)
|
2021-12-22 15:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2021-12-22 16:41:10 +00:00
|
|
|
func (b *Bullet) URL() (*url.URL, error) {
|
|
|
|
if len(b.InstanceServers) == 0 {
|
|
|
|
return nil, errors.New("InstanceServers is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(b.InstanceServers[0].Endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
params := url.Values{}
|
|
|
|
params.Add("token", b.Token)
|
|
|
|
|
|
|
|
u.RawQuery = params.Encode()
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2021-12-22 15:35:39 +00:00
|
|
|
func (r *GetPublicBulletRequest) Do(ctx context.Context) (*Bullet, error) {
|
2021-12-22 15:52:44 +00:00
|
|
|
req, err := r.client.NewRequest("POST", "/api/v1/bullet-public", nil, nil)
|
2021-12-22 15:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-22 15:52:44 +00:00
|
|
|
response, err := r.client.SendRequest(req)
|
2021-12-22 15:35:39 +00:00
|
|
|
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) {
|
2021-12-22 15:52:44 +00:00
|
|
|
req, err := r.client.NewAuthenticatedRequest("POST", "/api/v1/bullet-private", nil, nil)
|
2021-12-22 15:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-22 15:52:44 +00:00
|
|
|
response, err := r.client.SendRequest(req)
|
2021-12-22 15:35:39 +00:00
|
|
|
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
|
|
|
|
}
|