From b8b5ccdd2df893dff0b42fb4cf6a94ebf710db28 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 31 Dec 2021 01:23:16 +0800 Subject: [PATCH] kucoin: refactor account service with requestgen --- examples/kucoin/accounts.go | 2 +- examples/kucoin/subaccounts.go | 4 +- go.mod | 2 +- go.sum | 2 + pkg/exchange/kucoin/kucoinapi/account.go | 96 ++++--------- .../new_get_account_request_requestgen.go | 131 ++++++++++++++++++ .../new_list_accounts_request_requestgen.go | 115 +++++++++++++++ ...ew_list_sub_accounts_request_requestgen.go | 115 +++++++++++++++ 8 files changed, 398 insertions(+), 69 deletions(-) create mode 100644 pkg/exchange/kucoin/kucoinapi/new_get_account_request_requestgen.go create mode 100644 pkg/exchange/kucoin/kucoinapi/new_list_accounts_request_requestgen.go create mode 100644 pkg/exchange/kucoin/kucoinapi/new_list_sub_accounts_request_requestgen.go diff --git a/examples/kucoin/accounts.go b/examples/kucoin/accounts.go index 5e8ec6d4e..676bc4f80 100644 --- a/examples/kucoin/accounts.go +++ b/examples/kucoin/accounts.go @@ -19,7 +19,7 @@ var accountsCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { if len(args) > 0 { - account, err := client.AccountService.GetAccount(args[0]) + account, err := client.AccountService.GetAccount(context.Background(), args[0]) if err != nil { return err } diff --git a/examples/kucoin/subaccounts.go b/examples/kucoin/subaccounts.go index b3eae2bfe..db4a96c61 100644 --- a/examples/kucoin/subaccounts.go +++ b/examples/kucoin/subaccounts.go @@ -1,6 +1,8 @@ package main import ( + "context" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -17,7 +19,7 @@ var subAccountsCmd = &cobra.Command{ SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { - subAccounts, err := client.AccountService.QuerySubAccounts() + subAccounts, err := client.AccountService.ListSubAccounts(context.Background()) if err != nil { return err } diff --git a/go.mod b/go.mod index f87323611..03725fd2c 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ go 1.13 require ( github.com/DATA-DOG/go-sqlmock v1.5.0 github.com/adshao/go-binance/v2 v2.3.3 - github.com/c9s/requestgen v1.1.0 + github.com/c9s/requestgen v1.1.1-0.20211230171502-c042072e23cd github.com/c9s/rockhopper v1.2.1-0.20210217093258-2661955904a9 github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/codingconcepts/env v0.0.0-20200821220118-a8fbf8d84482 diff --git a/go.sum b/go.sum index d3c33948f..14e03d0e6 100644 --- a/go.sum +++ b/go.sum @@ -64,6 +64,8 @@ github.com/c9s/requestgen v1.0.1 h1:gmgs0WA40T30GdehW//QrmAdhVpKEi6MzYaeaUEx9Os= github.com/c9s/requestgen v1.0.1/go.mod h1:5n9FU3hr5307IiXAmbMiZbHYaPiys1u9jCWYexZr9qA= github.com/c9s/requestgen v1.1.0 h1:pUFm7/6WlrIJrDy0o3vOtxiK4hyCOqml4HVByAeKJ+g= github.com/c9s/requestgen v1.1.0/go.mod h1:5n9FU3hr5307IiXAmbMiZbHYaPiys1u9jCWYexZr9qA= +github.com/c9s/requestgen v1.1.1-0.20211230171502-c042072e23cd h1:o87kZ8aHtxA1ZduOV2Z55T4PX2xM4OTGH/Z9W5AjKnU= +github.com/c9s/requestgen v1.1.1-0.20211230171502-c042072e23cd/go.mod h1:5n9FU3hr5307IiXAmbMiZbHYaPiys1u9jCWYexZr9qA= github.com/c9s/rockhopper v1.2.1-0.20210217093258-2661955904a9 h1:Wlr5DjDOf5Kygoo0LoUthxwAhNwLEXMWHqCKXbMHCsw= github.com/c9s/rockhopper v1.2.1-0.20210217093258-2661955904a9/go.mod h1:KJnQjZSrWA83jjwGF/+O7Y96VCVirYTYEvXJJOc6kMU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= diff --git a/pkg/exchange/kucoin/kucoinapi/account.go b/pkg/exchange/kucoin/kucoinapi/account.go index a6dbb3f9a..271e2537d 100644 --- a/pkg/exchange/kucoin/kucoinapi/account.go +++ b/pkg/exchange/kucoin/kucoinapi/account.go @@ -1,8 +1,13 @@ 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" + "github.com/c9s/requestgen" + "github.com/c9s/bbgo/pkg/fixedpoint" ) @@ -10,6 +15,21 @@ type AccountService struct { client *RestClient } +func (s *AccountService) ListSubAccounts(ctx context.Context) ([]SubAccount, error) { + req := &NewListSubAccountsRequest{client: s.client} + return req.Do(ctx) +} + +func (s *AccountService) ListAccounts(ctx context.Context) ([]Account, error) { + req := &NewListAccountsRequest{client: s.client} + return req.Do(ctx) +} + +func (s *AccountService) GetAccount(ctx context.Context, accountID string) (*Account, error) { + req := &NewGetAccountRequest{client: s.client, accountID: accountID} + return req.Do(ctx) +} + type SubAccount struct { UserID string `json:"userId"` Name string `json:"subName"` @@ -17,28 +37,9 @@ type SubAccount struct { Remark string `json:"remarks"` } -func (s *AccountService) QuerySubAccounts() ([]SubAccount, error) { - req, err := s.client.NewAuthenticatedRequest(context.Background(), "GET", "/api/v1/sub/user", 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 []SubAccount `json:"data"` - } - - if err := response.DecodeJSON(&apiResponse); err != nil { - return nil, err - } - - return apiResponse.Data, nil +//go:generate GetRequest -url "/api/v1/sub/user" -type NewListSubAccountsRequest -responseDataType []SubAccount +type NewListSubAccountsRequest struct { + client requestgen.AuthenticatedAPIClient } type Account struct { @@ -50,50 +51,13 @@ type Account struct { Holds fixedpoint.Value `json:"holds"` } -func (s *AccountService) ListAccounts(ctx context.Context) ([]Account, error) { - req, err := s.client.NewAuthenticatedRequest(ctx, "GET", "/api/v1/accounts", 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 []Account `json:"data"` - } - - if err := response.DecodeJSON(&apiResponse); err != nil { - return nil, err - } - - return apiResponse.Data, nil +//go:generate GetRequest -url "/api/v1/accounts" -type NewListAccountsRequest -responseDataType []Account +type NewListAccountsRequest struct { + client requestgen.AuthenticatedAPIClient } -func (s *AccountService) GetAccount(accountID string) (*Account, error) { - req, err := s.client.NewAuthenticatedRequest(context.Background(), "GET", "/api/v1/accounts/"+accountID, 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 *Account `json:"data"` - } - - if err := response.DecodeJSON(&apiResponse); err != nil { - return nil, err - } - - return apiResponse.Data, nil +//go:generate GetRequest -url "/api/v1/accounts/:accountID" -type NewGetAccountRequest -responseDataType .Account +type NewGetAccountRequest struct { + client requestgen.AuthenticatedAPIClient + accountID string `param:"accountID,slug"` } diff --git a/pkg/exchange/kucoin/kucoinapi/new_get_account_request_requestgen.go b/pkg/exchange/kucoin/kucoinapi/new_get_account_request_requestgen.go new file mode 100644 index 000000000..5f6c96ae0 --- /dev/null +++ b/pkg/exchange/kucoin/kucoinapi/new_get_account_request_requestgen.go @@ -0,0 +1,131 @@ +// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/v1/accounts/:accountID -type NewGetAccountRequest -responseDataType .Account"; DO NOT EDIT. + +package kucoinapi + +import ( + "context" + "encoding/json" + "fmt" + "net/url" + "regexp" +) + +func (n *NewGetAccountRequest) AccountID(accountID string) *NewGetAccountRequest { + n.accountID = accountID + return n +} + +// GetQueryParameters builds and checks the query parameters and returns url.Values +func (n *NewGetAccountRequest) 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 (n *NewGetAccountRequest) 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 (n *NewGetAccountRequest) GetParametersQuery() (url.Values, error) { + query := url.Values{} + + params, err := n.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 (n *NewGetAccountRequest) GetParametersJSON() ([]byte, error) { + params, err := n.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 (n *NewGetAccountRequest) GetSlugParameters() (map[string]interface{}, error) { + var params = map[string]interface{}{} + // check accountID field -> json key accountID + accountID := n.accountID + + // assign parameter of accountID + params["accountID"] = accountID + + return params, nil +} + +func (n *NewGetAccountRequest) 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 (n *NewGetAccountRequest) GetSlugsMap() (map[string]string, error) { + slugs := map[string]string{} + params, err := n.GetSlugParameters() + if err != nil { + return slugs, nil + } + + for k, v := range params { + slugs[k] = fmt.Sprintf("%v", v) + } + + return slugs, nil +} + +func (n *NewGetAccountRequest) Do(ctx context.Context) (*Account, error) { + + // no body params + var params interface{} + query := url.Values{} + + apiURL := "/api/v1/accounts/:accountID" + slugs, err := n.GetSlugsMap() + if err != nil { + return nil, err + } + + apiURL = n.applySlugsToUrl(apiURL, slugs) + + req, err := n.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params) + if err != nil { + return nil, err + } + + response, err := n.client.SendRequest(req) + if err != nil { + return nil, err + } + + var apiResponse APIResponse + if err := response.DecodeJSON(&apiResponse); err != nil { + return nil, err + } + var data Account + if err := json.Unmarshal(apiResponse.Data, &data); err != nil { + return nil, err + } + return &data, nil +} diff --git a/pkg/exchange/kucoin/kucoinapi/new_list_accounts_request_requestgen.go b/pkg/exchange/kucoin/kucoinapi/new_list_accounts_request_requestgen.go new file mode 100644 index 000000000..d56c9a655 --- /dev/null +++ b/pkg/exchange/kucoin/kucoinapi/new_list_accounts_request_requestgen.go @@ -0,0 +1,115 @@ +// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/v1/accounts -type NewListAccountsRequest -responseDataType []Account"; DO NOT EDIT. + +package kucoinapi + +import ( + "context" + "encoding/json" + "fmt" + "net/url" + "regexp" +) + +// GetQueryParameters builds and checks the query parameters and returns url.Values +func (n *NewListAccountsRequest) 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 (n *NewListAccountsRequest) 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 (n *NewListAccountsRequest) GetParametersQuery() (url.Values, error) { + query := url.Values{} + + params, err := n.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 (n *NewListAccountsRequest) GetParametersJSON() ([]byte, error) { + params, err := n.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 (n *NewListAccountsRequest) GetSlugParameters() (map[string]interface{}, error) { + var params = map[string]interface{}{} + + return params, nil +} + +func (n *NewListAccountsRequest) 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 (n *NewListAccountsRequest) GetSlugsMap() (map[string]string, error) { + slugs := map[string]string{} + params, err := n.GetSlugParameters() + if err != nil { + return slugs, nil + } + + for k, v := range params { + slugs[k] = fmt.Sprintf("%v", v) + } + + return slugs, nil +} + +func (n *NewListAccountsRequest) Do(ctx context.Context) ([]Account, error) { + + // no body params + var params interface{} + query := url.Values{} + + apiURL := "/api/v1/accounts" + + req, err := n.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params) + if err != nil { + return nil, err + } + + response, err := n.client.SendRequest(req) + if err != nil { + return nil, err + } + + var apiResponse APIResponse + if err := response.DecodeJSON(&apiResponse); err != nil { + return nil, err + } + var data []Account + if err := json.Unmarshal(apiResponse.Data, &data); err != nil { + return nil, err + } + return data, nil +} diff --git a/pkg/exchange/kucoin/kucoinapi/new_list_sub_accounts_request_requestgen.go b/pkg/exchange/kucoin/kucoinapi/new_list_sub_accounts_request_requestgen.go new file mode 100644 index 000000000..9cf5694ca --- /dev/null +++ b/pkg/exchange/kucoin/kucoinapi/new_list_sub_accounts_request_requestgen.go @@ -0,0 +1,115 @@ +// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/v1/sub/user -type NewListSubAccountsRequest -responseDataType []SubAccount"; DO NOT EDIT. + +package kucoinapi + +import ( + "context" + "encoding/json" + "fmt" + "net/url" + "regexp" +) + +// GetQueryParameters builds and checks the query parameters and returns url.Values +func (n *NewListSubAccountsRequest) 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 (n *NewListSubAccountsRequest) 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 (n *NewListSubAccountsRequest) GetParametersQuery() (url.Values, error) { + query := url.Values{} + + params, err := n.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 (n *NewListSubAccountsRequest) GetParametersJSON() ([]byte, error) { + params, err := n.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 (n *NewListSubAccountsRequest) GetSlugParameters() (map[string]interface{}, error) { + var params = map[string]interface{}{} + + return params, nil +} + +func (n *NewListSubAccountsRequest) 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 (n *NewListSubAccountsRequest) GetSlugsMap() (map[string]string, error) { + slugs := map[string]string{} + params, err := n.GetSlugParameters() + if err != nil { + return slugs, nil + } + + for k, v := range params { + slugs[k] = fmt.Sprintf("%v", v) + } + + return slugs, nil +} + +func (n *NewListSubAccountsRequest) Do(ctx context.Context) ([]SubAccount, error) { + + // no body params + var params interface{} + query := url.Values{} + + apiURL := "/api/v1/sub/user" + + req, err := n.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params) + if err != nil { + return nil, err + } + + response, err := n.client.SendRequest(req) + if err != nil { + return nil, err + } + + var apiResponse APIResponse + if err := response.DecodeJSON(&apiResponse); err != nil { + return nil, err + } + var data []SubAccount + if err := json.Unmarshal(apiResponse.Data, &data); err != nil { + return nil, err + } + return data, nil +}