diff --git a/pkg/exchange/bitget/bitgetapi/v2/client_test.go b/pkg/exchange/bitget/bitgetapi/v2/client_test.go index f8eef7862..3873b1479 100644 --- a/pkg/exchange/bitget/bitgetapi/v2/client_test.go +++ b/pkg/exchange/bitget/bitgetapi/v2/client_test.go @@ -101,4 +101,10 @@ func TestClient(t *testing.T) { assert.NoError(t, err) t.Logf("resp: %+v", resp) }) + + t.Run("GetAccountAssetsRequest", func(t *testing.T) { + resp, err := client.NewGetAccountAssetsRequest().AssetType(AssetTypeHoldOnly).Do(ctx) + assert.NoError(t, err) + t.Logf("resp: %+v", resp) + }) } diff --git a/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request.go b/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request.go new file mode 100644 index 000000000..d87468143 --- /dev/null +++ b/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request.go @@ -0,0 +1,39 @@ +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 AccountAsset struct { + Coin string `json:"coin"` + Available fixedpoint.Value `json:"available"` + Frozen fixedpoint.Value `json:"frozen"` + Locked fixedpoint.Value `json:"locked"` + LimitAvailable fixedpoint.Value `json:"limitAvailable"` + UpdatedTime types.MillisecondTimestamp `json:"uTime"` +} + +type AssetType string + +const ( + AssetTypeHoldOnly AssetType = "hold_only" + AssetTypeHAll AssetType = "all" +) + +//go:generate GetRequest -url "/api/v2/spot/account/assets" -type GetAccountAssetsRequest -responseDataType []AccountAsset +type GetAccountAssetsRequest struct { + client requestgen.AuthenticatedAPIClient + + coin *string `param:"symbol,query"` + assetType AssetType `param:"limit,query"` +} + +func (c *Client) NewGetAccountAssetsRequest() *GetAccountAssetsRequest { + return &GetAccountAssetsRequest{client: c.Client} +} diff --git a/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request_requestgen.go b/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request_requestgen.go new file mode 100644 index 000000000..945b25675 --- /dev/null +++ b/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request_requestgen.go @@ -0,0 +1,195 @@ +// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/v2/spot/account/assets -type GetAccountAssetsRequest -responseDataType []AccountAsset"; DO NOT EDIT. + +package bitgetapi + +import ( + "context" + "encoding/json" + "fmt" + "github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi" + "net/url" + "reflect" + "regexp" +) + +func (c *GetAccountAssetsRequest) Coin(coin string) *GetAccountAssetsRequest { + c.coin = &coin + return c +} + +func (c *GetAccountAssetsRequest) AssetType(assetType AssetType) *GetAccountAssetsRequest { + c.assetType = assetType + return c +} + +// GetQueryParameters builds and checks the query parameters and returns url.Values +func (c *GetAccountAssetsRequest) GetQueryParameters() (url.Values, error) { + var params = map[string]interface{}{} + // check coin field -> json key symbol + if c.coin != nil { + coin := *c.coin + + // assign parameter of coin + params["symbol"] = coin + } else { + } + // check assetType field -> json key limit + assetType := c.assetType + + // TEMPLATE check-valid-values + switch assetType { + case AssetTypeHoldOnly, AssetTypeHAll: + params["limit"] = assetType + + default: + return nil, fmt.Errorf("limit value %v is invalid", assetType) + + } + // END TEMPLATE check-valid-values + + // assign parameter of assetType + params["limit"] = assetType + + 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 (c *GetAccountAssetsRequest) 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 (c *GetAccountAssetsRequest) GetParametersQuery() (url.Values, error) { + query := url.Values{} + + params, err := c.GetParameters() + if err != nil { + return query, err + } + + for _k, _v := range params { + if c.isVarSlice(_v) { + c.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 (c *GetAccountAssetsRequest) GetParametersJSON() ([]byte, error) { + params, err := c.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 (c *GetAccountAssetsRequest) GetSlugParameters() (map[string]interface{}, error) { + var params = map[string]interface{}{} + + return params, nil +} + +func (c *GetAccountAssetsRequest) 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 (c *GetAccountAssetsRequest) 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 (c *GetAccountAssetsRequest) isVarSlice(_v interface{}) bool { + rt := reflect.TypeOf(_v) + switch rt.Kind() { + case reflect.Slice: + return true + } + return false +} + +func (c *GetAccountAssetsRequest) GetSlugsMap() (map[string]string, error) { + slugs := map[string]string{} + params, err := c.GetSlugParameters() + if err != nil { + return slugs, nil + } + + for _k, _v := range params { + slugs[_k] = fmt.Sprintf("%v", _v) + } + + return slugs, nil +} + +// GetPath returns the request path of the API +func (c *GetAccountAssetsRequest) GetPath() string { + return "/api/v2/spot/account/assets" +} + +// Do generates the request object and send the request object to the API endpoint +func (c *GetAccountAssetsRequest) Do(ctx context.Context) ([]AccountAsset, error) { + + // no body params + var params interface{} + query, err := c.GetQueryParameters() + if err != nil { + return nil, err + } + + var apiURL string + + apiURL = c.GetPath() + + req, err := c.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params) + if err != nil { + return nil, err + } + + response, err := c.client.SendRequest(req) + if err != nil { + return nil, err + } + + var apiResponse bitgetapi.APIResponse + if err := response.DecodeJSON(&apiResponse); err != nil { + return nil, err + } + + type responseValidator interface { + Validate() error + } + validator, ok := interface{}(apiResponse).(responseValidator) + if ok { + if err := validator.Validate(); err != nil { + return nil, err + } + } + var data []AccountAsset + if err := json.Unmarshal(apiResponse.Data, &data); err != nil { + return nil, err + } + return data, nil +}