kucoin: refactor bullet url code

This commit is contained in:
c9s 2021-12-23 00:41:10 +08:00
parent 6cbccc9a3f
commit a4c9aea6c6
2 changed files with 19 additions and 7 deletions

View File

@ -3,7 +3,6 @@ package main
import ( import (
"context" "context"
"errors" "errors"
"net/url"
"os" "os"
"os/signal" "os/signal"
"time" "time"
@ -59,7 +58,7 @@ var websocketCmd = &cobra.Command{
} }
u, err := url.Parse(bullet.InstanceServers[0].Endpoint) u, err := bullet.URL()
if err != nil { if err != nil {
return err return err
} }
@ -67,12 +66,7 @@ var websocketCmd = &cobra.Command{
interrupt := make(chan os.Signal, 1) interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt) signal.Notify(interrupt, os.Interrupt)
params := url.Values{}
params.Add("token", bullet.Token)
u.RawQuery = params.Encode()
logrus.Infof("connecting %s", u.String()) logrus.Infof("connecting %s", u.String())
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil { if err != nil {
return err return err

View File

@ -6,6 +6,7 @@ import (
"net/url" "net/url"
"github.com/c9s/bbgo/pkg/util" "github.com/c9s/bbgo/pkg/util"
"github.com/pkg/errors"
) )
// ApiClient defines the request builder method and request method for the API service // ApiClient defines the request builder method and request method for the API service
@ -48,6 +49,23 @@ type Bullet struct {
Token string `json:"token"` Token string `json:"token"`
} }
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
}
func (r *GetPublicBulletRequest) Do(ctx context.Context) (*Bullet, error) { func (r *GetPublicBulletRequest) Do(ctx context.Context) (*Bullet, error) {
req, err := r.client.NewRequest("POST", "/api/v1/bullet-public", nil, nil) req, err := r.client.NewRequest("POST", "/api/v1/bullet-public", nil, nil)
if err != nil { if err != nil {