Merge pull request #356 from c9s/feature/kucoin

refactor: apply requestgen
This commit is contained in:
Yo-An Lin 2021-12-13 02:14:05 +08:00 committed by GitHub
commit d847d223e3
6 changed files with 428 additions and 155 deletions

1
.gitignore vendored
View File

@ -22,6 +22,7 @@
/.env.local /.env.local
/.env.*.local /.env.*.local
/.env.production
.DS_Store .DS_Store

View File

@ -0,0 +1,54 @@
// Code generated by "requestgen -type CancelAllOrderRequest"; DO NOT EDIT.
package kucoinapi
import (
"fmt"
"net/url"
)
func (r *CancelAllOrderRequest) Symbol(symbol string) *CancelAllOrderRequest {
r.symbol = &symbol
return r
}
func (r *CancelAllOrderRequest) TradeType(tradeType string) *CancelAllOrderRequest {
r.tradeType = &tradeType
return r
}
func (r *CancelAllOrderRequest) getParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
if r.symbol != nil {
symbol := *r.symbol
params["symbol"] = symbol
} else {
}
// check tradeType field -> json key tradeType
if r.tradeType != nil {
tradeType := *r.tradeType
params["tradeType"] = tradeType
} else {
}
return params, nil
}
func (r *CancelAllOrderRequest) getQuery() (url.Values, error) {
query := url.Values{}
params, err := r.getParameters()
if err != nil {
return query, err
}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}

View File

@ -0,0 +1,54 @@
// Code generated by "requestgen -type CancelOrderRequest"; DO NOT EDIT.
package kucoinapi
import (
"fmt"
"net/url"
)
func (c *CancelOrderRequest) OrderID(orderID string) *CancelOrderRequest {
c.orderID = &orderID
return c
}
func (c *CancelOrderRequest) ClientOrderID(clientOrderID string) *CancelOrderRequest {
c.clientOrderID = &clientOrderID
return c
}
func (c *CancelOrderRequest) getParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check orderID field -> json key orderID
if c.orderID != nil {
orderID := *c.orderID
params["orderID"] = orderID
} else {
}
// check clientOrderID field -> json key clientOrderID
if c.clientOrderID != nil {
clientOrderID := *c.clientOrderID
params["clientOrderID"] = clientOrderID
} else {
}
return params, nil
}
func (c *CancelOrderRequest) getQuery() (url.Values, error) {
query := url.Values{}
params, err := c.getParameters()
if err != nil {
return query, err
}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}

View File

@ -0,0 +1,141 @@
// Code generated by "requestgen -type ListOrdersRequest"; DO NOT EDIT.
package kucoinapi
import (
"fmt"
"net/url"
"strconv"
"time"
)
func (r *ListOrdersRequest) Status(status string) *ListOrdersRequest {
r.status = &status
return r
}
func (r *ListOrdersRequest) Symbol(symbol string) *ListOrdersRequest {
r.symbol = &symbol
return r
}
func (r *ListOrdersRequest) Side(side SideType) *ListOrdersRequest {
r.side = &side
return r
}
func (r *ListOrdersRequest) OrderType(orderType OrderType) *ListOrdersRequest {
r.orderType = &orderType
return r
}
func (r *ListOrdersRequest) TradeType(tradeType TradeType) *ListOrdersRequest {
r.tradeType = &tradeType
return r
}
func (r *ListOrdersRequest) StartAt(startAt time.Time) *ListOrdersRequest {
r.startAt = &startAt
return r
}
func (r *ListOrdersRequest) EndAt(endAt time.Time) *ListOrdersRequest {
r.endAt = &endAt
return r
}
func (r *ListOrdersRequest) getParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check status field -> json key status
if r.status != nil {
status := *r.status
switch status {
case "active", "done":
params["status"] = status
default:
return params, fmt.Errorf("status value %v is invalid", status)
}
params["status"] = status
} else {
}
// check symbol field -> json key symbol
if r.symbol != nil {
symbol := *r.symbol
params["symbol"] = symbol
} else {
}
// check side field -> json key side
if r.side != nil {
side := *r.side
switch side {
case "buy", "sell":
params["side"] = side
default:
return params, fmt.Errorf("side value %v is invalid", side)
}
params["side"] = side
} else {
}
// check orderType field -> json key type
if r.orderType != nil {
orderType := *r.orderType
params["type"] = orderType
} else {
}
// check tradeType field -> json key tradeType
if r.tradeType != nil {
tradeType := *r.tradeType
params["tradeType"] = tradeType
} else {
}
// check startAt field -> json key startAt
if r.startAt != nil {
startAt := *r.startAt
// convert time.Time to milliseconds time
params["startAt"] = strconv.FormatInt(startAt.UnixNano()/int64(time.Millisecond), 10)
} else {
}
// check endAt field -> json key endAt
if r.endAt != nil {
endAt := *r.endAt
// convert time.Time to milliseconds time
params["endAt"] = strconv.FormatInt(endAt.UnixNano()/int64(time.Millisecond), 10)
} else {
}
return params, nil
}
func (r *ListOrdersRequest) getQuery() (url.Values, error) {
query := url.Values{}
params, err := r.getParameters()
if err != nil {
return query, err
}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}

