-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
252 lines (218 loc) · 6.37 KB
/
client.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package ynab
import (
"context"
"net/url"
"time"
"github.com/kevinburke/go-types"
"github.com/kevinburke/rest"
)
type Client struct {
*rest.Client
Accounts *AccountService
Budgets *BudgetService
Categories *CategoryService
Transactions *TransactionService
}
type TransactionListResponse struct {
Data TransactionListWrapper `json:"data"`
}
type CategoryListResponse struct {
Data CategoryListWrapper `json:"data"`
}
type CategoryListWrapper struct {
CategoryGroups []*CategoryGroup `json:"category_groups"`
}
type CategoryGroup struct {
ID string
Name string
Hidden bool
Deleted bool
Categories []*Category
}
type Category struct {
ID string
Name string
CategoryGroupID string `json:"category_group_id"`
Note string
Hidden bool
Budgeted int64
Activity int64
}
type TransactionListWrapper struct {
Transactions []*Transaction `json:"transactions"`
}
type ScheduledTransactionListResponse struct {
Data ScheduledTransactionListWrapper `json:"data"`
}
type ScheduledTransactionListWrapper struct {
ScheduledTransactions []*ScheduledTransaction `json:"scheduled_transactions"`
}
type Date time.Time
func (t *Date) UnmarshalJSON(b []byte) error {
t2, err := time.ParseInLocation(`"2006-01-02"`, string(b), time.Local)
if err != nil {
return err
}
*t = Date(t2)
return nil
}
func (t Date) String() string {
return time.Time(t).Format("2006-01-02")
}
func (t Date) GoString() string {
return time.Time(t).GoString()
}
type ScheduledTransaction struct {
AccountID string `json:"account_id"`
AccountName string `json:"account_name"`
Amount int64
Approved bool
CategoryName types.NullString `json:"category_name"`
Cleared string
DateFirst Date `json:"date_first"`
DateNext Date `json:"date_next"`
Deleted bool
Frequency string
ID string `json:"id"`
Memo string
PayeeName string `json:"payee_name"`
TransferAccountID types.NullString `json:"transfer_account_id"`
Subtransactions []Transaction `json:"subtransactions"`
}
type Transaction struct {
AccountID string `json:"account_id"`
AccountName string `json:"account_name"`
Amount int64
Approved bool
CategoryID types.NullString `json:"category_id"`
CategoryName types.NullString `json:"category_name"`
Cleared string
Date Date
Deleted bool
ID string `json:"id"`
Memo string
PayeeName string `json:"payee_name"`
TransferAccountID types.NullString `json:"transfer_account_id"`
TransferTransactionID types.NullString `json:"transfer_transaction_id"`
MatchedTransactionID types.NullString `json:"matched_transaction_id"`
Subtransactions []Transaction `json:"subtransactions"`
}
type AccountService struct {
client *Client
}
type Account struct {
ID string
Name string
Type string
OnBudget bool `json:"on_budget"`
Closed bool
Note string
Balance int64
StartingBalance int64 `json:"starting_balance"`
Deleted bool
}
func (a Account) CashBacked() bool {
return a.Type == "cash" || a.Type == "savings" || a.Type == "checking"
}
type AccountListResponse struct {
Data AccountListWrapper `json:"data"`
}
type AccountListWrapper struct {
Accounts []*Account `json:"accounts"`
}
type Budget struct {
ID string `json:"id"`
Name string `json:"name"`
}
type BudgetListResponse struct {
Data BudgetListWrapper `json:"data"`
}
type BudgetListWrapper struct {
Budgets []*Budget `json:"budgets"`
}
func (b *BudgetService) GetPage(ctx context.Context, data url.Values) (*BudgetListResponse, error) {
req, err := b.client.NewRequest("GET", "/budgets?"+data.Encode(), nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
budgetResp := new(BudgetListResponse)
if err := b.client.Do(req, budgetResp); err != nil {
return nil, err
}
return budgetResp, nil
}
func (b *BudgetService) GetAccounts(ctx context.Context, budgetID string, data url.Values) (*AccountListResponse, error) {
req, err := b.client.NewRequest("GET", "/budgets/"+budgetID+"/accounts?"+data.Encode(), nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
accountResp := new(AccountListResponse)
if err := b.client.Do(req, accountResp); err != nil {
return nil, err
}
return accountResp, nil
}
func (b *BudgetService) GetTransactions(ctx context.Context, budgetID string, data url.Values) (*TransactionListResponse, error) {
req, err := b.client.NewRequest("GET", "/budgets/"+budgetID+"/transactions?"+data.Encode(), nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
transactionResp := new(TransactionListResponse)
if err := b.client.Do(req, transactionResp); err != nil {
return nil, err
}
return transactionResp, nil
}
func (b *BudgetService) GetScheduledTransactions(ctx context.Context, budgetID string, data url.Values) (*ScheduledTransactionListResponse, error) {
req, err := b.client.NewRequest("GET", "/budgets/"+budgetID+"/scheduled_transactions?"+data.Encode(), nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
transactionResp := new(ScheduledTransactionListResponse)
if err := b.client.Do(req, transactionResp); err != nil {
return nil, err
}
return transactionResp, nil
}
func (b *BudgetService) GetCategories(ctx context.Context, budgetID string, data url.Values) (*CategoryListResponse, error) {
req, err := b.client.NewRequest("GET", "/budgets/"+budgetID+"/categories?"+data.Encode(), nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
categoryResp := new(CategoryListResponse)
if err := b.client.Do(req, categoryResp); err != nil {
return nil, err
}
return categoryResp, nil
}
type BudgetService struct {
client *Client
}
type TransactionService struct {
client *Client
}
type CategoryService struct {
client *Client
}
func NewClient(token string) *Client {
client := rest.NewBearerClient(token, "https://api.youneedabudget.com/v1")
c := &Client{Client: client}
c.Accounts = &AccountService{
client: c,
}
c.Budgets = &BudgetService{
client: c,
}
c.Transactions = &TransactionService{
client: c,
}
c.Categories = &CategoryService{
client: c,
}
return c
}