refactor orderbook requests with requestgen

This commit is contained in:
c9s 2021-12-30 01:15:19 +08:00
parent dba4d4da46
commit 41435458d1
14 changed files with 575 additions and 233 deletions

View File

@ -18,9 +18,21 @@ func (r *CancelAllOrderRequest) TradeType(tradeType string) *CancelAllOrderReque
return r
}
func (r *CancelAllOrderRequest) GetParameters() (map[string]interface{}, error) {
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (r *CancelAllOrderRequest) 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 *CancelAllOrderRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
if r.symbol != nil {
symbol := *r.symbol
@ -28,7 +40,6 @@ func (r *CancelAllOrderRequest) GetParameters() (map[string]interface{}, error)
// assign parameter of symbol
params["symbol"] = symbol
}
// check tradeType field -> json key tradeType
if r.tradeType != nil {
tradeType := *r.tradeType
@ -40,6 +51,7 @@ func (r *CancelAllOrderRequest) GetParameters() (map[string]interface{}, error)
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (r *CancelAllOrderRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
@ -55,6 +67,7 @@ func (r *CancelAllOrderRequest) GetParametersQuery() (url.Values, error) {
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (r *CancelAllOrderRequest) GetParametersJSON() ([]byte, error) {
params, err := r.GetParameters()
if err != nil {

View File

@ -1,65 +0,0 @@
// Code generated by "requestgen -type CancelOrderRequest"; DO NOT EDIT.
package kucoinapi
import (
"encoding/json"
"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
// assign parameter of orderID
params["orderID"] = orderID
}
// check clientOrderID field -> json key clientOrderID
if c.clientOrderID != nil {
clientOrderID := *c.clientOrderID
// assign parameter of clientOrderID
params["clientOrderID"] = 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,78 @@
// Code generated by "requestgen -type CancelOrderRequest"; DO NOT EDIT.
package kucoinapi
import (
"encoding/json"
"fmt"
"net/url"
)
func (r *CancelOrderRequest) OrderID(orderID string) *CancelOrderRequest {
r.orderID = &orderID
return r
}
func (r *CancelOrderRequest) ClientOrderID(clientOrderID string) *CancelOrderRequest {
r.clientOrderID = &clientOrderID
return r
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (r *CancelOrderRequest) 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 *CancelOrderRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check orderID field -> json key orderID
if r.orderID != nil {
orderID := *r.orderID
// assign parameter of orderID
params["orderID"] = orderID
}
// check clientOrderID field -> json key clientOrderID
if r.clientOrderID != nil {
clientOrderID := *r.clientOrderID
// assign parameter of clientOrderID
params["clientOrderID"] = clientOrderID
}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (r *CancelOrderRequest) GetParametersQuery() (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
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (r *CancelOrderRequest) GetParametersJSON() ([]byte, error) {
params, err := r.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}

View File

@ -40,9 +40,21 @@ func (r *GetFillsRequest) EndAt(endAt time.Time) *GetFillsRequest {
return r
}
func (r *GetFillsRequest) GetParameters() (map[string]interface{}, error) {
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (r *GetFillsRequest) 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 *GetFillsRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check orderID field -> json key orderId
if r.orderID != nil {
orderID := *r.orderID
@ -50,7 +62,6 @@ func (r *GetFillsRequest) GetParameters() (map[string]interface{}, error) {
// assign parameter of orderID
params["orderId"] = orderID
}
// check symbol field -> json key symbol
if r.symbol != nil {
symbol := *r.symbol
@ -58,7 +69,6 @@ func (r *GetFillsRequest) GetParameters() (map[string]interface{}, error) {
// assign parameter of symbol
params["symbol"] = symbol
}
// check side field -> json key side
if r.side != nil {
side := *r.side
@ -75,7 +85,6 @@ func (r *GetFillsRequest) GetParameters() (map[string]interface{}, error) {
// assign parameter of side
params["side"] = side
}
// check orderType field -> json key type
if r.orderType != nil {
orderType := *r.orderType
@ -92,28 +101,27 @@ func (r *GetFillsRequest) GetParameters() (map[string]interface{}, error) {
// assign parameter of orderType
params["type"] = orderType
}
// check startAt field -> json key startAt
if r.startAt != nil {
startAt := *r.startAt
// assign parameter of startAt
// convert time.Time to milliseconds time
// convert time.Time to milliseconds time stamp
params["startAt"] = strconv.FormatInt(startAt.UnixNano()/int64(time.Millisecond), 10)
}
// check endAt field -> json key endAt
if r.endAt != nil {
endAt := *r.endAt
// assign parameter of endAt
// convert time.Time to milliseconds time
// convert time.Time to milliseconds time stamp
params["endAt"] = strconv.FormatInt(endAt.UnixNano()/int64(time.Millisecond), 10)
}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (r *GetFillsRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
@ -129,6 +137,7 @@ func (r *GetFillsRequest) GetParametersQuery() (url.Values, error) {
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (r *GetFillsRequest) GetParametersJSON() ([]byte, error) {
params, err := r.GetParameters()
if err != nil {

View File

@ -1,100 +0,0 @@
// Code generated by "requestgen -type GetKLinesRequest"; DO NOT EDIT.
package kucoinapi
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"time"
)
func (g *GetKLinesRequest) Symbol(symbol string) *GetKLinesRequest {
g.symbol = symbol
return g
}
func (g *GetKLinesRequest) Interval(interval string) *GetKLinesRequest {
g.interval = interval
return g
}
func (g *GetKLinesRequest) StartAt(startAt time.Time) *GetKLinesRequest {
g.startAt = &startAt
return g
}
func (g *GetKLinesRequest) EndAt(endAt time.Time) *GetKLinesRequest {
g.endAt = &endAt
return g
}
func (g *GetKLinesRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
symbol := g.symbol
// assign parameter of symbol
params["symbol"] = symbol
// check interval field -> json key type
interval := g.interval
switch interval {
case "1min", "3min", "5min", "15min", "30min", "1hour", "2hour", "4hour", "6hour", "8hour", "12hour", "1day", "1week":
params["type"] = interval
default:
return params, fmt.Errorf("type value %v is invalid", interval)
}
// assign parameter of interval
params["type"] = interval
// check startAt field -> json key startAt
if g.startAt != nil {
startAt := *g.startAt
// assign parameter of startAt
// convert time.Time to seconds time stamp
params["startAt"] = strconv.FormatInt(startAt.Unix(), 10)
}
// check endAt field -> json key endAt
if g.endAt != nil {
endAt := *g.endAt
// assign parameter of endAt
// convert time.Time to seconds time stamp
params["endAt"] = strconv.FormatInt(endAt.Unix(), 10)
}
return params, nil
}
func (g *GetKLinesRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}
func (g *GetKLinesRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}

View File

@ -0,0 +1,111 @@
// Code generated by "requestgen -type GetKLinesRequest"; DO NOT EDIT.
package kucoinapi
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"time"
)
func (r *GetKLinesRequest) Symbol(symbol string) *GetKLinesRequest {
r.symbol = symbol
return r
}
func (r *GetKLinesRequest) Interval(interval string) *GetKLinesRequest {
r.interval = interval
return r
}
func (r *GetKLinesRequest) StartAt(startAt time.Time) *GetKLinesRequest {
r.startAt = &startAt
return r
}
func (r *GetKLinesRequest) EndAt(endAt time.Time) *GetKLinesRequest {
r.endAt = &endAt
return r
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (r *GetKLinesRequest) 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 *GetKLinesRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
symbol := r.symbol
// assign parameter of symbol
params["symbol"] = symbol
// check interval field -> json key type
interval := r.interval
switch interval {
case "1min", "3min", "5min", "15min", "30min", "1hour", "2hour", "4hour", "6hour", "8hour", "12hour", "1day", "1week":
params["type"] = interval
default:
return params, fmt.Errorf("type value %v is invalid", interval)
}
// assign parameter of interval
params["type"] = interval
// check startAt field -> json key startAt
if r.startAt != nil {
startAt := *r.startAt
// assign parameter of startAt
// convert time.Time to seconds time stamp
params["startAt"] = strconv.FormatInt(startAt.Unix(), 10)
}
// check endAt field -> json key endAt
if r.endAt != nil {
endAt := *r.endAt
// assign parameter of endAt
// convert time.Time to seconds time stamp
params["endAt"] = strconv.FormatInt(endAt.Unix(), 10)
}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (r *GetKLinesRequest) GetParametersQuery() (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
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (r *GetKLinesRequest) GetParametersJSON() ([]byte, error) {
params, err := r.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}

View File

@ -0,0 +1,95 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -type GetOrderBookLevel2Depth100Request -url /api/v1/market/orderbook/level2_100 -responseDataType .OrderBook"; DO NOT EDIT.
package kucoinapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
)
func (g *GetOrderBookLevel2Depth100Request) Symbol(symbol string) *GetOrderBookLevel2Depth100Request {
g.symbol = symbol
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetOrderBookLevel2Depth100Request) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
symbol := g.symbol
// assign parameter of symbol
params["symbol"] = symbol
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 (g *GetOrderBookLevel2Depth100Request) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetOrderBookLevel2Depth100Request) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetOrderBookLevel2Depth100Request) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
func (g *GetOrderBookLevel2Depth100Request) Do(ctx context.Context) (*OrderBook, error) {
// no body params
var params interface{}
query, err := g.GetQueryParameters()
if err != nil {
return nil, err
}
req, err := g.client.NewRequest(ctx, "GET", "/api/v1/market/orderbook/level2_100", query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data OrderBook
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -0,0 +1,95 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -type GetOrderBookLevel2Depth20Request -url /api/v1/market/orderbook/level2_20 -responseDataType .OrderBook"; DO NOT EDIT.
package kucoinapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
)
func (g *GetOrderBookLevel2Depth20Request) Symbol(symbol string) *GetOrderBookLevel2Depth20Request {
g.symbol = symbol
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetOrderBookLevel2Depth20Request) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
symbol := g.symbol
// assign parameter of symbol
params["symbol"] = symbol
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 (g *GetOrderBookLevel2Depth20Request) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetOrderBookLevel2Depth20Request) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetOrderBookLevel2Depth20Request) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
func (g *GetOrderBookLevel2Depth20Request) Do(ctx context.Context) (*OrderBook, error) {
// no body params
var params interface{}
query, err := g.GetQueryParameters()
if err != nil {
return nil, err
}
req, err := g.client.NewRequest(ctx, "GET", "/api/v1/market/orderbook/level2_20", query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data OrderBook
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -0,0 +1,95 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -type GetOrderBookLevel2DepthAllRequest -url /api/v3/market/orderbook/level2 -responseDataType .OrderBook"; DO NOT EDIT.
package kucoinapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
)
func (g *GetOrderBookLevel2DepthAllRequest) Symbol(symbol string) *GetOrderBookLevel2DepthAllRequest {
g.symbol = symbol
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetOrderBookLevel2DepthAllRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
symbol := g.symbol
// assign parameter of symbol
params["symbol"] = symbol
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 (g *GetOrderBookLevel2DepthAllRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetOrderBookLevel2DepthAllRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetOrderBookLevel2DepthAllRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
func (g *GetOrderBookLevel2DepthAllRequest) Do(ctx context.Context) (*OrderBook, error) {
// no body params
var params interface{}
query, err := g.GetQueryParameters()
if err != nil {
return nil, err
}
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", "/api/v3/market/orderbook/level2", query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data OrderBook
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -8,18 +8,18 @@ import (
"net/url"
)
func (r *GetPublicBulletRequest) Do(ctx context.Context) (*Bullet, error) {
func (g *GetPublicBulletRequest) Do(ctx context.Context) (*Bullet, error) {
// no body params
var params interface{}
query := url.Values{}
req, err := r.client.NewRequest(ctx, "POST", "/api/v1/bullet-public", query, params)
req, err := g.client.NewRequest(ctx, "POST", "/api/v1/bullet-public", query, params)
if err != nil {
return nil, err
}
response, err := r.client.SendRequest(req)
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}

View File

@ -45,9 +45,21 @@ func (r *ListOrdersRequest) EndAt(endAt time.Time) *ListOrdersRequest {
return r
}
func (r *ListOrdersRequest) GetParameters() (map[string]interface{}, error) {
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (r *ListOrdersRequest) 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 *ListOrdersRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check status field -> json key status
if r.status != nil {
status := *r.status
@ -64,7 +76,6 @@ func (r *ListOrdersRequest) GetParameters() (map[string]interface{}, error) {
// assign parameter of status
params["status"] = status
}
// check symbol field -> json key symbol
if r.symbol != nil {
symbol := *r.symbol
@ -72,7 +83,6 @@ func (r *ListOrdersRequest) GetParameters() (map[string]interface{}, error) {
// assign parameter of symbol
params["symbol"] = symbol
}
// check side field -> json key side
if r.side != nil {
side := *r.side
@ -89,7 +99,6 @@ func (r *ListOrdersRequest) GetParameters() (map[string]interface{}, error) {
// assign parameter of side
params["side"] = side
}
// check orderType field -> json key type
if r.orderType != nil {
orderType := *r.orderType
@ -97,7 +106,6 @@ func (r *ListOrdersRequest) GetParameters() (map[string]interface{}, error) {
// assign parameter of orderType
params["type"] = orderType
}
// check tradeType field -> json key tradeType
if r.tradeType != nil {
tradeType := *r.tradeType
@ -105,28 +113,27 @@ func (r *ListOrdersRequest) GetParameters() (map[string]interface{}, error) {
// assign parameter of tradeType
params["tradeType"] = tradeType
}
// check startAt field -> json key startAt
if r.startAt != nil {
startAt := *r.startAt
// assign parameter of startAt
// convert time.Time to milliseconds time
// convert time.Time to milliseconds time stamp
params["startAt"] = strconv.FormatInt(startAt.UnixNano()/int64(time.Millisecond), 10)
}
// check endAt field -> json key endAt
if r.endAt != nil {
endAt := *r.endAt
// assign parameter of endAt
// convert time.Time to milliseconds time
// convert time.Time to milliseconds time stamp
params["endAt"] = strconv.FormatInt(endAt.UnixNano()/int64(time.Millisecond), 10)
}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (r *ListOrdersRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
@ -142,6 +149,7 @@ func (r *ListOrdersRequest) GetParametersQuery() (url.Values, error) {
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (r *ListOrdersRequest) GetParametersJSON() ([]byte, error) {
params, err := r.GetParameters()
if err != nil {

View File

@ -3,12 +3,11 @@ package kucoinapi
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/c9s/requestgen"
"github.com/pkg/errors"
"github.com/valyala/fastjson"
@ -245,49 +244,46 @@ type OrderBook struct {
Asks types.PriceVolumeSlice `json:"asks,omitempty"`
}
func (s *MarketDataService) GetOrderBook(symbol string, depth int) (*OrderBook, error) {
params := url.Values{}
params["symbol"] = []string{symbol}
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
var req *http.Request
var err error
//go:generate GetRequest -type GetOrderBookLevel2Depth20Request -url "/api/v1/market/orderbook/level2_20" -responseDataType .OrderBook
type GetOrderBookLevel2Depth20Request struct {
client requestgen.APIClient
symbol string `param:"symbol,query"`
}
//go:generate GetRequest -type GetOrderBookLevel2Depth100Request -url "/api/v1/market/orderbook/level2_100" -responseDataType .OrderBook
type GetOrderBookLevel2Depth100Request struct {
client requestgen.APIClient
symbol string `param:"symbol,query"`
}
//go:generate GetRequest -type GetOrderBookLevel2DepthAllRequest -url "/api/v3/market/orderbook/level2" -responseDataType .OrderBook
type GetOrderBookLevel2DepthAllRequest struct {
client requestgen.AuthenticatedAPIClient
symbol string `param:"symbol,query"`
}
type OrderBookRequest interface {
Do(ctx context.Context) (*OrderBook, error)
}
func (s *MarketDataService) NewGetOrderBookRequest(symbol string, depth int) OrderBookRequest {
switch depth {
case 20, 100:
refURL := "/api/v1/market/orderbook/level2_" + strconv.Itoa(depth)
req, err = s.client.NewRequest(context.Background(), "GET", refURL, params, nil)
if err != nil {
return nil, err
case 20:
return &GetOrderBookLevel2Depth20Request{client: s.client, symbol: symbol}
case 100:
return &GetOrderBookLevel2Depth100Request{client: s.client, symbol: symbol}
}
case 0:
refURL := "/api/v3/market/orderbook/level2"
req, err = s.client.NewAuthenticatedRequest(context.Background(), "GET", refURL, params, nil)
if err != nil {
return nil, err
return &GetOrderBookLevel2DepthAllRequest{client: s.client, symbol: symbol}
}
default:
return nil, fmt.Errorf("depth %d is not supported, use 20, 100 or 0", depth)
}
response, err := s.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
Data *OrderBook `json:"data"`
}
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
return apiResponse.Data, nil
func (s *MarketDataService) GetOrderBook(symbol string, depth int) (*OrderBook, error) {
req := s.NewGetOrderBookRequest(symbol, depth)
return req.Do(context.Background())
}
//go:generate requestgen -type GetKLinesRequest

View File

@ -49,9 +49,21 @@ func (r *PlaceOrderRequest) TimeInForce(timeInForce TimeInForceType) *PlaceOrder
return r
}
func (r *PlaceOrderRequest) GetParameters() (map[string]interface{}, error) {
// 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 clientOrderID field -> json key clientOid
if r.clientOrderID != nil {
clientOrderID := *r.clientOrderID
@ -70,7 +82,6 @@ func (r *PlaceOrderRequest) GetParameters() (map[string]interface{}, error) {
// assign parameter of clientOrderID
params["clientOid"] = clientOrderID
}
// check symbol field -> json key symbol
symbol := r.symbol
@ -80,7 +91,6 @@ func (r *PlaceOrderRequest) GetParameters() (map[string]interface{}, error) {
// assign parameter of symbol
params["symbol"] = symbol
// check tag field -> json key tag
if r.tag != nil {
tag := *r.tag
@ -88,19 +98,16 @@ func (r *PlaceOrderRequest) GetParameters() (map[string]interface{}, error) {
// assign parameter of tag
params["tag"] = tag
}
// check side field -> json key side
side := r.side
// assign parameter of side
params["side"] = side
// check orderType field -> json key ordType
orderType := r.orderType
// assign parameter of orderType
params["ordType"] = orderType
// check size field -> json key size
size := r.size
@ -110,7 +117,6 @@ func (r *PlaceOrderRequest) GetParameters() (map[string]interface{}, error) {
// assign parameter of size
params["size"] = size
// check price field -> json key price
if r.price != nil {
price := *r.price
@ -118,7 +124,6 @@ func (r *PlaceOrderRequest) GetParameters() (map[string]interface{}, error) {
// assign parameter of price
params["price"] = price
}
// check timeInForce field -> json key timeInForce
if r.timeInForce != nil {
timeInForce := *r.timeInForce
@ -134,6 +139,7 @@ func (r *PlaceOrderRequest) GetParameters() (map[string]interface{}, error) {
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (r *PlaceOrderRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
@ -149,6 +155,7 @@ func (r *PlaceOrderRequest) GetParametersQuery() (url.Values, error) {
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 {