View File

@ -0,0 +1,140 @@
// Code generated by "requestgen -type PlaceOrderRequest"; DO NOT EDIT.
package kucoinapi
import (
"fmt"
"github.com/google/uuid"
"net/url"
)
func (r *PlaceOrderRequest) ClientOrderID(clientOrderID string) *PlaceOrderRequest {
r.clientOrderID = &clientOrderID
return r
}
func (r *PlaceOrderRequest) Symbol(symbol string) *PlaceOrderRequest {
r.symbol = symbol
return r
}
func (r *PlaceOrderRequest) Tag(tag string) *PlaceOrderRequest {
r.tag = &tag
return r
}
func (r *PlaceOrderRequest) Side(side SideType) *PlaceOrderRequest {
r.side = side
return r
}
func (r *PlaceOrderRequest) OrderType(orderType OrderType) *PlaceOrderRequest {
r.orderType = orderType
return r
}
func (r *PlaceOrderRequest) Size(size string) *PlaceOrderRequest {
r.size = size
return r
}
func (r *PlaceOrderRequest) Price(price string) *PlaceOrderRequest {
r.price = &price
return r
}
func (r *PlaceOrderRequest) TimeInForce(timeInForce TimeInForceType) *PlaceOrderRequest {
r.timeInForce = &timeInForce
return r
}
func (r *PlaceOrderRequest) getParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check clientOrderID field -> json key clientOid
if r.clientOrderID != nil {
clientOrderID := *r.clientOrderID
if len(clientOrderID) == 0 {
return params, fmt.Errorf("clientOid is required, empty string given")
}
params["clientOid"] = clientOrderID
} else {
clientOrderID := uuid.New().String()
params["clientOid"] = clientOrderID
}
// check symbol field -> json key symbol
symbol := r.symbol
if len(symbol) == 0 {
return params, fmt.Errorf("symbol is required, empty string given")
}
params["symbol"] = symbol
// check tag field -> json key tag
if r.tag != nil {
tag := *r.tag
params["tag"] = tag
} else {
}
// check side field -> json key side
side := r.side
params["side"] = side
// check orderType field -> json key ordType
orderType := r.orderType
params["ordType"] = orderType
// check size field -> json key size
size := r.size
if len(size) == 0 {
return params, fmt.Errorf("size is required, empty string given")
}
params["size"] = size
// check price field -> json key price
if r.price != nil {
price := *r.price
params["price"] = price
} else {
}
// check timeInForce field -> json key timeInForce
if r.timeInForce != nil {
timeInForce := *r.timeInForce
if len(timeInForce) == 0 {
return params, fmt.Errorf("timeInForce is required, empty string given")
}
params["timeInForce"] = timeInForce
} else {
}
return params, nil
}
func (r *PlaceOrderRequest) getQuery() (url.Values, error) {
query := url.Values{}
params, err := r.getParameters()
if err != nil {
return query, err
}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}

