kucoin: rewrite GetTickerRequest with requestgen

This commit is contained in:
c9s 2021-12-30 02:22:33 +08:00
parent 5136001c9b
commit 0fc91500e4
3 changed files with 114 additions and 27 deletions

View File

@ -0,0 +1,95 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -type GetTickerRequest -url /api/v1/market/orderbook/level1 -responseDataType Ticker"; DO NOT EDIT.
package kucoinapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
)
func (g *GetTickerRequest) Symbol(symbol string) *GetTickerRequest {
g.symbol = symbol
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetTickerRequest) 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 *GetTickerRequest) 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 *GetTickerRequest) 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 *GetTickerRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
func (g *GetTickerRequest) Do(ctx context.Context) (*Ticker, 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/level1", 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 Ticker
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -1,4 +1,4 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -type ListSymbolsRequest -method GET -url /api/v1/symbols -responseDataType []Symbol -debug"; DO NOT EDIT. // Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -type ListSymbolsRequest -url /api/v1/symbols -responseDataType []Symbol"; DO NOT EDIT.
package kucoinapi package kucoinapi

View File

@ -43,14 +43,18 @@ type Symbol struct {
EnableTrading bool `json:"enableTrading"` EnableTrading bool `json:"enableTrading"`
} }
//go:generate GetRequest -type ListSymbolsRequest -method "GET" -url "/api/v1/symbols" -responseDataType []Symbol -debug //go:generate GetRequest -type ListSymbolsRequest -url "/api/v1/symbols" -responseDataType []Symbol
type ListSymbolsRequest struct { type ListSymbolsRequest struct {
client requestgen.APIClient client requestgen.APIClient
market *string `param:"market"` market *string `param:"market"`
} }
func (s *MarketDataService) NewListSymbolsRequest() *ListSymbolsRequest {
return &ListSymbolsRequest{client: s.client}
}
func (s *MarketDataService) ListSymbols(market ...string) ([]Symbol, error) { func (s *MarketDataService) ListSymbols(market ...string) ([]Symbol, error) {
req := &ListSymbolsRequest{client: s.client} req := s.NewListSymbolsRequest()
if len(market) == 1 { if len(market) == 1 {
req.Market(market[0]) req.Market(market[0])
} else if len(market) > 1 { } else if len(market) > 1 {
@ -83,31 +87,19 @@ type Ticker struct {
Time types.MillisecondTimestamp `json:"time"` Time types.MillisecondTimestamp `json:"time"`
} }
//go:generate GetRequest -type GetTickerRequest -url "/api/v1/market/orderbook/level1" -responseDataType Ticker
type GetTickerRequest struct {
client requestgen.APIClient
symbol string `param:"symbol,query"`
}
func (s *MarketDataService) NewGetTickerRequest(symbol string) *GetTickerRequest {
return &GetTickerRequest{client: s.client, symbol: symbol}
}
func (s *MarketDataService) GetTicker(symbol string) (*Ticker, error) { func (s *MarketDataService) GetTicker(symbol string) (*Ticker, error) {
var params = url.Values{} req := s.NewGetTickerRequest(symbol)
params["symbol"] = []string{symbol} return req.Do(context.Background())
req, err := s.client.NewRequest(context.Background(), "GET", "/api/v1/market/orderbook/level1", 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 *Ticker `json:"data"`
}
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
return apiResponse.Data, nil
} }
/* /*