apply requestgen for CancelOrderRequest and CancelAllOrderRequest

This commit is contained in:
c9s 2021-12-13 02:08:18 +08:00
parent 79056de057
commit e2937acb28
6 changed files with 83 additions and 35 deletions

View File

@ -177,6 +177,7 @@ func (r *restRequest) newAuthenticatedRequest(ctx context.Context) (*http.Reques
if err != nil { if err != nil {
return nil, err return nil, err
} }
ts := strconv.FormatInt(timestamp(), 10) ts := strconv.FormatInt(timestamp(), 10)
p := fmt.Sprintf("%s%s%s", ts, r.m, u.Path) p := fmt.Sprintf("%s%s%s", ts, r.m, u.Path)
if len(r.q) > 0 { if len(r.q) > 0 {

View File

@ -3,6 +3,7 @@
package kucoinapi package kucoinapi
import ( import (
"encoding/json"
"fmt" "fmt"
"net/url" "net/url"
) )
@ -17,31 +18,32 @@ func (r *CancelAllOrderRequest) TradeType(tradeType string) *CancelAllOrderReque
return r return r
} }
func (r *CancelAllOrderRequest) getParameters() (map[string]interface{}, error) { func (r *CancelAllOrderRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{} var params = map[string]interface{}{}
// check symbol field -> json key symbol // check symbol field -> json key symbol
if r.symbol != nil { if r.symbol != nil {
symbol := *r.symbol symbol := *r.symbol
// assign parameter of symbol
params["symbol"] = symbol params["symbol"] = symbol
} else {
} }
// check tradeType field -> json key tradeType // check tradeType field -> json key tradeType
if r.tradeType != nil { if r.tradeType != nil {
tradeType := *r.tradeType tradeType := *r.tradeType
// assign parameter of tradeType
params["tradeType"] = tradeType params["tradeType"] = tradeType
} else {
} }
return params, nil return params, nil
} }
func (r *CancelAllOrderRequest) getQuery() (url.Values, error) { func (r *CancelAllOrderRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{} query := url.Values{}
params, err := r.getParameters() params, err := r.GetParameters()
if err != nil { if err != nil {
return query, err return query, err
} }
@ -52,3 +54,12 @@ func (r *CancelAllOrderRequest) getQuery() (url.Values, error) {
return query, nil return query, nil
} }
func (r *CancelAllOrderRequest) GetParametersJSON() ([]byte, error) {
params, err := r.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}

View File

@ -3,6 +3,7 @@
package kucoinapi package kucoinapi
import ( import (
"encoding/json"
"fmt" "fmt"
"net/url" "net/url"
) )
@ -17,31 +18,32 @@ func (c *CancelOrderRequest) ClientOrderID(clientOrderID string) *CancelOrderReq
return c return c
} }
func (c *CancelOrderRequest) getParameters() (map[string]interface{}, error) { func (c *CancelOrderRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{} var params = map[string]interface{}{}
// check orderID field -> json key orderID // check orderID field -> json key orderID
if c.orderID != nil { if c.orderID != nil {
orderID := *c.orderID orderID := *c.orderID
// assign parameter of orderID
params["orderID"] = orderID params["orderID"] = orderID
} else {
} }
// check clientOrderID field -> json key clientOrderID // check clientOrderID field -> json key clientOrderID
if c.clientOrderID != nil { if c.clientOrderID != nil {
clientOrderID := *c.clientOrderID clientOrderID := *c.clientOrderID
// assign parameter of clientOrderID
params["clientOrderID"] = clientOrderID params["clientOrderID"] = clientOrderID
} else {
} }
return params, nil return params, nil
} }
func (c *CancelOrderRequest) getQuery() (url.Values, error) { func (c *CancelOrderRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{} query := url.Values{}
params, err := c.getParameters() params, err := c.GetParameters()
if err != nil { if err != nil {
return query, err return query, err
} }
@ -52,3 +54,12 @@ func (c *CancelOrderRequest) getQuery() (url.Values, error) {
return query, nil return query, nil
} }
func (c *CancelOrderRequest) GetParametersJSON() ([]byte, error) {
params, err := c.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}

View File

@ -3,6 +3,7 @@
package kucoinapi package kucoinapi
import ( import (
"encoding/json"
"fmt" "fmt"
"net/url" "net/url"
"strconv" "strconv"
@ -44,7 +45,7 @@ func (r *ListOrdersRequest) EndAt(endAt time.Time) *ListOrdersRequest {
return r return r
} }
func (r *ListOrdersRequest) getParameters() (map[string]interface{}, error) { func (r *ListOrdersRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{} var params = map[string]interface{}{}
// check status field -> json key status // check status field -> json key status
@ -60,16 +61,16 @@ func (r *ListOrdersRequest) getParameters() (map[string]interface{}, error) {
} }
// assign parameter of status
params["status"] = status params["status"] = status
} else {
} }
// check symbol field -> json key symbol // check symbol field -> json key symbol
if r.symbol != nil { if r.symbol != nil {
symbol := *r.symbol symbol := *r.symbol
// assign parameter of symbol
params["symbol"] = symbol params["symbol"] = symbol
} else {
} }
// check side field -> json key side // check side field -> json key side
@ -85,50 +86,51 @@ func (r *ListOrdersRequest) getParameters() (map[string]interface{}, error) {
} }
// assign parameter of side
params["side"] = side params["side"] = side
} else {
} }
// check orderType field -> json key type // check orderType field -> json key type
if r.orderType != nil { if r.orderType != nil {
orderType := *r.orderType orderType := *r.orderType
// assign parameter of orderType
params["type"] = orderType params["type"] = orderType
} else {
} }
// check tradeType field -> json key tradeType // check tradeType field -> json key tradeType
if r.tradeType != nil { if r.tradeType != nil {
tradeType := *r.tradeType tradeType := *r.tradeType
// assign parameter of tradeType
params["tradeType"] = tradeType params["tradeType"] = tradeType
} else {
} }
// check startAt field -> json key startAt // check startAt field -> json key startAt
if r.startAt != nil { if r.startAt != nil {
startAt := *r.startAt startAt := *r.startAt
// assign parameter of startAt
// convert time.Time to milliseconds time // convert time.Time to milliseconds time
params["startAt"] = strconv.FormatInt(startAt.UnixNano()/int64(time.Millisecond), 10) params["startAt"] = strconv.FormatInt(startAt.UnixNano()/int64(time.Millisecond), 10)
} else {
} }
// check endAt field -> json key endAt // check endAt field -> json key endAt
if r.endAt != nil { if r.endAt != nil {
endAt := *r.endAt endAt := *r.endAt
// assign parameter of endAt
// convert time.Time to milliseconds time // convert time.Time to milliseconds time
params["endAt"] = strconv.FormatInt(endAt.UnixNano()/int64(time.Millisecond), 10) params["endAt"] = strconv.FormatInt(endAt.UnixNano()/int64(time.Millisecond), 10)
} else {
} }
return params, nil return params, nil
} }
func (r *ListOrdersRequest) getQuery() (url.Values, error) { func (r *ListOrdersRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{} query := url.Values{}
params, err := r.getParameters() params, err := r.GetParameters()
if err != nil { if err != nil {
return query, err return query, err
} }
@ -139,3 +141,12 @@ func (r *ListOrdersRequest) getQuery() (url.Values, error) {
return query, nil return query, nil
} }
func (r *ListOrdersRequest) GetParametersJSON() ([]byte, error) {
params, err := r.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}

View File

@ -3,6 +3,7 @@
package kucoinapi package kucoinapi
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/google/uuid" "github.com/google/uuid"
"net/url" "net/url"
@ -48,7 +49,7 @@ func (r *PlaceOrderRequest) TimeInForce(timeInForce TimeInForceType) *PlaceOrder
return r return r
} }
func (r *PlaceOrderRequest) getParameters() (map[string]interface{}, error) { func (r *PlaceOrderRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{} var params = map[string]interface{}{}
// check clientOrderID field -> json key clientOid // check clientOrderID field -> json key clientOid
@ -59,10 +60,14 @@ func (r *PlaceOrderRequest) getParameters() (map[string]interface{}, error) {
return params, fmt.Errorf("clientOid is required, empty string given") return params, fmt.Errorf("clientOid is required, empty string given")
} }
// assign parameter of clientOrderID
params["clientOid"] = clientOrderID params["clientOid"] = clientOrderID
} else { } else {
// assign default of clientOrderID
clientOrderID := uuid.New().String() clientOrderID := uuid.New().String()
// assign parameter of clientOrderID
params["clientOid"] = clientOrderID params["clientOid"] = clientOrderID
} }
@ -73,24 +78,27 @@ func (r *PlaceOrderRequest) getParameters() (map[string]interface{}, error) {
return params, fmt.Errorf("symbol is required, empty string given") return params, fmt.Errorf("symbol is required, empty string given")
} }
// assign parameter of symbol
params["symbol"] = symbol params["symbol"] = symbol
// check tag field -> json key tag // check tag field -> json key tag
if r.tag != nil { if r.tag != nil {
tag := *r.tag tag := *r.tag
// assign parameter of tag
params["tag"] = tag params["tag"] = tag
} else {
} }
// check side field -> json key side // check side field -> json key side
side := r.side side := r.side
// assign parameter of side
params["side"] = side params["side"] = side
// check orderType field -> json key ordType // check orderType field -> json key ordType
orderType := r.orderType orderType := r.orderType
// assign parameter of orderType
params["ordType"] = orderType params["ordType"] = orderType
// check size field -> json key size // check size field -> json key size
@ -100,14 +108,15 @@ func (r *PlaceOrderRequest) getParameters() (map[string]interface{}, error) {
return params, fmt.Errorf("size is required, empty string given") return params, fmt.Errorf("size is required, empty string given")
} }
// assign parameter of size
params["size"] = size params["size"] = size
// check price field -> json key price // check price field -> json key price
if r.price != nil { if r.price != nil {
price := *r.price price := *r.price
// assign parameter of price
params["price"] = price params["price"] = price
} else {
} }
// check timeInForce field -> json key timeInForce // check timeInForce field -> json key timeInForce
@ -118,16 +127,17 @@ func (r *PlaceOrderRequest) getParameters() (map[string]interface{}, error) {
return params, fmt.Errorf("timeInForce is required, empty string given") return params, fmt.Errorf("timeInForce is required, empty string given")
} }
// assign parameter of timeInForce
params["timeInForce"] = timeInForce params["timeInForce"] = timeInForce
} else {
} }
return params, nil return params, nil
} }
func (r *PlaceOrderRequest) getQuery() (url.Values, error) { func (r *PlaceOrderRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{} query := url.Values{}
params, err := r.getParameters() params, err := r.GetParameters()
if err != nil { if err != nil {
return query, err return query, err
} }
@ -138,3 +148,12 @@ func (r *PlaceOrderRequest) getQuery() (url.Values, error) {
return query, nil return query, nil
} }
func (r *PlaceOrderRequest) GetParametersJSON() ([]byte, error) {
params, err := r.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}

View File

@ -6,7 +6,6 @@ import (
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/google/uuid"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -98,7 +97,7 @@ type OrderListPage struct {
} }
func (r *ListOrdersRequest) Do(ctx context.Context) (*OrderListPage, error) { func (r *ListOrdersRequest) Do(ctx context.Context) (*OrderListPage, error) {
params, err := r.getQuery() params, err := r.GetParametersQuery()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -164,7 +163,7 @@ type PlaceOrderRequest struct {
} }
func (r *PlaceOrderRequest) Do(ctx context.Context) (*OrderResponse, error) { func (r *PlaceOrderRequest) Do(ctx context.Context) (*OrderResponse, error) {
payload, err := r.getParameters() payload, err := r.GetParameters()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -260,7 +259,7 @@ type CancelAllOrderRequest struct {
} }
func (r *CancelAllOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, error) { func (r *CancelAllOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, error) {
params, err := r.getQuery() params, err := r.GetParametersQuery()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -315,15 +314,11 @@ func (r *BatchPlaceOrderRequest) Add(reqs ...*PlaceOrderRequest) *BatchPlaceOrde
func (r *BatchPlaceOrderRequest) Do(ctx context.Context) ([]OrderResponse, error) { func (r *BatchPlaceOrderRequest) Do(ctx context.Context) ([]OrderResponse, error) {
var orderList []map[string]interface{} var orderList []map[string]interface{}
for _, req := range r.reqs { for _, req := range r.reqs {
params, err := req.getParameters() params, err := req.GetParameters()
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, ok := params["clientOid"]; !ok {
params["clientOid"] = uuid.New().String()
}
orderList = append(orderList, params) orderList = append(orderList, params)
} }