-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallets.go
61 lines (51 loc) · 1.49 KB
/
wallets.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package itbit
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"net/url"
)
const walletsPerPage = 50
type Wallet struct {
ID string `json:"id"`
UserID string `json:"userId"`
Name string `json:"name"`
Balances []*WalletBalance `json:"balances"`
}
type WalletBalance struct {
Currency Currency `json:"currency"`
Available float64 `json:"availableBalance,string"`
Total float64 `json:"totalBalance,string"`
}
// GetAllWallets returns the wallets for the user ID in the given config
func GetAllWallets(conf *Config) ([]*Wallet, error) {
if conf.UserID == "" {
return nil, errors.New("no UserID on config")
}
return readAllWallets(conf, 1)
}
// readAllWallets recursively gets all wallets for the user ID in the conf
func readAllWallets(conf *Config, page int) ([]*Wallet, error) {
params := url.Values{
"userId": []string{conf.UserID},
"page": []string{fmt.Sprintf("%d", page)},
"perPage": []string{fmt.Sprintf("%d", walletsPerPage)},
}
resp, err := doReq(conf, "GET", fmt.Sprintf("/wallets?%s", params.Encode()), true, nil)
if err != nil {
return nil, errors.Wrapf(err,"getting page %d", page)
}
dec := json.NewDecoder(resp)
var wallets []*Wallet
if err := dec.Decode(&wallets); err != nil {
return nil, errors.Wrapf(err,"decoding page %d", page)
}
if len(wallets) >= walletsPerPage {
more, err := readAllWallets(conf, page+1)
if err != nil {
return nil, errors.Wrapf(err, "recursing after page %d", page)
}
wallets = append(wallets, more...)
}
return wallets, nil
}