-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
plaid.go
60 lines (50 loc) · 1.73 KB
/
plaid.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
package lunchmoney
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/Rhymond/go-money"
"github.com/go-playground/validator/v10"
)
// PlaidAccountsResponse is a list plaid accounts response.
type PlaidAccountsResponse struct {
PlaidAccounts []*PlaidAccount `json:"plaid_accounts"`
}
// PlaidAccount is a single LM Plaid account.
type PlaidAccount struct {
ID int64 `json:"id"`
DateLinked time.Time `json:"date_linked"`
Name string `json:"name"`
Type string `json:"type"`
Subtype string `json:"subtype"`
Mask string `json:"mask"`
InstitutionName string `json:"institution_name"`
Status string `json:"status"`
LastImport time.Time `json:"last_import"`
Balance string `json:"balance"`
Currency string `json:"currency"`
BalanceLastUpdate time.Time `json:"balance_last_update"`
Limit int64 `json:"limit"`
}
// ParsedAmount turns the currency from lunchmoney into a Go currency.
func (p *PlaidAccount) ParsedAmount() (*money.Money, error) {
return ParseCurrency(p.Balance, p.Currency)
}
// GetPlaidAccounts gets all plaid accounts filtered by the filters.
func (c *Client) GetPlaidAccounts(ctx context.Context) ([]*PlaidAccount, error) {
validate := validator.New()
options := map[string]string{}
body, err := c.Get(ctx, "/v1/plaid_accounts", options)
if err != nil {
return nil, fmt.Errorf("get plaid accounts: %w", err)
}
resp := &PlaidAccountsResponse{}
if err := json.NewDecoder(body).Decode(resp); err != nil {
return nil, fmt.Errorf("decode response: %w", err)
}
if err := validate.Struct(resp); err != nil {
return nil, err
}
return resp.PlaidAccounts, nil
}