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
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
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
import (
"context"
"encoding/json"
@ -40,36 +43,21 @@ type Symbol struct {
EnableTrading bool `json:"enableTrading"`
}
func (s *MarketDataService) ListSymbols(market ...string) ([]Symbol, error) {
var params = url.Values{}
//go:generate GetRequest -type ListSymbolsRequest -method "GET" -url "/api/v1/symbols" -responseDataType []Symbol -debug
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 {
params["market"] = []string{market[0]}
req.Market(market[0])
} else if len(market) > 1 {
return nil, errors.New("symbols api only supports one market parameter")
}
req, err := s.client.NewRequest(context.Background(), "GET", "/api/v1/symbols", params, nil)
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
return req.Do(context.Background())
}
/*
@ -244,9 +232,6 @@ type OrderBook struct {
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
type GetOrderBookLevel2Depth20Request struct {
client requestgen.APIClient