From 3cf499b605e09c6d61eca897139d797468608dc0 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 30 Dec 2021 02:33:07 +0800 Subject: [PATCH] kucoin: rewrite GetAllTickersRequest api --- .../get_all_tickers_request_requestgen.go | 82 +++++++++++++++++++ pkg/exchange/kucoin/kucoinapi/marketdata.go | 28 ++----- 2 files changed, 89 insertions(+), 21 deletions(-) create mode 100644 pkg/exchange/kucoin/kucoinapi/get_all_tickers_request_requestgen.go diff --git a/pkg/exchange/kucoin/kucoinapi/get_all_tickers_request_requestgen.go b/pkg/exchange/kucoin/kucoinapi/get_all_tickers_request_requestgen.go new file mode 100644 index 000000000..8f5b9dba9 --- /dev/null +++ b/pkg/exchange/kucoin/kucoinapi/get_all_tickers_request_requestgen.go @@ -0,0 +1,82 @@ +// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -type GetAllTickersRequest -url /api/v1/market/allTickers -responseDataType AllTickers"; DO NOT EDIT. + +package kucoinapi + +import ( + "context" + "encoding/json" + "fmt" + "net/url" +) + +// GetQueryParameters builds and checks the query parameters and returns url.Values +func (g *GetAllTickersRequest) 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 (g *GetAllTickersRequest) 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 *GetAllTickersRequest) 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 *GetAllTickersRequest) GetParametersJSON() ([]byte, error) { + params, err := g.GetParameters() + if err != nil { + return nil, err + } + + return json.Marshal(params) +} + +func (g *GetAllTickersRequest) Do(ctx context.Context) (*AllTickers, error) { + + // no body params + var params interface{} + query := url.Values{} + + req, err := g.client.NewRequest(ctx, "GET", "/api/v1/market/allTickers", 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 AllTickers + if err := json.Unmarshal(apiResponse.Data, &data); err != nil { + return nil, err + } + return &data, nil +} diff --git a/pkg/exchange/kucoin/kucoinapi/marketdata.go b/pkg/exchange/kucoin/kucoinapi/marketdata.go index f43d73257..c687269c2 100644 --- a/pkg/exchange/kucoin/kucoinapi/marketdata.go +++ b/pkg/exchange/kucoin/kucoinapi/marketdata.go @@ -156,28 +156,14 @@ type AllTickers struct { Ticker []Ticker24H `json:"ticker"` } +//go:generate GetRequest -type GetAllTickersRequest -url "/api/v1/market/allTickers" -responseDataType AllTickers +type GetAllTickersRequest struct { + client requestgen.APIClient +} + func (s *MarketDataService) ListTickers() (*AllTickers, error) { - req, err := s.client.NewRequest(context.Background(), "GET", "/api/v1/market/allTickers", nil, 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 *AllTickers `json:"data"` - } - - if err := response.DecodeJSON(&apiResponse); err != nil { - return nil, err - } - - return apiResponse.Data, nil + req := &GetAllTickersRequest{client: s.client} + return req.Do(context.Background()) } func (s *MarketDataService) GetTicker24HStat(symbol string) (*Ticker24H, error) {