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
|
/*
Copyright 2018 Mikael Berthe
Licensed under the MIT license. Please see the LICENSE file is this directory.
*/
package madon
import (
"fmt"
"github.com/pkg/errors"
"github.com/sendgrid/rest"
)
// GetList returns a List entity
func (mc *Client) GetList(listID ActivityID) (*List, error) {
if listID == "" {
return nil, errors.New("invalid list ID")
}
endPoint := "lists/" + listID
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 ActivityID, lopt *LimitParams) ([]List, error) {
endPoint := "lists"
if accountID != "" {
endPoint = "accounts/" + accountID + "/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
for (lopt.All || lopt.Limit > len(lists)) && links.next != nil {
listSlice := []List{}
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...)
}
}
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, "", params)
}
// UpdateList updates an existing List
func (mc *Client) UpdateList(listID ActivityID, title string) (*List, error) {
if listID == "" {
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 ActivityID) error {
if listID == "" {
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 ActivityID, lopt *LimitParams) ([]Account, error) {
endPoint := "lists/" + listID + "/accounts"
return mc.getMultipleAccounts(endPoint, nil, lopt)
}
// AddListAccounts adds the accounts to a given list
func (mc *Client) AddListAccounts(listID ActivityID, accountIDs []ActivityID) error {
endPoint := "lists/" + listID + "/accounts"
method := rest.Post
params := make(apiCallParams)
for i, id := range accountIDs {
if id == "" {
return ErrInvalidID
}
qID := fmt.Sprintf("[%d]account_ids", i)
params[qID] = id
}
return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil)
}
// RemoveListAccounts removes the accounts from the given list
func (mc *Client) RemoveListAccounts(listID ActivityID, accountIDs []ActivityID) error {
endPoint := "lists/" + listID + "/accounts"
method := rest.Delete
params := make(apiCallParams)
for i, id := range accountIDs {
if id == "" {
return ErrInvalidID
}
qID := fmt.Sprintf("[%d]account_ids", i)
params[qID] = id
}
return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil)
}
func (mc *Client) setSingleList(method rest.Method, listID ActivityID, params apiCallParams) (*List, error) {
var endPoint string
if listID != "" {
endPoint = "lists/" + listID
} 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
}
|