1 /* |
|
2 Copyright 2018 Mikael Berthe |
|
3 |
|
4 Licensed under the MIT license. Please see the LICENSE file is this directory. |
|
5 */ |
|
6 |
|
7 package madon |
|
8 |
|
9 import ( |
|
10 "fmt" |
|
11 "strconv" |
|
12 |
|
13 "github.com/pkg/errors" |
|
14 "github.com/sendgrid/rest" |
|
15 ) |
|
16 |
|
17 // GetList returns a List entity |
|
18 func (mc *Client) GetList(listID int64) (*List, error) { |
|
19 if listID <= 0 { |
|
20 return nil, errors.New("invalid list ID") |
|
21 } |
|
22 endPoint := "lists/" + strconv.FormatInt(listID, 10) |
|
23 var list List |
|
24 if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, nil, nil, &list); err != nil { |
|
25 return nil, err |
|
26 } |
|
27 return &list, nil |
|
28 } |
|
29 |
|
30 // GetLists returns a list of List entities |
|
31 // If accountID is > 0, this will return the lists containing this account. |
|
32 // If lopt.All is true, several requests will be made until the API server |
|
33 // has nothing to return. |
|
34 func (mc *Client) GetLists(accountID int64, lopt *LimitParams) ([]List, error) { |
|
35 endPoint := "lists" |
|
36 |
|
37 if accountID > 0 { |
|
38 endPoint = "accounts/" + strconv.FormatInt(accountID, 10) + "/lists" |
|
39 } |
|
40 |
|
41 var lists []List |
|
42 var links apiLinks |
|
43 if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, lopt, &links, &lists); err != nil { |
|
44 return nil, err |
|
45 } |
|
46 if lopt != nil { // Fetch more pages to reach our limit |
|
47 var listSlice []List |
|
48 for (lopt.All || lopt.Limit > len(lists)) && links.next != nil { |
|
49 newlopt := links.next |
|
50 links = apiLinks{} |
|
51 if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, newlopt, &links, &listSlice); err != nil { |
|
52 return nil, err |
|
53 } |
|
54 lists = append(lists, listSlice...) |
|
55 listSlice = listSlice[:0] // Clear struct |
|
56 } |
|
57 } |
|
58 return lists, nil |
|
59 } |
|
60 |
|
61 // CreateList creates a List |
|
62 func (mc *Client) CreateList(title string) (*List, error) { |
|
63 params := apiCallParams{"title": title} |
|
64 method := rest.Post |
|
65 return mc.setSingleList(method, 0, params) |
|
66 } |
|
67 |
|
68 // UpdateList updates an existing List |
|
69 func (mc *Client) UpdateList(listID int64, title string) (*List, error) { |
|
70 if listID <= 0 { |
|
71 return nil, errors.New("invalid list ID") |
|
72 } |
|
73 params := apiCallParams{"title": title} |
|
74 method := rest.Put |
|
75 return mc.setSingleList(method, listID, params) |
|
76 } |
|
77 |
|
78 // DeleteList deletes a list |
|
79 func (mc *Client) DeleteList(listID int64) error { |
|
80 if listID <= 0 { |
|
81 return errors.New("invalid list ID") |
|
82 } |
|
83 method := rest.Delete |
|
84 _, err := mc.setSingleList(method, listID, nil) |
|
85 return err |
|
86 } |
|
87 |
|
88 // GetListAccounts returns the accounts belonging to a given list |
|
89 func (mc *Client) GetListAccounts(listID int64, lopt *LimitParams) ([]Account, error) { |
|
90 endPoint := "lists/" + strconv.FormatInt(listID, 10) + "/accounts" |
|
91 return mc.getMultipleAccounts(endPoint, nil, lopt) |
|
92 } |
|
93 |
|
94 // AddListAccounts adds the accounts to a given list |
|
95 func (mc *Client) AddListAccounts(listID int64, accountIDs []int64) error { |
|
96 endPoint := "lists/" + strconv.FormatInt(listID, 10) + "/accounts" |
|
97 method := rest.Post |
|
98 params := make(apiCallParams) |
|
99 for i, id := range accountIDs { |
|
100 if id < 1 { |
|
101 return ErrInvalidID |
|
102 } |
|
103 qID := fmt.Sprintf("[%d]account_ids", i) |
|
104 params[qID] = strconv.FormatInt(id, 10) |
|
105 } |
|
106 return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil) |
|
107 } |
|
108 |
|
109 // RemoveListAccounts removes the accounts from the given list |
|
110 func (mc *Client) RemoveListAccounts(listID int64, accountIDs []int64) error { |
|
111 endPoint := "lists/" + strconv.FormatInt(listID, 10) + "/accounts" |
|
112 method := rest.Delete |
|
113 params := make(apiCallParams) |
|
114 for i, id := range accountIDs { |
|
115 if id < 1 { |
|
116 return ErrInvalidID |
|
117 } |
|
118 qID := fmt.Sprintf("[%d]account_ids", i) |
|
119 params[qID] = strconv.FormatInt(id, 10) |
|
120 } |
|
121 return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil) |
|
122 } |
|
123 |
|
124 func (mc *Client) setSingleList(method rest.Method, listID int64, params apiCallParams) (*List, error) { |
|
125 var endPoint string |
|
126 if listID > 0 { |
|
127 endPoint = "lists/" + strconv.FormatInt(listID, 10) |
|
128 } else { |
|
129 endPoint = "lists" |
|
130 } |
|
131 var list List |
|
132 if err := mc.apiCall("v1/"+endPoint, method, params, nil, nil, &list); err != nil { |
|
133 return nil, err |
|
134 } |
|
135 return &list, nil |
|
136 } |
|