bitget: update generated request files and fix account assets api data type

This commit is contained in:
c9s 2023-05-17 17:53:24 +08:00
parent e31a6ca3c8
commit c347a2423a
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
12 changed files with 216 additions and 9 deletions

View File

@ -0,0 +1,37 @@
package bitgetapi
import (
"context"
"os"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/testutil"
)
func getTestClientOrSkip(t *testing.T) *RestClient {
if b, _ := strconv.ParseBool(os.Getenv("CI")); b {
t.Skip("skip test for CI")
}
key, secret, ok := testutil.IntegrationTestConfigured(t, "BITGET")
if !ok {
t.Skip("BITGET_* env vars are not configured")
return nil
}
client := NewClient()
client.Auth(key, secret, os.Getenv("BITGET_API_PASSPHRASE"))
return client
}
func TestClient_GetAccountAssetsRequest(t *testing.T) {
client := getTestClientOrSkip(t)
ctx := context.Background()
req := client.NewGetAccountAssetsRequest()
assets, err := req.Do(ctx)
assert.NoError(t, err)
t.Logf("assets: %+v", assets)
}

View File

@ -11,7 +11,7 @@ import (
)
type AccountAsset struct {
CoinId string `json:"coinId"`
CoinId int64 `json:"coinId"`
CoinName string `json:"coinName"`
Available fixedpoint.Value `json:"available"`
Frozen fixedpoint.Value `json:"frozen"`

View File

@ -9,7 +9,7 @@ import (
//go:generate GetRequest -url "/api/spot/v1/market/tickers" -type GetAllTickersRequest -responseDataType []Ticker
type GetAllTickersRequest struct {
client requestgen.AuthenticatedAPIClient
client requestgen.APIClient
}
func (c *RestClient) NewGetAllTickersRequest() *GetAllTickersRequest {

View File

@ -117,7 +117,7 @@ func (g *GetAllTickersRequest) Do(ctx context.Context) ([]Ticker, error) {
apiURL := "/api/spot/v1/market/tickers"
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
req, err := g.client.NewRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}

View File

@ -0,0 +1,31 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type Candle struct {
Open fixedpoint.Value `json:"open"`
High fixedpoint.Value `json:"high"`
Low fixedpoint.Value `json:"low"`
Close fixedpoint.Value `json:"close"`
QuoteVol fixedpoint.Value `json:"quoteVol"`
BaseVol fixedpoint.Value `json:"baseVol"`
UsdtVol fixedpoint.Value `json:"usdtVol"`
Ts types.MillisecondTimestamp `json:"ts"`
}
//go:generate GetRequest -url "/api/spot/v1/market/candles" -type GetCandlesRequest -responseDataType []Candle
type GetCandlesRequest struct {
client requestgen.APIClient
}
func (c *RestClient) NewGetCandlesRequest() *GetCandlesRequest {
return &GetCandlesRequest{client: c}
}

View File

@ -0,0 +1,139 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/spot/v1/market/candles -type GetCandlesRequest -responseDataType []Candle"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetCandlesRequest) 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 *GetCandlesRequest) 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 *GetCandlesRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetCandlesRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetCandlesRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetCandlesRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetCandlesRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetCandlesRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetCandlesRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetCandlesRequest) Do(ctx context.Context) ([]Candle, error) {
// no body params
var params interface{}
query := url.Values{}
apiURL := "/api/spot/v1/market/candles"
req, err := g.client.NewRequest(ctx, "GET", apiURL, 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 []Candle
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return data, nil
}

View File

@ -13,7 +13,7 @@ type ServerTime = types.MillisecondTimestamp
//go:generate GetRequest -url "/api/spot/v1/public/time" -type GetServerTimeRequest -responseDataType .ServerTime
type GetServerTimeRequest struct {
client requestgen.AuthenticatedAPIClient
client requestgen.APIClient
}
func (c *RestClient) NewGetServerTimeRequest() *GetServerTimeRequest {

View File

@ -118,7 +118,7 @@ func (g *GetServerTimeRequest) Do(ctx context.Context) (*types.MillisecondTimest
apiURL := "/api/spot/v1/public/time"
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
req, err := g.client.NewRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}

View File

@ -28,7 +28,7 @@ type Symbol struct {
//go:generate GetRequest -url "/api/spot/v1/public/products" -type GetSymbolsRequest -responseDataType []Symbol
type GetSymbolsRequest struct {
client requestgen.AuthenticatedAPIClient
client requestgen.APIClient
}
func (c *RestClient) NewGetSymbolsRequest() *GetSymbolsRequest {

View File

@ -117,7 +117,7 @@ func (g *GetSymbolsRequest) Do(ctx context.Context) ([]Symbol, error) {
apiURL := "/api/spot/v1/public/products"
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
req, err := g.client.NewRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}

View File

@ -30,7 +30,7 @@ type Ticker struct {
//go:generate GetRequest -url "/api/spot/v1/market/ticker" -type GetTickerRequest -responseDataType .Ticker
type GetTickerRequest struct {
client requestgen.AuthenticatedAPIClient
client requestgen.APIClient
}
func (c *RestClient) NewGetTickerRequest() *GetTickerRequest {

View File

@ -117,7 +117,7 @@ func (g *GetTickerRequest) Do(ctx context.Context) (*Ticker, error) {
apiURL := "/api/spot/v1/market/ticker"
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
req, err := g.client.NewRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}