-
Notifications
You must be signed in to change notification settings - Fork 26
/
account.go
43 lines (35 loc) · 905 Bytes
/
account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package civogo
import (
"bytes"
"encoding/json"
)
// PaginatedAccounts returns a paginated list of Account object
type PaginatedAccounts struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
Pages int `json:"pages"`
Items []Account `json:"items"`
}
// ListAccounts lists all accounts
func (c *Client) ListAccounts() (*PaginatedAccounts, error) {
resp, err := c.SendGetRequest("/v2/accounts")
if err != nil {
return nil, decodeError(err)
}
accounts := &PaginatedAccounts{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&accounts); err != nil {
return nil, decodeError(err)
}
return accounts, nil
}
// GetAccountID returns the account ID
func (c *Client) GetAccountID() string {
accounts, err := c.ListAccounts()
if err != nil {
return ""
}
if len(accounts.Items) == 0 {
return "No account found"
}
return accounts.Items[0].ID
}