kucoin: rewrite ListSymbolsRequest

This commit is contained in:
c9s 2021-12-30 02:17:03 +08:00
parent 41435458d1
commit 5136001c9b
3 changed files with 107 additions and 27 deletions

View File

@ -1,3 +1,4 @@
//go:build ignore
// +build ignore // +build ignore
package main package main

View File

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

View File

@ -1,5 +1,8 @@
package kucoinapi package kucoinapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
import ( import (
"context" "context"
"encoding/json" "encoding/json"
@ -40,36 +43,21 @@ type Symbol struct {
EnableTrading bool `json:"enableTrading"` EnableTrading bool `json:"enableTrading"`
} }
func (s *MarketDataService) ListSymbols(market ...string) ([]Symbol, error) { //go:generate GetRequest -type ListSymbolsRequest -method "GET" -url "/api/v1/symbols" -responseDataType []Symbol -debug
var params = url.Values{} type ListSymbolsRequest struct {
client requestgen.APIClient
market *string `param:"market"`
}
func (s *MarketDataService) ListSymbols(market ...string) ([]Symbol, error) {
req := &ListSymbolsRequest{client: s.client}
if len(market) == 1 { if len(market) == 1 {
params["market"] = []string{market[0]} req.Market(market[0])
} else if len(market) > 1 { } else if len(market) > 1 {
return nil, errors.New("symbols api only supports one market parameter") return nil, errors.New("symbols api only supports one market parameter")
} }
req, err := s.client.NewRequest(context.Background(), "GET", "/api/v1/symbols", params, nil) return req.Do(context.Background())
if err != nil {
return nil, err
}
response, err := s.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
Data []Symbol `json:"data"`
}
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
return apiResponse.Data, nil
} }
/* /*
@ -244,9 +232,6 @@ type OrderBook struct {
Asks types.PriceVolumeSlice `json:"asks,omitempty"` Asks types.PriceVolumeSlice `json:"asks,omitempty"`
} }
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate GetRequest -type GetOrderBookLevel2Depth20Request -url "/api/v1/market/orderbook/level2_20" -responseDataType .OrderBook //go:generate GetRequest -type GetOrderBookLevel2Depth20Request -url "/api/v1/market/orderbook/level2_20" -responseDataType .OrderBook
type GetOrderBookLevel2Depth20Request struct { type GetOrderBookLevel2Depth20Request struct {
client requestgen.APIClient client requestgen.APIClient