okex: rewrite okex api request with requestgen

This commit is contained in:
c9s 2021-12-13 14:52:14 +08:00
parent e2937acb28
commit 74811abb36
3 changed files with 249 additions and 101 deletions

View File

@ -0,0 +1,76 @@
// Code generated by "requestgen -type CancelOrderRequest"; DO NOT EDIT.
package okexapi
import (
"encoding/json"
"fmt"
"net/url"
)
func (c *CancelOrderRequest) InstrumentID(instrumentID string) *CancelOrderRequest {
c.instrumentID = instrumentID
return c
}
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 instrumentID field -> json key instId
instrumentID := c.instrumentID
// assign parameter of instrumentID
params["instId"] = instrumentID
// check orderID field -> json key ordId
if c.orderID != nil {
orderID := *c.orderID
// assign parameter of orderID
params["ordId"] = orderID
}
// check clientOrderID field -> json key clOrdId
if c.clientOrderID != nil {
clientOrderID := *c.clientOrderID
// assign parameter of clientOrderID
params["clOrdId"] = clientOrderID
}
return params, nil
}
func (c *CancelOrderRequest) GetParametersQuery() (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
}
func (c *CancelOrderRequest) GetParametersJSON() ([]byte, error) {
params, err := c.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}

View File

@ -0,0 +1,151 @@
// Code generated by "requestgen -type PlaceOrderRequest"; DO NOT EDIT.
package okexapi
import (
"encoding/json"
"fmt"
"net/url"
)
func (p *PlaceOrderRequest) InstrumentID(instrumentID string) *PlaceOrderRequest {
p.instrumentID = instrumentID
return p
}
func (p *PlaceOrderRequest) TradeMode(tradeMode string) *PlaceOrderRequest {
p.tradeMode = tradeMode
return p
}
func (p *PlaceOrderRequest) ClientOrderID(clientOrderID string) *PlaceOrderRequest {
p.clientOrderID = &clientOrderID
return p
}
func (p *PlaceOrderRequest) Tag(tag string) *PlaceOrderRequest {
p.tag = &tag
return p
}
func (p *PlaceOrderRequest) Side(side SideType) *PlaceOrderRequest {
p.side = side
return p
}
func (p *PlaceOrderRequest) OrderType(orderType OrderType) *PlaceOrderRequest {
p.orderType = orderType
return p
}
func (p *PlaceOrderRequest) Quantity(quantity string) *PlaceOrderRequest {
p.quantity = quantity
return p
}
func (p *PlaceOrderRequest) Price(price string) *PlaceOrderRequest {
p.price = &price
return p
}
func (p *PlaceOrderRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check instrumentID field -> json key instId
instrumentID := p.instrumentID
// assign parameter of instrumentID
params["instId"] = instrumentID
// check tradeMode field -> json key tdMode
tradeMode := p.tradeMode
switch tradeMode {
case "cross", "isolated", "cash":
params["tdMode"] = tradeMode
default:
return params, fmt.Errorf("tdMode value %v is invalid", tradeMode)
}
// assign parameter of tradeMode
params["tdMode"] = tradeMode
// check clientOrderID field -> json key clOrdId
if p.clientOrderID != nil {
clientOrderID := *p.clientOrderID
// assign parameter of clientOrderID
params["clOrdId"] = clientOrderID
}
// check tag field -> json key tag
if p.tag != nil {
tag := *p.tag
// assign parameter of tag
params["tag"] = tag
}
// check side field -> json key side
side := p.side
switch side {
case "buy", "sell":
params["side"] = side
default:
return params, fmt.Errorf("side value %v is invalid", side)
}
// assign parameter of side
params["side"] = side
// check orderType field -> json key ordType
orderType := p.orderType
// assign parameter of orderType
params["ordType"] = orderType
// check quantity field -> json key sz
quantity := p.quantity
// assign parameter of quantity
params["sz"] = quantity
// check price field -> json key px
if p.price != nil {
price := *p.price
// assign parameter of price
params["px"] = price
}
return params, nil
}
func (p *PlaceOrderRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := p.GetParameters()
if err != nil {
return query, err
}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}
func (p *PlaceOrderRequest) GetParametersJSON() ([]byte, error) {
params, err := p.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}

View File

