kucoin: refactor account service with requestgen

This commit is contained in:
c9s 2021-12-31 01:23:16 +08:00
parent 5474a42f24
commit b8b5ccdd2d
8 changed files with 398 additions and 69 deletions

View File

@ -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
}

View File

@ -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
}

2
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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
//go:generate GetRequest -url "/api/v1/accounts" -type NewListAccountsRequest -responseDataType []Account
type NewListAccountsRequest struct {
client requestgen.AuthenticatedAPIClient
}
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
}
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"`
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}