-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts.go
48 lines (37 loc) · 890 Bytes
/
accounts.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
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func listAccounts(c *MonzoClient) error {
path := "accounts"
requestURL := fmt.Sprintf("%s/%s", c.endpoints["APIURL"], path)
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.accessToken))
rsp, err := c.Do(req)
if err != nil {
return err
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", rsp.StatusCode)
}
if rsp.Body == nil {
return fmt.Errorf("response body is empty")
}
var accountsResp AccountsResp
err = json.NewDecoder(rsp.Body).Decode(&accountsResp)
if err != nil {
return err
}
for _, account := range accountsResp.Accounts {
if !account.Closed {
fmt.Printf("Account: %s - %s\n", account.Type, account.ID)
}
}
return nil
}