View File

@ -2,8 +2,6 @@ package kucoinapi
import ( import (
"context" "context"
"net/url"
"strconv"
"time" "time"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
@ -44,46 +42,23 @@ func (c *TradeService) NewCancelAllOrderRequest() *CancelAllOrderRequest {
} }
} }
//go:generate requestgen -type ListOrdersRequest
type ListOrdersRequest struct { type ListOrdersRequest struct {
client *RestClient client *RestClient
status *string status *string `param:"status" validValues:"active,done"`
symbol *string symbol *string `param:"symbol"`
side *SideType side *SideType `param:"side" validValues:"buy,sell"`
orderType *OrderType orderType *OrderType `param:"type"`
tradeType *TradeType tradeType *TradeType `param:"tradeType"`
startAt *time.Time startAt *time.Time `param:"startAt,milliseconds"`
endAt *time.Time endAt *time.Time `param:"endAt,milliseconds"`
}
func (r *ListOrdersRequest) Status(status string) {
r.status = &status
}
func (r *ListOrdersRequest) Symbol(symbol string) {
r.symbol = &symbol
}
func (r *ListOrdersRequest) Side(side SideType) {
r.side = &side
}
func (r *ListOrdersRequest) OrderType(orderType OrderType) {
r.orderType = &orderType
}
func (r *ListOrdersRequest) StartAt(startAt time.Time) {
r.startAt = &startAt
}
func (r *ListOrdersRequest) EndAt(endAt time.Time) {
r.endAt = &endAt
} }
type Order struct { type Order struct {
@ -123,36 +98,13 @@ type OrderListPage struct {
} }
func (r *ListOrdersRequest) Do(ctx context.Context) (*OrderListPage, error) { func (r *ListOrdersRequest) Do(ctx context.Context) (*OrderListPage, error) {
var params = url.Values{} params, err := r.getQuery()
if err != nil {
if r.status != nil { return nil, err
params["status"] = []string{*r.status}
} }
if r.symbol != nil { if !params.Has("tradeType") {
params["symbol"] = []string{*r.symbol} params.Add("tradeType", "TRADE")
}
if r.side != nil {
params["side"] = []string{string(*r.side)}
}
if r.orderType != nil {
params["type"] = []string{string(*r.orderType)}
}
if r.tradeType != nil {
params["tradeType"] = []string{string(*r.tradeType)}
} else {
params["tradeType"] = []string{"TRADE"}
}
if r.startAt != nil {
params["startAt"] = []string{strconv.FormatInt(r.startAt.UnixNano()/int64(time.Millisecond), 10)}
}
if r.endAt != nil {
params["endAt"] = []string{strconv.FormatInt(r.endAt.UnixNano()/int64(time.Millisecond), 10)}
} }
req, err := r.client.newAuthenticatedRequest("GET", "/api/v1/orders", params, nil) req, err := r.client.newAuthenticatedRequest("GET", "/api/v1/orders", params, nil)
@ -186,93 +138,29 @@ func (c *TradeService) NewListOrdersRequest() *ListOrdersRequest {
return &ListOrdersRequest{client: c.client} return &ListOrdersRequest{client: c.client}
} }
//go:generate requestgen -type PlaceOrderRequest
type PlaceOrderRequest struct { type PlaceOrderRequest struct {
client *RestClient client *RestClient
// A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 32 characters. // A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 32 characters.
clientOrderID *string clientOrderID *string `param:"clientOid,required" defaultValuer:"uuid()"`
symbol string symbol string `param:"symbol,required"`
// A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 8 characters. // A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 8 characters.
tag *string tag *string `param:"tag"`
// "buy" or "sell" // "buy" or "sell"
side SideType side SideType `param:"side"`
ordType OrderType orderType OrderType `param:"ordType"`
// limit order parameters // limit order parameters
size string size string `param:"size,required"`
price *string price *string `param:"price"`
timeInForce *TimeInForceType timeInForce *TimeInForceType `param:"timeInForce,required"`
}
func (r *PlaceOrderRequest) Symbol(symbol string) *PlaceOrderRequest {
r.symbol = symbol
return r
}
func (r *PlaceOrderRequest) ClientOrderID(clientOrderID string) *PlaceOrderRequest {
r.clientOrderID = &clientOrderID
return r
}
func (r *PlaceOrderRequest) Side(side SideType) *PlaceOrderRequest {
r.side = side
return r
}
func (r *PlaceOrderRequest) Size(size string) *PlaceOrderRequest {
r.size = size
return r
}
func (r *PlaceOrderRequest) Price(price string) *PlaceOrderRequest {
r.price = &price
return r
}
func (r *PlaceOrderRequest) TimeInForce(timeInForce TimeInForceType) *PlaceOrderRequest {
r.timeInForce = &timeInForce
return r
}
func (r *PlaceOrderRequest) OrderType(orderType OrderType) *PlaceOrderRequest {
r.ordType = orderType
return r
}
func (r *PlaceOrderRequest) getParameters() (map[string]interface{}, error) {
payload := map[string]interface{}{}
payload["symbol"] = r.symbol
if r.clientOrderID != nil {
payload["clientOid"] = r.clientOrderID
} else {
payload["clientOid"] = uuid.New().String()
}
if len(r.side) == 0 {
return nil, errors.New("order side is required")
}
payload["side"] = r.side
payload["type"] = r.ordType
payload["size"] = r.size
if r.price != nil {
payload["price"] = r.price
}
if r.timeInForce != nil {
payload["timeInForce"] = r.timeInForce
}
return payload, nil
} }
func (r *PlaceOrderRequest) Do(ctx context.Context) (*OrderResponse, error) { func (r *PlaceOrderRequest) Do(ctx context.Context) (*OrderResponse, error) {
@ -308,21 +196,12 @@ func (r *PlaceOrderRequest) Do(ctx context.Context) (*OrderResponse, error) {
return orderResponse.Data, nil return orderResponse.Data, nil
} }
//go:generate requestgen -type CancelOrderRequest
type CancelOrderRequest struct { type CancelOrderRequest struct {
client *RestClient client *RestClient
orderID *string orderID *string `param:"orderID"`
clientOrderID *string clientOrderID *string `param:"clientOrderID"`
}
func (r *CancelOrderRequest) OrderID(orderID string) *CancelOrderRequest {
r.orderID = &orderID
return r
}
func (r *CancelOrderRequest) ClientOrderID(clientOrderID string) *CancelOrderRequest {
r.clientOrderID = &clientOrderID
return r
} }
type CancelOrderResponse struct { type CancelOrderResponse struct {
@ -372,21 +251,21 @@ func (r *CancelOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, erro
return apiResponse.Data, nil return apiResponse.Data, nil
} }
//go:generate requestgen -type CancelAllOrderRequest
type CancelAllOrderRequest struct { type CancelAllOrderRequest struct {
client *RestClient client *RestClient
symbol *string symbol *string `param:"symbol"`
tradeType *string `param:"tradeType"`
// tradeType string
}
func (r *CancelAllOrderRequest) Symbol(symbol string) *CancelAllOrderRequest {
r.symbol = &symbol
return r
} }
func (r *CancelAllOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, error) { func (r *CancelAllOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, error) {
req, err := r.client.newAuthenticatedRequest("DELETE", "/api/v1/orders", nil, nil) params, err := r.getQuery()
if err != nil {
return nil, err
}
req, err := r.client.newAuthenticatedRequest("DELETE", "/api/v1/orders", params, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -441,6 +320,10 @@ func (r *BatchPlaceOrderRequest) Do(ctx context.Context) ([]OrderResponse, error
return nil, err return nil, err
} }
if _, ok := params["clientOid"]; !ok {
params["clientOid"] = uuid.New().String()
}
orderList = append(orderList, params) orderList = append(orderList, params)
} }