forked from McKael/madon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlists.go
136 lines (121 loc) · 3.81 KB
/
lists.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
/*
Copyright 2018 Mikael Berthe
Licensed under the MIT license. Please see the LICENSE file is this directory.
*/
package madon
import (
"fmt"
"strconv"
"github.com/pkg/errors"
"github.com/sendgrid/rest"
)
// GetList returns a List entity
func (mc *Client) GetList(listID int64) (*List, error) {
if listID <= 0 {
return nil, errors.New("invalid list ID")
}
endPoint := "lists/" + strconv.FormatInt(listID, 10)
var list List
if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, nil, nil, &list); err != nil {
return nil, err
}
return &list, nil
}
// GetLists returns a list of List entities
// If accountID is > 0, this will return the lists containing this account.
// If lopt.All is true, several requests will be made until the API server
// has nothing to return.
func (mc *Client) GetLists(accountID int64, lopt *LimitParams) ([]List, error) {
endPoint := "lists"
if accountID > 0 {
endPoint = "accounts/" + strconv.FormatInt(accountID, 10) + "/lists"
}
var lists []List
var links apiLinks
if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, lopt, &links, &lists); err != nil {
return nil, err
}
if lopt != nil { // Fetch more pages to reach our limit
var listSlice []List
for (lopt.All || lopt.Limit > len(lists)) && links.next != nil {
newlopt := links.next
links = apiLinks{}
if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, newlopt, &links, &listSlice); err != nil {
return nil, err
}
lists = append(lists, listSlice...)
listSlice = listSlice[:0] // Clear struct
}
}
return lists, nil
}
// CreateList creates a List
func (mc *Client) CreateList(title string) (*List, error) {
params := apiCallParams{"title": title}
method := rest.Post
return mc.setSingleList(method, 0, params)
}
// UpdateList updates an existing List
func (mc *Client) UpdateList(listID int64, title string) (*List, error) {
if listID <= 0 {
return nil, errors.New("invalid list ID")
}
params := apiCallParams{"title": title}
method := rest.Put
return mc.setSingleList(method, listID, params)
}
// DeleteList deletes a list
func (mc *Client) DeleteList(listID int64) error {
if listID <= 0 {
return errors.New("invalid list ID")
}
method := rest.Delete
_, err := mc.setSingleList(method, listID, nil)
return err
}
// GetListAccounts returns the accounts belonging to a given list
func (mc *Client) GetListAccounts(listID int64, lopt *LimitParams) ([]Account, error) {
endPoint := "lists/" + strconv.FormatInt(listID, 10) + "/accounts"
return mc.getMultipleAccounts(endPoint, nil, lopt)
}
// AddListAccounts adds the accounts to a given list
func (mc *Client) AddListAccounts(listID int64, accountIDs []int64) error {
endPoint := "lists/" + strconv.FormatInt(listID, 10) + "/accounts"
method := rest.Post
params := make(apiCallParams)
for i, id := range accountIDs {
if id < 1 {
return ErrInvalidID
}
qID := fmt.Sprintf("[%d]account_ids", i)
params[qID] = strconv.FormatInt(id, 10)
}
return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil)
}
// RemoveListAccounts removes the accounts from the given list
func (mc *Client) RemoveListAccounts(listID int64, accountIDs []int64) error {
endPoint := "lists/" + strconv.FormatInt(listID, 10) + "/accounts"
method := rest.Delete
params := make(apiCallParams)
for i, id := range accountIDs {
if id < 1 {
return ErrInvalidID
}
qID := fmt.Sprintf("[%d]account_ids", i)
params[qID] = strconv.FormatInt(id, 10)
}
return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil)
}
func (mc *Client) setSingleList(method rest.Method, listID int64, params apiCallParams) (*List, error) {
var endPoint string
if listID > 0 {
endPoint = "lists/" + strconv.FormatInt(listID, 10)
} else {
endPoint = "lists"
}
var list List
if err := mc.apiCall("v1/"+endPoint, method, params, nil, nil, &list); err != nil {
return nil, err
}
return &list, nil
}