@ -64,96 +64,37 @@ func (c *TradeService) NewGetTransactionDetailsRequest() *GetTransactionDetailsR
} }
} }
//go:generate requestgen -type PlaceOrderRequest
type PlaceOrderRequest struct { type PlaceOrderRequest struct {
client *RestClient client *RestClient
instId string instrumentID string `param:"instId"`
// tdMode // tdMode
// margin mode: "cross", "isolated" // margin mode: "cross", "isolated"
// non-margin mode cash // non-margin mode cash
tdMode string tradeMode string `param:"tdMode" validValues:"cross,isolated,cash"`
// 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:"clOrdId"`
// 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" validValues:"buy,sell"`
ordType OrderType orderType OrderType `param:"ordType"`
// sz Quantity quantity string `param:"sz"`
sz string
// price // price
px *string price *string `param:"px"`
}
func (r *PlaceOrderRequest) InstrumentID(instID string) *PlaceOrderRequest {
r.instId = instID
return r
}
func (r *PlaceOrderRequest) TradeMode(mode string) *PlaceOrderRequest {
r.tdMode = mode
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) Quantity(quantity string) *PlaceOrderRequest {
r.sz = quantity
return r
}
func (r *PlaceOrderRequest) Price(price string) *PlaceOrderRequest {
r.px = &price
return r
}
func (r *PlaceOrderRequest) OrderType(orderType OrderType) *PlaceOrderRequest {
r.ordType = orderType
return r
} }
func (r *PlaceOrderRequest) Parameters() map[string]interface{} { func (r *PlaceOrderRequest) Parameters() map[string]interface{} {
payload := map[string]interface{}{} params, _ := r.GetParameters()
return params
payload["instId"] = r.instId
if r.tdMode == "" {
payload["tdMode"] = "cash"
} else {
payload["tdMode"] = r.tdMode
}
if r.clientOrderID != nil {
payload["clOrdId"] = r.clientOrderID
}
payload["side"] = r.side
payload["ordType"] = r.ordType
payload["sz"] = r.sz
if r.px != nil {
payload["px"] = r.px
}
if r.tag != nil {
payload["tag"] = r.tag
}
return payload
} }
func (r *PlaceOrderRequest) Do(ctx context.Context) (*OrderResponse, error) { func (r *PlaceOrderRequest) Do(ctx context.Context) (*OrderResponse, error) {
@ -184,47 +125,27 @@ func (r *PlaceOrderRequest) Do(ctx context.Context) (*OrderResponse, error) {
return &orderResponse.Data[0], nil return &orderResponse.Data[0], nil
} }
//go:generate requestgen -type CancelOrderRequest
type CancelOrderRequest struct { type CancelOrderRequest struct {
client *RestClient client *RestClient
instId string instrumentID string `param:"instId"`
ordId *string orderID *string `param:"ordId"`
clOrdId *string clientOrderID *string `param:"clOrdId"`
}
func (r *CancelOrderRequest) InstrumentID(instId string) *CancelOrderRequest {
r.instId = instId
return r
}
func (r *CancelOrderRequest) OrderID(orderID string) *CancelOrderRequest {
r.ordId = &orderID
return r
}
func (r *CancelOrderRequest) ClientOrderID(clientOrderID string) *CancelOrderRequest {
r.clOrdId = &clientOrderID
return r
} }
func (r *CancelOrderRequest) Parameters() map[string]interface{} { func (r *CancelOrderRequest) Parameters() map[string]interface{} {
var payload = map[string]interface{}{ payload, _ := r.GetParameters()
"instId": r.instId,
}
if r.ordId != nil {
payload["ordId"] = r.ordId
} else if r.clOrdId != nil {
payload["clOrdId"] = r.clOrdId
}
return payload return payload
} }
func (r *CancelOrderRequest) Do(ctx context.Context) ([]OrderResponse, error) { func (r *CancelOrderRequest) Do(ctx context.Context) ([]OrderResponse, error) {
var payload = r.Parameters() payload, err := r.GetParameters()
if err != nil {
return nil, err
}
if r.ordId == nil && r.clOrdId != nil { if r.clientOrderID == nil && r.orderID != nil {
return nil, errors.New("either orderID or clientOrderID is required for canceling order") return nil, errors.New("either orderID or clientOrderID is required for canceling order")
} }