add GetAccount api

This commit is contained in:
c9s 2021-12-11 00:34:49 +08:00
parent 76f122d998
commit be7e9f551a
3 changed files with 38 additions and 3 deletions

View File

@ -26,7 +26,7 @@ var rootCmd = &cobra.Command{
SilenceUsage: true, SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
accounts, err := client.AccountService.QueryAccounts() accounts, err := client.AccountService.ListAccounts()
if err != nil { if err != nil {
return err return err
} }

View File

@ -12,7 +12,17 @@ var accountsCmd = &cobra.Command{
SilenceUsage: true, SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
accounts, err := client.AccountService.QueryAccounts() if len(args) > 0 {
account, err := client.AccountService.GetAccount(args[0])
if err != nil {
return err
}
logrus.Infof("account: %+v", account)
return nil
}
accounts, err := client.AccountService.ListAccounts()
if err != nil { if err != nil {
return err return err
} }
@ -21,3 +31,4 @@ var accountsCmd = &cobra.Command{
return nil return nil
}, },
} }

View File

@ -46,7 +46,7 @@ type Account struct {
Holds fixedpoint.Value `json:"holds"` Holds fixedpoint.Value `json:"holds"`
} }
func (s *AccountService) QueryAccounts() ([]Account, error) { func (s *AccountService) ListAccounts() ([]Account, error) {
req, err := s.client.newAuthenticatedRequest("GET", "/api/v1/accounts", nil, nil) req, err := s.client.newAuthenticatedRequest("GET", "/api/v1/accounts", nil, nil)
if err != nil { if err != nil {
return nil, err return nil, err
@ -67,5 +67,29 @@ func (s *AccountService) QueryAccounts() ([]Account, error) {
return nil, err return nil, err
} }
return apiResponse.Data, nil
}
func (s *AccountService) GetAccount(accountID string) (*Account, error) {
req, err := s.client.newAuthenticatedRequest("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 return apiResponse.Data, nil
} }