-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccounts.go
88 lines (73 loc) · 2.74 KB
/
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
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
package up
import (
"context"
"net/http"
"strconv"
"strings"
"go.opentelemetry.io/otel"
)
// AccountsPaginationWrapper is a pagination wrapperfor a slice of AccountData.
// This type is used in organizing paginated account data received from the API.
type AccountsPaginationWrapper WrapperSlice[AccountDataWrapper]
// ListAccountsOption defines the available options used to configure the
// ListAccounts function when listing accounts from the API.
type ListAccountsOption struct {
listOption
}
// ListAccountsOptionPageSize sets the page size used when when listing
// accounts from the API. This option affects how many accounts are returned at
// once - increasing this can improve performance as the number of API calls
// is reduced.
func ListAccountsOptionPageSize(size int) ListAccountsOption {
return ListAccountsOption{newListOption("page[size]", strconv.Itoa(size))}
}
// ListAccountsOptionFilterAccountType filters the accounts returned from the
// API to those who are either "SAVER", "TRANSACTIONAL", or "HOME_LOAN". Use
// this option if, for example, you'd like to list all accounts that are
// transactional and not associated with your savings or your home loan.
func ListAccountsOptionFilterAccountType(t AccountType) ListAccountsOption {
return ListAccountsOption{newListOption("filter[accountType]", string(t))}
}
// ListAccountsOptionFilterAccountOwnershipType filters the accounts returned
// from the API to those who are either "INDIVIDUAL" or "JOINT". Use this option
// if, for example, you'd like to list all accounts that belong to you only..
func ListAccountsOptionFilterAccountOwnershipType(t AccountOwnershipType) ListAccountsOption {
return ListAccountsOption{newListOption("filter[ownershipType]", string(t))}
}
// ListAccounts returns a list of ALL accounts associated with authed user from
// the API. This function supports pagination, and is configurable by the given
// ListAccountsOptions options.
// https://developer.up.com.au/#get_accounts.
func (c *Client) ListAccounts(
ctx context.Context,
opts ...ListAccountsOption,
) (accounts []AccountResource, err error) {
// setup tracing.
newCtx, span := otel.Tracer(c.tracerName).Start(ctx, "ListAccounts")
defer span.End()
// setup request.
sr := senderRequest{
method: http.MethodGet,
path: "/accounts",
queries: setupQueries(opts),
}
// retrieve accounts.
for {
// get response.
var resp AccountsPaginationWrapper
if _, err := c.sender(newCtx, sr, &resp); err != nil {
return nil, err
}
// extract response data.
for _, a := range resp.Data {
accounts = append(accounts, a.Attributes)
}
// paginate?
if resp.Links.Next == "" {
break
}
sr.path = strings.Replace(resp.Links.Next, c.endpoint, "", 1)
sr.queries = nil
}
return accounts, nil
}