This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
/
feed.go
76 lines (67 loc) · 2.42 KB
/
feed.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
package starling
import (
"context"
"net/http"
"time"
)
// Feed is a slice of Items representing customer transactions
type feed struct {
Items []Item `json:"feedItems"`
}
// Item is a single customer transaction in their feed
type Item struct {
FeedItemUID string `json:"feedItemUid"`
CategoryUID string `json:"categoryUid"`
Amount Amount `json:"amount"`
SourceAmount Amount `json:"sourceAmount"`
Direction string `json:"direction"`
TransactionTime time.Time `json:"transactionTime"`
Source string `json:"source"`
SourceSubType string `json:"sourceSubType"`
Status string `json:"status"`
CounterPartyType string `json:"counterPartyType"`
CounterPartyUID string `json:"counterPartyUid"`
CounterPartySubEntityUID string `json:"counterPartySubEntityUid"`
Reference string `json:"reference"`
Country string `json:"country"`
SpendingCategory string `json:"spendingCategory"`
}
// FeedOpts defines options that can be passed when requesting a feed
type FeedOpts struct {
Since time.Time
}
// Feed returns a slice of Items for a given account and category. It returns an error if unable
// to retrieve the feed.
// Note: Feed uses the v2 API which is still under active development.
func (c *Client) Feed(ctx context.Context, act, cat string, opts *FeedOpts) ([]Item, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v2/feed/account/"+act+"/category/"+cat, nil)
if err != nil {
return nil, nil, err
}
if opts != nil {
q := req.URL.Query()
q.Add("changesSince", opts.Since.Format(time.RFC3339Nano))
req.URL.RawQuery = q.Encode()
}
var f feed
resp, err := c.Do(ctx, req, &f)
if err != nil {
return nil, resp, err
}
return f.Items, resp, nil
}
// FeedItem returns a feed Item for a given account and category. It returns an error if unable to
// retrieve the feed Item.
// Note: FeedItem uses the v2 API which is still under active development.
func (c *Client) FeedItem(ctx context.Context, act, cat, itm string) (*Item, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v2/feed/account/"+act+"/category/"+cat+"/"+itm, nil)
if err != nil {
return nil, nil, err
}
var i Item
resp, err := c.Do(ctx, req, &i)
if err != nil {
return nil, resp, err
}
return &i, resp, nil
}