bbgo/pkg/exchange/okex/okexapi/place_order_request_requestgen.go

306 lines
7.0 KiB
Go
Raw Normal View History

// Code generated by "requestgen -method POST -responseType .APIResponse -responseDataField Data -url /api/v5/trade/order -type PlaceOrderRequest -responseDataType []OrderResponse"; DO NOT EDIT.
package okexapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (r *PlaceOrderRequest) InstrumentID(instrumentID string) *PlaceOrderRequest {
r.instrumentID = instrumentID
return r
}
func (r *PlaceOrderRequest) TradeMode(tradeMode TradeMode) *PlaceOrderRequest {
r.tradeMode = tradeMode
return r
}
func (r *PlaceOrderRequest) ClientOrderID(clientOrderID string) *PlaceOrderRequest {
r.clientOrderID = &clientOrderID
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) TargetCurrency(targetCurrency TargetCurrency) *PlaceOrderRequest {
r.targetCurrency = &targetCurrency
return r
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (r *PlaceOrderRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (r *PlaceOrderRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check instrumentID field -> json key instId
instrumentID := r.instrumentID
// assign parameter of instrumentID
params["instId"] = instrumentID
// check tradeMode field -> json key tdMode
tradeMode := r.tradeMode
// TEMPLATE check-valid-values
switch tradeMode {
case "cross", "isolated", "cash":
params["tdMode"] = tradeMode
default:
return nil, fmt.Errorf("tdMode value %v is invalid", tradeMode)
}
// END TEMPLATE check-valid-values
// assign parameter of tradeMode
params["tdMode"] = tradeMode
// check clientOrderID field -> json key clOrdId
if r.clientOrderID != nil {
clientOrderID := *r.clientOrderID
// assign parameter of clientOrderID
params["clOrdId"] = clientOrderID
} else {
}
// check tag field -> json key tag
if r.tag != nil {
tag := *r.tag
// assign parameter of tag
params["tag"] = tag
} else {
}
// check side field -> json key side
side := r.side
// TEMPLATE check-valid-values
switch side {
case "buy", "sell":
params["side"] = side
default:
return nil, fmt.Errorf("side value %v is invalid", side)
}
// END TEMPLATE check-valid-values
// assign parameter of side
params["side"] = side
// check orderType field -> json key ordType
orderType := r.orderType
// TEMPLATE check-valid-values
switch orderType {
case OrderTypeMarket, OrderTypeLimit, OrderTypePostOnly, OrderTypeFOK, OrderTypeIOC:
params["ordType"] = orderType
default:
return nil, fmt.Errorf("ordType value %v is invalid", orderType)
}
// END TEMPLATE check-valid-values
// assign parameter of orderType
params["ordType"] = orderType
// check size field -> json key sz
size := r.size
// assign parameter of size
params["sz"] = size
// check price field -> json key px
if r.price != nil {
price := *r.price
// assign parameter of price
params["px"] = price
} else {
}
// check targetCurrency field -> json key tgtCcy
if r.targetCurrency != nil {
targetCurrency := *r.targetCurrency
// TEMPLATE check-valid-values
switch targetCurrency {
case "quote_ccy", "base_ccy":
params["tgtCcy"] = targetCurrency
default:
return nil, fmt.Errorf("tgtCcy value %v is invalid", targetCurrency)
}
// END TEMPLATE check-valid-values
// assign parameter of targetCurrency
params["tgtCcy"] = targetCurrency
} else {
}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (r *PlaceOrderRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := r.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if r.isVarSlice(_v) {
r.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (r *PlaceOrderRequest) GetParametersJSON() ([]byte, error) {
params, err := r.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (r *PlaceOrderRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (r *PlaceOrderRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (r *PlaceOrderRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (r *PlaceOrderRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (r *PlaceOrderRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := r.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
// GetPath returns the request path of the API
func (r *PlaceOrderRequest) GetPath() string {
return "/api/v5/trade/order"
}
// Do generates the request object and send the request object to the API endpoint
func (r *PlaceOrderRequest) Do(ctx context.Context) ([]OrderResponse, error) {
params, err := r.GetParameters()
if err != nil {
return nil, err
}
query := url.Values{}
var apiURL string
apiURL = r.GetPath()
req, err := r.client.NewAuthenticatedRequest(ctx, "POST", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := r.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
type responseValidator interface {
Validate() error
}
validator, ok := interface{}(apiResponse).(responseValidator)
if ok {
if err := validator.Validate(); err != nil {
return nil, err
}
}
var data []OrderResponse
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return data, nil
}