mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
add max v3 api
This commit is contained in:
parent
a66bae47fe
commit
c6ede883ce
|
@ -353,43 +353,3 @@ type CreateOrderRequest struct {
|
|||
func (s *OrderService) NewCreateOrderRequest() *CreateOrderRequest {
|
||||
return &CreateOrderRequest{client: s.client}
|
||||
}
|
||||
|
||||
//go:generate PostRequest -url "/api/v3/wallet/:walletType/orders" -type V3CreateOrderRequest -responseType .Order
|
||||
type V3CreateOrderRequest struct {
|
||||
client requestgen.AuthenticatedAPIClient
|
||||
|
||||
walletType string `param:"walletType,slug,required"`
|
||||
market string `param:"market,required"`
|
||||
side string `param:"side,required"`
|
||||
volume string `param:"volume,required"`
|
||||
orderType string `param:"ord_type"`
|
||||
|
||||
price *string `param:"price"`
|
||||
stopPrice *string `param:"stop_price"`
|
||||
clientOrderID *string `param:"client_oid"`
|
||||
groupID *string `param:"group_id"`
|
||||
}
|
||||
|
||||
func (s *OrderService) NewV3CreateOrderRequest() *V3CreateOrderRequest {
|
||||
return &V3CreateOrderRequest{client: s.client}
|
||||
}
|
||||
|
||||
//go:generate GetRequest -url "/api/v3/wallet/:walletType/orders" -type V3GetOrderRequest -responseType .Order
|
||||
type V3GetOrderRequest struct {
|
||||
client requestgen.AuthenticatedAPIClient
|
||||
|
||||
walletType string `param:"walletType,slug,required"`
|
||||
market string `param:"market,required"`
|
||||
side string `param:"side,required"`
|
||||
volume string `param:"volume,required"`
|
||||
orderType string `param:"ord_type"`
|
||||
|
||||
price *string `param:"price"`
|
||||
stopPrice *string `param:"stop_price"`
|
||||
clientOrderID *string `param:"client_oid"`
|
||||
groupID *string `param:"group_id"`
|
||||
}
|
||||
|
||||
func (s *OrderService) NewV3GetOrderRequest() *V3GetOrderRequest {
|
||||
return &V3GetOrderRequest{client: s.client}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package max
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
|
@ -9,11 +8,9 @@ import (
|
|||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
@ -40,18 +37,11 @@ const (
|
|||
TimestampSince = 1535760000
|
||||
)
|
||||
|
||||
var debugRequestDump = false
|
||||
var debugMaxRequestPayload = false
|
||||
var addUserAgentHeader = true
|
||||
|
||||
var httpTransportMaxIdleConnsPerHost = http.DefaultMaxIdleConnsPerHost
|
||||
var httpTransportMaxIdleConns = 100
|
||||
var httpTransportIdleConnTimeout = 90 * time.Second
|
||||
|
||||
func init() {
|
||||
debugMaxRequestPayload, _ = util.GetEnvVarBool("DEBUG_MAX_REQUEST_PAYLOAD")
|
||||
debugRequestDump, _ = util.GetEnvVarBool("DEBUG_MAX_REQUEST")
|
||||
addUserAgentHeader, _ = util.GetEnvVarBool("DISABLE_MAX_USER_AGENT_HEADER")
|
||||
|
||||
if val, ok := util.GetEnvVarInt("HTTP_TRANSPORT_MAX_IDLE_CONNS_PER_HOST"); ok {
|
||||
httpTransportMaxIdleConnsPerHost = val
|
||||
|
@ -81,9 +71,7 @@ var serverTimestamp = time.Now().Unix()
|
|||
var reqCount int64 = 1
|
||||
|
||||
type RestClient struct {
|
||||
client *http.Client
|
||||
|
||||
BaseURL *url.URL
|
||||
requestgen.BaseAPIClient
|
||||
|
||||
// Authentication
|
||||
APIKey string
|
||||
|
@ -108,8 +96,10 @@ func NewRestClientWithHttpClient(baseURL string, httpClient *http.Client) *RestC
|
|||
}
|
||||
|
||||
var client = &RestClient{
|
||||
client: httpClient,
|
||||
BaseURL: u,
|
||||
BaseAPIClient: requestgen.BaseAPIClient{
|
||||
HttpClient: httpClient,
|
||||
BaseURL: u,
|
||||
},
|
||||
}
|
||||
|
||||
client.AccountService = &AccountService{client}
|
||||
|
@ -175,45 +165,20 @@ func (c *RestClient) getNonce() int64 {
|
|||
return (seconds+timeOffset)*1000 - 1 + int64(math.Mod(float64(rc), 1000.0))
|
||||
}
|
||||
|
||||
// NewRequest create new API request. Relative url can be provided in refURL.
|
||||
func (c *RestClient) NewRequest(ctx context.Context, method string, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
|
||||
rel, err := url.Parse(refURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if params != nil {
|
||||
rel.RawQuery = params.Encode()
|
||||
}
|
||||
|
||||
var req *http.Request
|
||||
u := c.BaseURL.ResolveReference(rel)
|
||||
|
||||
body, err := castPayload(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err = http.NewRequest(method, u.String(), bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
if addUserAgentHeader {
|
||||
req.Header.Add("User-Agent", UserAgent)
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, m string, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
|
||||
return c.newAuthenticatedRequest(ctx, m, refURL, params, payload, nil)
|
||||
}
|
||||
|
||||
// newAuthenticatedRequest creates new http request for authenticated routes.
|
||||
func (c *RestClient) newAuthenticatedRequest(ctx context.Context, m string, refURL string, params url.Values, data interface{}, rel *url.URL) (*http.Request, error) {
|
||||
if len(c.APIKey) == 0 {
|
||||
return nil, errors.New("empty api key")
|
||||
}
|
||||
|
||||
if len(c.APISecret) == 0 {
|
||||
return nil, errors.New("empty api secret")
|
||||
}
|
||||
|
||||
var err error
|
||||
if rel == nil {
|
||||
rel, err = url.Parse(refURL)
|
||||
|
@ -223,22 +188,13 @@ func (c *RestClient) newAuthenticatedRequest(ctx context.Context, m string, refU
|
|||
}
|
||||
|
||||
var p []byte
|
||||
var payload map[string]interface{}
|
||||
var payload = map[string]interface{}{
|
||||
"nonce": c.getNonce(),
|
||||
"path": c.BaseURL.ResolveReference(rel).Path,
|
||||
}
|
||||
|
||||
switch d := data.(type) {
|
||||
|
||||
case nil:
|
||||
payload = map[string]interface{}{
|
||||
"nonce": c.getNonce(),
|
||||
"path": c.BaseURL.ResolveReference(rel).Path,
|
||||
}
|
||||
|
||||
case map[string]interface{}:
|
||||
payload = map[string]interface{}{
|
||||
"nonce": c.getNonce(),
|
||||
"path": c.BaseURL.ResolveReference(rel).Path,
|
||||
}
|
||||
|
||||
for k, v := range d {
|
||||
payload[k] = v
|
||||
}
|
||||
|
@ -258,22 +214,6 @@ func (c *RestClient) newAuthenticatedRequest(ctx context.Context, m string, refU
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if debugMaxRequestPayload {
|
||||
log.Infof("request payload: %s", p)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(c.APIKey) == 0 {
|
||||
return nil, errors.New("empty api key")
|
||||
}
|
||||
|
||||
if len(c.APISecret) == 0 {
|
||||
return nil, errors.New("empty api secret")
|
||||
}
|
||||
|
||||
req, err := c.NewRequest(ctx, m, refURL, params, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -286,95 +226,16 @@ func (c *RestClient) newAuthenticatedRequest(ctx context.Context, m string, refU
|
|||
req.Header.Add("X-MAX-PAYLOAD", encoded)
|
||||
req.Header.Add("X-MAX-SIGNATURE", signPayload(encoded, c.APISecret))
|
||||
|
||||
if debugRequestDump {
|
||||
dump, err2 := httputil.DumpRequestOut(req, true)
|
||||
if err2 != nil {
|
||||
log.Errorf("dump request error: %v", err2)
|
||||
} else {
|
||||
fmt.Printf("REQUEST:\n%s", dump)
|
||||
}
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func signPayload(payload string, secret string) string {
|
||||
var sig = hmac.New(sha256.New, []byte(secret))
|
||||
_, err := sig.Write([]byte(payload))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return hex.EncodeToString(sig.Sum(nil))
|
||||
}
|
||||
|
||||
func (c *RestClient) Do(req *http.Request) (resp *http.Response, err error) {
|
||||
return c.client.Do(req)
|
||||
}
|
||||
|
||||
// SendRequest sends the request to the API server and handle the response
|
||||
func (c *RestClient) SendRequest(req *http.Request) (*requestgen.Response, error) {
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// newResponse reads the response body and return a new Response object
|
||||
response, err := requestgen.NewResponse(resp)
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
|
||||
// Check error, if there is an error, return the ErrorResponse struct type
|
||||
if response.IsError() {
|
||||
errorResponse, err := ToErrorResponse(response)
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
return response, errorResponse
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (c *RestClient) sendAuthenticatedRequest(m string, refURL string, data map[string]interface{}) (*requestgen.Response, error) {
|
||||
req, err := c.newAuthenticatedRequest(nil, m, refURL, nil, data, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err := c.SendRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response, err
|
||||
}
|
||||
|
||||
// get sends GET http request to the api endpoint, the urlPath must start with a slash '/'
|
||||
func (c *RestClient) get(urlPath string, values url.Values) ([]byte, error) {
|
||||
var reqURL = c.BaseURL.String() + urlPath
|
||||
|
||||
// Create request
|
||||
req, err := http.NewRequest("GET", reqURL, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not init request: %s", err.Error())
|
||||
}
|
||||
|
||||
req.URL.RawQuery = values.Encode()
|
||||
req.Header.Add("User-Agent", UserAgent)
|
||||
|
||||
// Execute request
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not execute request: %s", err.Error())
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Load request
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not read response: %s", err.Error())
|
||||
}
|
||||
|
||||
return body, nil
|
||||
return c.SendRequest(req)
|
||||
}
|
||||
|
||||
// ErrorResponse is the custom error type that is returned if the API returns an
|
||||
|
@ -439,3 +300,12 @@ func castPayload(payload interface{}) ([]byte, error) {
|
|||
body, err := json.Marshal(payload)
|
||||
return body, err
|
||||
}
|
||||
|
||||
func signPayload(payload string, secret string) string {
|
||||
var sig = hmac.New(sha256.New, []byte(secret))
|
||||
_, err := sig.Write([]byte(payload))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return hex.EncodeToString(sig.Sum(nil))
|
||||
}
|
||||
|
|
270
pkg/exchange/max/maxapi/v3/create_order_request_requestgen.go
Normal file
270
pkg/exchange/max/maxapi/v3/create_order_request_requestgen.go
Normal file
|
@ -0,0 +1,270 @@
|
|||
// Code generated by "requestgen -method POST -url /api/v3/wallet/:walletType/orders -type CreateOrderRequest -responseType .Order -debug"; DO NOT EDIT.
|
||||
|
||||
package v3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/c9s/bbgo/pkg/exchange/max/maxapi"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func (c *CreateOrderRequest) Market(market string) *CreateOrderRequest {
|
||||
c.market = market
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *CreateOrderRequest) Side(side string) *CreateOrderRequest {
|
||||
c.side = side
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *CreateOrderRequest) Volume(volume string) *CreateOrderRequest {
|
||||
c.volume = volume
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *CreateOrderRequest) OrderType(orderType string) *CreateOrderRequest {
|
||||
c.orderType = orderType
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *CreateOrderRequest) Price(price string) *CreateOrderRequest {
|
||||
c.price = &price
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *CreateOrderRequest) StopPrice(stopPrice string) *CreateOrderRequest {
|
||||
c.stopPrice = &stopPrice
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *CreateOrderRequest) ClientOrderID(clientOrderID string) *CreateOrderRequest {
|
||||
c.clientOrderID = &clientOrderID
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *CreateOrderRequest) GroupID(groupID string) *CreateOrderRequest {
|
||||
c.groupID = &groupID
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *CreateOrderRequest) WalletType(walletType string) *CreateOrderRequest {
|
||||
c.walletType = walletType
|
||||
return c
|
||||
}
|
||||
|
||||
// GetQueryParameters builds and checks the query parameters and returns url.Values
|
||||
func (c *CreateOrderRequest) 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 (c *CreateOrderRequest) GetParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
// check market field -> json key market
|
||||
market := c.market
|
||||
|
||||
// TEMPLATE check-required
|
||||
if len(market) == 0 {
|
||||
return nil, fmt.Errorf("market is required, empty string given")
|
||||
}
|
||||
// END TEMPLATE check-required
|
||||
|
||||
// assign parameter of market
|
||||
params["market"] = market
|
||||
// check side field -> json key side
|
||||
side := c.side
|
||||
|
||||
// TEMPLATE check-required
|
||||
if len(side) == 0 {
|
||||
return nil, fmt.Errorf("side is required, empty string given")
|
||||
}
|
||||
// END TEMPLATE check-required
|
||||
|
||||
// assign parameter of side
|
||||
params["side"] = side
|
||||
// check volume field -> json key volume
|
||||
volume := c.volume
|
||||
|
||||
// TEMPLATE check-required
|
||||
if len(volume) == 0 {
|
||||
return nil, fmt.Errorf("volume is required, empty string given")
|
||||
}
|
||||
// END TEMPLATE check-required
|
||||
|
||||
// assign parameter of volume
|
||||
params["volume"] = volume
|
||||
// check orderType field -> json key ord_type
|
||||
orderType := c.orderType
|
||||
|
||||
// assign parameter of orderType
|
||||
params["ord_type"] = orderType
|
||||
// check price field -> json key price
|
||||
if c.price != nil {
|
||||
price := *c.price
|
||||
|
||||
// assign parameter of price
|
||||
params["price"] = price
|
||||
} else {
|
||||
}
|
||||
// check stopPrice field -> json key stop_price
|
||||
if c.stopPrice != nil {
|
||||
stopPrice := *c.stopPrice
|
||||
|
||||
// assign parameter of stopPrice
|
||||
params["stop_price"] = stopPrice
|
||||
} else {
|
||||
}
|
||||
// check clientOrderID field -> json key client_oid
|
||||
if c.clientOrderID != nil {
|
||||
clientOrderID := *c.clientOrderID
|
||||
|
||||
// assign parameter of clientOrderID
|
||||
params["client_oid"] = clientOrderID
|
||||
} else {
|
||||
}
|
||||
// check groupID field -> json key group_id
|
||||
if c.groupID != nil {
|
||||
groupID := *c.groupID
|
||||
|
||||
// assign parameter of groupID
|
||||
params["group_id"] = groupID
|
||||
} else {
|
||||
}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
|
||||
func (c *CreateOrderRequest) GetParametersQuery() (url.Values, error) {
|
||||
query := url.Values{}
|
||||
|
||||
params, err := c.GetParameters()
|
||||
if err != nil {
|
||||
return query, err
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
if c.isVarSlice(_v) {
|
||||
c.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 (c *CreateOrderRequest) GetParametersJSON() ([]byte, error) {
|
||||
params, err := c.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 (c *CreateOrderRequest) GetSlugParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
// check walletType field -> json key walletType
|
||||
walletType := c.walletType
|
||||
|
||||
// TEMPLATE check-required
|
||||
if len(walletType) == 0 {
|
||||
return nil, fmt.Errorf("walletType is required, empty string given")
|
||||
}
|
||||
// END TEMPLATE check-required
|
||||
|
||||
// assign parameter of walletType
|
||||
params["walletType"] = walletType
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func (c *CreateOrderRequest) 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 (c *CreateOrderRequest) 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 (c *CreateOrderRequest) isVarSlice(_v interface{}) bool {
|
||||
rt := reflect.TypeOf(_v)
|
||||
switch rt.Kind() {
|
||||
case reflect.Slice:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *CreateOrderRequest) GetSlugsMap() (map[string]string, error) {
|
||||
slugs := map[string]string{}
|
||||
params, err := c.GetSlugParameters()
|
||||
if err != nil {
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
slugs[_k] = fmt.Sprintf("%v", _v)
|
||||
}
|
||||
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
func (c *CreateOrderRequest) Do(ctx context.Context) (*max.Order, error) {
|
||||
|
||||
params, err := c.GetParameters()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query := url.Values{}
|
||||
|
||||
apiURL := "/api/v3/wallet/:walletType/orders"
|
||||
slugs, err := c.GetSlugsMap()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
apiURL = c.applySlugsToUrl(apiURL, slugs)
|
||||
|
||||
req, err := c.client.NewAuthenticatedRequest(ctx, "POST", apiURL, query, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := c.client.SendRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var apiResponse max.Order
|
||||
if err := response.DecodeJSON(&apiResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &apiResponse, nil
|
||||
}
|
|
@ -1,63 +1,64 @@
|
|||
// Code generated by "requestgen -method GET -url /api/v3/wallet/:walletType/orders -type V3CreateOrderRequest -responseType .Order"; DO NOT EDIT.
|
||||
// Code generated by "requestgen -method GET -url /api/v3/wallet/:walletType/orders -type GetOrderRequest -responseType .Order"; DO NOT EDIT.
|
||||
|
||||
package max
|
||||
package v3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/c9s/bbgo/pkg/exchange/max/maxapi"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func (v *V3CreateOrderRequest) Market(market string) *V3CreateOrderRequest {
|
||||
v.market = market
|
||||
return v
|
||||
func (g *GetOrderRequest) Market(market string) *GetOrderRequest {
|
||||
g.market = market
|
||||
return g
|
||||
}
|
||||
|
||||
func (v *V3CreateOrderRequest) Side(side string) *V3CreateOrderRequest {
|
||||
v.side = side
|
||||
return v
|
||||
func (g *GetOrderRequest) Side(side string) *GetOrderRequest {
|
||||
g.side = side
|
||||
return g
|
||||
}
|
||||
|
||||
func (v *V3CreateOrderRequest) Volume(volume string) *V3CreateOrderRequest {
|
||||
v.volume = volume
|
||||
return v
|
||||
func (g *GetOrderRequest) Volume(volume string) *GetOrderRequest {
|
||||
g.volume = volume
|
||||
return g
|
||||
}
|
||||
|
||||
func (v *V3CreateOrderRequest) OrderType(orderType string) *V3CreateOrderRequest {
|
||||
v.orderType = orderType
|
||||
return v
|
||||
func (g *GetOrderRequest) OrderType(orderType string) *GetOrderRequest {
|
||||
g.orderType = orderType
|
||||
return g
|
||||
}
|
||||
|
||||
func (v *V3CreateOrderRequest) Price(price string) *V3CreateOrderRequest {
|
||||
v.price = &price
|
||||
return v
|
||||
func (g *GetOrderRequest) Price(price string) *GetOrderRequest {
|
||||
g.price = &price
|
||||
return g
|
||||
}
|
||||
|
||||
func (v *V3CreateOrderRequest) StopPrice(stopPrice string) *V3CreateOrderRequest {
|
||||
v.stopPrice = &stopPrice
|
||||
return v
|
||||
func (g *GetOrderRequest) StopPrice(stopPrice string) *GetOrderRequest {
|
||||
g.stopPrice = &stopPrice
|
||||
return g
|
||||
}
|
||||
|
||||
func (v *V3CreateOrderRequest) ClientOrderID(clientOrderID string) *V3CreateOrderRequest {
|
||||
v.clientOrderID = &clientOrderID
|
||||
return v
|
||||
func (g *GetOrderRequest) ClientOrderID(clientOrderID string) *GetOrderRequest {
|
||||
g.clientOrderID = &clientOrderID
|
||||
return g
|
||||
}
|
||||
|
||||
func (v *V3CreateOrderRequest) GroupID(groupID string) *V3CreateOrderRequest {
|
||||
v.groupID = &groupID
|
||||
return v
|
||||
func (g *GetOrderRequest) GroupID(groupID string) *GetOrderRequest {
|
||||
g.groupID = &groupID
|
||||
return g
|
||||
}
|
||||
|
||||
func (v *V3CreateOrderRequest) WalletType(walletType string) *V3CreateOrderRequest {
|
||||
v.walletType = walletType
|
||||
return v
|
||||
func (g *GetOrderRequest) WalletType(walletType string) *GetOrderRequest {
|
||||
g.walletType = walletType
|
||||
return g
|
||||
}
|
||||
|
||||
// GetQueryParameters builds and checks the query parameters and returns url.Values
|
||||
func (v *V3CreateOrderRequest) GetQueryParameters() (url.Values, error) {
|
||||
func (g *GetOrderRequest) GetQueryParameters() (url.Values, error) {
|
||||
var params = map[string]interface{}{}
|
||||
|
||||
query := url.Values{}
|
||||
|
@ -69,10 +70,10 @@ func (v *V3CreateOrderRequest) GetQueryParameters() (url.Values, error) {
|
|||
}
|
||||
|
||||
// GetParameters builds and checks the parameters and return the result in a map object
|
||||
func (v *V3CreateOrderRequest) GetParameters() (map[string]interface{}, error) {
|
||||
func (g *GetOrderRequest) GetParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
// check market field -> json key market
|
||||
market := v.market
|
||||
market := g.market
|
||||
|
||||
// TEMPLATE check-required
|
||||
if len(market) == 0 {
|
||||
|
@ -83,7 +84,7 @@ func (v *V3CreateOrderRequest) GetParameters() (map[string]interface{}, error) {
|
|||
// assign parameter of market
|
||||
params["market"] = market
|
||||
// check side field -> json key side
|
||||
side := v.side
|
||||
side := g.side
|
||||
|
||||
// TEMPLATE check-required
|
||||
if len(side) == 0 {
|
||||
|
@ -94,7 +95,7 @@ func (v *V3CreateOrderRequest) GetParameters() (map[string]interface{}, error) {
|
|||
// assign parameter of side
|
||||
params["side"] = side
|
||||
// check volume field -> json key volume
|
||||
volume := v.volume
|
||||
volume := g.volume
|
||||
|
||||
// TEMPLATE check-required
|
||||
if len(volume) == 0 {
|
||||
|
@ -105,37 +106,37 @@ func (v *V3CreateOrderRequest) GetParameters() (map[string]interface{}, error) {
|
|||
// assign parameter of volume
|
||||
params["volume"] = volume
|
||||
// check orderType field -> json key ord_type
|
||||
orderType := v.orderType
|
||||
orderType := g.orderType
|
||||
|
||||
// assign parameter of orderType
|
||||
params["ord_type"] = orderType
|
||||
// check price field -> json key price
|
||||
if v.price != nil {
|
||||
price := *v.price
|
||||
if g.price != nil {
|
||||
price := *g.price
|
||||
|
||||
// assign parameter of price
|
||||
params["price"] = price
|
||||
} else {
|
||||
}
|
||||
// check stopPrice field -> json key stop_price
|
||||
if v.stopPrice != nil {
|
||||
stopPrice := *v.stopPrice
|
||||
if g.stopPrice != nil {
|
||||
stopPrice := *g.stopPrice
|
||||
|
||||
// assign parameter of stopPrice
|
||||
params["stop_price"] = stopPrice
|
||||
} else {
|
||||
}
|
||||
// check clientOrderID field -> json key client_oid
|
||||
if v.clientOrderID != nil {
|
||||
clientOrderID := *v.clientOrderID
|
||||
if g.clientOrderID != nil {
|
||||
clientOrderID := *g.clientOrderID
|
||||
|
||||
// assign parameter of clientOrderID
|
||||
params["client_oid"] = clientOrderID
|
||||
} else {
|
||||
}
|
||||
// check groupID field -> json key group_id
|
||||
if v.groupID != nil {
|
||||
groupID := *v.groupID
|
||||
if g.groupID != nil {
|
||||
groupID := *g.groupID
|
||||
|
||||
// assign parameter of groupID
|
||||
params["group_id"] = groupID
|
||||
|
@ -146,17 +147,17 @@ func (v *V3CreateOrderRequest) GetParameters() (map[string]interface{}, error) {
|
|||
}
|
||||
|
||||
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
|
||||
func (v *V3CreateOrderRequest) GetParametersQuery() (url.Values, error) {
|
||||
func (g *GetOrderRequest) GetParametersQuery() (url.Values, error) {
|
||||
query := url.Values{}
|
||||
|
||||
params, err := v.GetParameters()
|
||||
params, err := g.GetParameters()
|
||||
if err != nil {
|
||||
return query, err
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
if v.isVarSlice(_v) {
|
||||
v.iterateSlice(_v, func(it interface{}) {
|
||||
if g.isVarSlice(_v) {
|
||||
g.iterateSlice(_v, func(it interface{}) {
|
||||
query.Add(_k+"[]", fmt.Sprintf("%v", it))
|
||||
})
|
||||
} else {
|
||||
|
@ -168,8 +169,8 @@ func (v *V3CreateOrderRequest) GetParametersQuery() (url.Values, error) {
|
|||
}
|
||||
|
||||
// GetParametersJSON converts the parameters from GetParameters into the JSON format
|
||||
func (v *V3CreateOrderRequest) GetParametersJSON() ([]byte, error) {
|
||||
params, err := v.GetParameters()
|
||||
func (g *GetOrderRequest) GetParametersJSON() ([]byte, error) {
|
||||
params, err := g.GetParameters()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -178,10 +179,10 @@ func (v *V3CreateOrderRequest) GetParametersJSON() ([]byte, error) {
|
|||
}
|
||||
|
||||
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
|
||||
func (v *V3CreateOrderRequest) GetSlugParameters() (map[string]interface{}, error) {
|
||||
func (g *GetOrderRequest) GetSlugParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
// check walletType field -> json key walletType
|
||||
walletType := v.walletType
|
||||
walletType := g.walletType
|
||||
|
||||
// TEMPLATE check-required
|
||||
if len(walletType) == 0 {
|
||||
|
@ -195,7 +196,7 @@ func (v *V3CreateOrderRequest) GetSlugParameters() (map[string]interface{}, erro
|
|||
return params, nil
|
||||
}
|
||||
|
||||
func (v *V3CreateOrderRequest) applySlugsToUrl(url string, slugs map[string]string) string {
|
||||
func (g *GetOrderRequest) applySlugsToUrl(url string, slugs map[string]string) string {
|
||||
for _k, _v := range slugs {
|
||||
needleRE := regexp.MustCompile(":" + _k + "\\b")
|
||||
url = needleRE.ReplaceAllString(url, _v)
|
||||
|
@ -204,7 +205,7 @@ func (v *V3CreateOrderRequest) applySlugsToUrl(url string, slugs map[string]stri
|
|||
return url
|
||||
}
|
||||
|
||||
func (v *V3CreateOrderRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
|
||||
func (g *GetOrderRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
|
||||
sliceValue := reflect.ValueOf(slice)
|
||||
for _i := 0; _i < sliceValue.Len(); _i++ {
|
||||
it := sliceValue.Index(_i).Interface()
|
||||
|
@ -212,7 +213,7 @@ func (v *V3CreateOrderRequest) iterateSlice(slice interface{}, _f func(it interf
|
|||
}
|
||||
}
|
||||
|
||||
func (v *V3CreateOrderRequest) isVarSlice(_v interface{}) bool {
|
||||
func (g *GetOrderRequest) isVarSlice(_v interface{}) bool {
|
||||
rt := reflect.TypeOf(_v)
|
||||
switch rt.Kind() {
|
||||
case reflect.Slice:
|
||||
|
@ -221,9 +222,9 @@ func (v *V3CreateOrderRequest) isVarSlice(_v interface{}) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func (v *V3CreateOrderRequest) GetSlugsMap() (map[string]string, error) {
|
||||
func (g *GetOrderRequest) GetSlugsMap() (map[string]string, error) {
|
||||
slugs := map[string]string{}
|
||||
params, err := v.GetSlugParameters()
|
||||
params, err := g.GetSlugParameters()
|
||||
if err != nil {
|
||||
return slugs, nil
|
||||
}
|
||||
|
@ -235,34 +236,34 @@ func (v *V3CreateOrderRequest) GetSlugsMap() (map[string]string, error) {
|
|||
return slugs, nil
|
||||
}
|
||||
|
||||
func (v *V3CreateOrderRequest) Do(ctx context.Context) (*Order, error) {
|
||||
func (g *GetOrderRequest) Do(ctx context.Context) (*max.Order, error) {
|
||||
|
||||
// empty params for GET operation
|
||||
var params interface{}
|
||||
query, err := v.GetParametersQuery()
|
||||
query, err := g.GetParametersQuery()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
apiURL := "/api/v3/wallet/:walletType/orders"
|
||||
slugs, err := v.GetSlugsMap()
|
||||
slugs, err := g.GetSlugsMap()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
apiURL = v.applySlugsToUrl(apiURL, slugs)
|
||||
apiURL = g.applySlugsToUrl(apiURL, slugs)
|
||||
|
||||
req, err := v.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
|
||||
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := v.client.SendRequest(req)
|
||||
response, err := g.client.SendRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var apiResponse Order
|
||||
var apiResponse max.Order
|
||||
if err := response.DecodeJSON(&apiResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
84
pkg/exchange/max/maxapi/v3/order.go
Normal file
84
pkg/exchange/max/maxapi/v3/order.go
Normal file
|
@ -0,0 +1,84 @@
|
|||
package v3
|
||||
|
||||
//go:generate -command GetRequest requestgen -method GET
|
||||
//go:generate -command PostRequest requestgen -method POST
|
||||
//go:generate -command DeleteRequest requestgen -method DELETE
|
||||
|
||||
import (
|
||||
"github.com/c9s/requestgen"
|
||||
|
||||
maxapi "github.com/c9s/bbgo/pkg/exchange/max/maxapi"
|
||||
)
|
||||
|
||||
type Order = maxapi.Order
|
||||
|
||||
// OrderService manages the Order endpoint.
|
||||
type OrderService struct {
|
||||
client *maxapi.RestClient
|
||||
}
|
||||
|
||||
func (s *OrderService) NewCreateOrderRequest() *CreateOrderRequest {
|
||||
return &CreateOrderRequest{client: s.client}
|
||||
}
|
||||
|
||||
func (s *OrderService) NewGetOrderRequest() *GetOrderRequest {
|
||||
return &GetOrderRequest{client: s.client}
|
||||
}
|
||||
|
||||
func (s *OrderService) NewOrderCancelAllRequest() *OrderCancelAllRequest {
|
||||
return &OrderCancelAllRequest{client: s.client}
|
||||
}
|
||||
|
||||
func (s *OrderService) NewOrderCancelRequest() *OrderCancelRequest {
|
||||
return &OrderCancelRequest{client: s.client}
|
||||
}
|
||||
|
||||
//go:generate PostRequest -url "/api/v3/wallet/:walletType/orders" -type CreateOrderRequest -responseType .Order -debug
|
||||
type CreateOrderRequest struct {
|
||||
client requestgen.AuthenticatedAPIClient
|
||||
|
||||
walletType string `param:"walletType,slug,required"`
|
||||
market string `param:"market,required"`
|
||||
side string `param:"side,required"`
|
||||
volume string `param:"volume,required"`
|
||||
orderType string `param:"ord_type"`
|
||||
|
||||
price *string `param:"price"`
|
||||
stopPrice *string `param:"stop_price"`
|
||||
clientOrderID *string `param:"client_oid"`
|
||||
groupID *string `param:"group_id"`
|
||||
}
|
||||
|
||||
//go:generate GetRequest -url "/api/v3/wallet/:walletType/orders" -type GetOrderRequest -responseType .Order
|
||||
type GetOrderRequest struct {
|
||||
client requestgen.AuthenticatedAPIClient
|
||||
|
||||
walletType string `param:"walletType,slug,required"`
|
||||
market string `param:"market,required"`
|
||||
side string `param:"side,required"`
|
||||
volume string `param:"volume,required"`
|
||||
orderType string `param:"ord_type"`
|
||||
|
||||
price *string `param:"price"`
|
||||
stopPrice *string `param:"stop_price"`
|
||||
clientOrderID *string `param:"client_oid"`
|
||||
groupID *string `param:"group_id"`
|
||||
}
|
||||
|
||||
//go:generate DeleteRequest -url "/api/v3/wallet/:walletType/orders" -type OrderCancelAllRequest -responseType []Order
|
||||
type OrderCancelAllRequest struct {
|
||||
client requestgen.AuthenticatedAPIClient
|
||||
|
||||
walletType string `param:"walletType,slug,required"`
|
||||
side *string `param:"side"`
|
||||
market *string `param:"market"`
|
||||
groupID *uint32 `param:"groupID"`
|
||||
}
|
||||
|
||||
//go:generate PostRequest -url "/api/v3/order" -type OrderCancelRequest -responseType .Order
|
||||
type OrderCancelRequest struct {
|
||||
client requestgen.AuthenticatedAPIClient
|
||||
|
||||
id *uint64 `param:"id,omitempty"`
|
||||
clientOrderID *string `param:"client_oid,omitempty"`
|
||||
}
|
|
@ -0,0 +1,199 @@
|
|||
// Code generated by "requestgen -method DELETE -url /api/v3/wallet/:walletType/orders -type OrderCancelAllRequest -responseType []Order"; DO NOT EDIT.
|
||||
|
||||
package v3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/c9s/bbgo/pkg/exchange/max/maxapi"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func (o *OrderCancelAllRequest) Side(side string) *OrderCancelAllRequest {
|
||||
o.side = &side
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *OrderCancelAllRequest) Market(market string) *OrderCancelAllRequest {
|
||||
o.market = &market
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *OrderCancelAllRequest) GroupID(groupID uint32) *OrderCancelAllRequest {
|
||||
o.groupID = &groupID
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *OrderCancelAllRequest) WalletType(walletType string) *OrderCancelAllRequest {
|
||||
o.walletType = walletType
|
||||
return o
|
||||
}
|
||||
|
||||
// GetQueryParameters builds and checks the query parameters and returns url.Values
|
||||
func (o *OrderCancelAllRequest) 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 (o *OrderCancelAllRequest) GetParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
// check side field -> json key side
|
||||
if o.side != nil {
|
||||
side := *o.side
|
||||
|
||||
// assign parameter of side
|
||||
params["side"] = side
|
||||
} else {
|
||||
}
|
||||
// check market field -> json key market
|
||||
if o.market != nil {
|
||||
market := *o.market
|
||||
|
||||
// assign parameter of market
|
||||
params["market"] = market
|
||||
} else {
|
||||
}
|
||||
// check groupID field -> json key groupID
|
||||
if o.groupID != nil {
|
||||
groupID := *o.groupID
|
||||
|
||||
// assign parameter of groupID
|
||||
params["groupID"] = groupID
|
||||
} else {
|
||||
}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
|
||||
func (o *OrderCancelAllRequest) GetParametersQuery() (url.Values, error) {
|
||||
query := url.Values{}
|
||||
|
||||
params, err := o.GetParameters()
|
||||
if err != nil {
|
||||
return query, err
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
if o.isVarSlice(_v) {
|
||||
o.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 (o *OrderCancelAllRequest) GetParametersJSON() ([]byte, error) {
|
||||
params, err := o.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 (o *OrderCancelAllRequest) GetSlugParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
// check walletType field -> json key walletType
|
||||
walletType := o.walletType
|
||||
|
||||
// TEMPLATE check-required
|
||||
if len(walletType) == 0 {
|
||||
return nil, fmt.Errorf("walletType is required, empty string given")
|
||||
}
|
||||
// END TEMPLATE check-required
|
||||
|
||||
// assign parameter of walletType
|
||||
params["walletType"] = walletType
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func (o *OrderCancelAllRequest) 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 (o *OrderCancelAllRequest) 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 (o *OrderCancelAllRequest) isVarSlice(_v interface{}) bool {
|
||||
rt := reflect.TypeOf(_v)
|
||||
switch rt.Kind() {
|
||||
case reflect.Slice:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (o *OrderCancelAllRequest) GetSlugsMap() (map[string]string, error) {
|
||||
slugs := map[string]string{}
|
||||
params, err := o.GetSlugParameters()
|
||||
if err != nil {
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
slugs[_k] = fmt.Sprintf("%v", _v)
|
||||
}
|
||||
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
func (o *OrderCancelAllRequest) Do(ctx context.Context) ([]max.Order, error) {
|
||||
|
||||
params, err := o.GetParameters()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query := url.Values{}
|
||||
|
||||
apiURL := "/api/v3/wallet/:walletType/orders"
|
||||
slugs, err := o.GetSlugsMap()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
apiURL = o.applySlugsToUrl(apiURL, slugs)
|
||||
|
||||
req, err := o.client.NewAuthenticatedRequest(ctx, "DELETE", apiURL, query, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := o.client.SendRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var apiResponse []max.Order
|
||||
if err := response.DecodeJSON(&apiResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return apiResponse, nil
|
||||
}
|
164
pkg/exchange/max/maxapi/v3/order_cancel_request_requestgen.go
Normal file
164
pkg/exchange/max/maxapi/v3/order_cancel_request_requestgen.go
Normal file
|
@ -0,0 +1,164 @@
|
|||
// Code generated by "requestgen -method POST -url /api/v3/order -type OrderCancelRequest -responseType .Order"; DO NOT EDIT.
|
||||
|
||||
package v3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/c9s/bbgo/pkg/exchange/max/maxapi"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func (o *OrderCancelRequest) Id(id uint64) *OrderCancelRequest {
|
||||
o.id = &id
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *OrderCancelRequest) ClientOrderID(clientOrderID string) *OrderCancelRequest {
|
||||
o.clientOrderID = &clientOrderID
|
||||
return o
|
||||
}
|
||||
|
||||
// GetQueryParameters builds and checks the query parameters and returns url.Values
|
||||
func (o *OrderCancelRequest) 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 (o *OrderCancelRequest) GetParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
// check id field -> json key id
|
||||
if o.id != nil {
|
||||
id := *o.id
|
||||
|
||||
// assign parameter of id
|
||||
params["id"] = id
|
||||
} else {
|
||||
}
|
||||
// check clientOrderID field -> json key client_oid
|
||||
if o.clientOrderID != nil {
|
||||
clientOrderID := *o.clientOrderID
|
||||
|
||||
// assign parameter of clientOrderID
|
||||
params["client_oid"] = clientOrderID
|
||||
} else {
|
||||
}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
|
||||
func (o *OrderCancelRequest) GetParametersQuery() (url.Values, error) {
|
||||
query := url.Values{}
|
||||
|
||||
params, err := o.GetParameters()
|
||||
if err != nil {
|
||||
return query, err
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
if o.isVarSlice(_v) {
|
||||
o.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 (o *OrderCancelRequest) GetParametersJSON() ([]byte, error) {
|
||||
params, err := o.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 (o *OrderCancelRequest) GetSlugParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func (o *OrderCancelRequest) 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 (o *OrderCancelRequest) 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 (o *OrderCancelRequest) isVarSlice(_v interface{}) bool {
|
||||
rt := reflect.TypeOf(_v)
|
||||
switch rt.Kind() {
|
||||
case reflect.Slice:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (o *OrderCancelRequest) GetSlugsMap() (map[string]string, error) {
|
||||
slugs := map[string]string{}
|
||||
params, err := o.GetSlugParameters()
|
||||
if err != nil {
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
slugs[_k] = fmt.Sprintf("%v", _v)
|
||||
}
|
||||
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
func (o *OrderCancelRequest) Do(ctx context.Context) (*max.Order, error) {
|
||||
|
||||
params, err := o.GetParameters()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query := url.Values{}
|
||||
|
||||
apiURL := "/api/v3/order"
|
||||
|
||||
req, err := o.client.NewAuthenticatedRequest(ctx, "POST", apiURL, query, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := o.client.SendRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var apiResponse max.Order
|
||||
if err := response.DecodeJSON(&apiResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &apiResponse, nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user