-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.go
130 lines (102 loc) · 3.97 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
package contacts
import (
"fmt"
"net/http"
)
// A List is a group of Recipients
type List struct {
ID uint `json:"id,omitempty"`
Name string `json:"name"`
RecipientCount int `json:"recipient_count,omitempty"`
}
// ListsClient provides methods for interacting with Lists.
type ListsClient struct {
client *Client
}
// Create a List
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Create-a-List-POST
func (c *ListsClient) Create(name string) (*List, error) {
list := &List{Name: name}
err := c.client.makeRequest(http.MethodPost, "/contactdb/lists", list, &list)
if err != nil {
return nil, err
}
return list, nil
}
type listListsResponse struct {
Lists []*List `json:"lists"`
}
// List all Lists
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#List-All-Lists-GET
func (c *ListsClient) List() ([]*List, error) {
var resp *listListsResponse
err := c.client.makeRequest(http.MethodGet, "/contactdb/lists", nil, &resp)
if err != nil {
return nil, err
}
return resp.Lists, nil
}
// Delete multiple Lists
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Delete-Multiple-lists-DELETE
func (c *ListsClient) Delete(listIDs ...uint) error {
return c.client.makeRequest(http.MethodDelete, "/contactdb/lists", listIDs, nil)
}
// Get (Retrieve) a List
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Retrieve-a-List-GET
func (c *ListsClient) Get(listID uint) (*List, error) {
var list *List
err := c.client.makeRequest(http.MethodGet, fmt.Sprintf("/contactdb/lists/%d", listID), nil, &list)
if err != nil {
return nil, err
}
return list, nil
}
// Update a List
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Update-a-List-PATCH
func (c *ListsClient) Update(list *List) error {
return c.client.makeRequest(http.MethodPatch, fmt.Sprintf("/contactdb/lists/%d", list.ID), list, nil)
}
// ListRecipients on a given List
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#List-Recipients-on-a-List-GET
func (c *ListsClient) ListRecipients(listID, pageSize, pageNum uint) ([]*Recipient, error) {
var resp *listRecipientsResponse
err := c.client.makeRequest(http.MethodGet, fmt.Sprintf("/contactdb/lists/%d/recipients?page_size=%d&page=%d", listID, pageSize, pageNum), nil, &resp)
if err != nil {
return nil, err
}
return resp.Recipients, nil
}
// AddRecipients to a List
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Add-Multiple-Recipients-to-a-List-POST
func (c *ListsClient) AddRecipients(listID uint, recipients ...*Recipient) error {
var recipientIDs []string
for _, recipient := range recipients {
recipientIDs = append(recipientIDs, recipient.ID)
}
return c.AddRecipientsByIDs(listID, recipientIDs...)
}
// AddRecipientsByIDs to a List
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Add-Multiple-Recipients-to-a-List-POST
func (c *ListsClient) AddRecipientsByIDs(listID uint, recipientIDs ...string) error {
return c.client.makeRequest(http.MethodPost, fmt.Sprintf("/contactdb/lists/%d/recipients", listID), recipientIDs, nil)
}
// DeleteRecipient from a List
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Delete-a-Single-Recipient-from-a-Single-List-DELETE
func (c *ListsClient) DeleteRecipient(listID uint, recipient *Recipient) error {
return c.DeleteRecipientByID(listID, recipient.ID)
}
// DeleteRecipientByID from a List
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Delete-a-Single-Recipient-from-a-Single-List-DELETE
func (c *ListsClient) DeleteRecipientByID(listID uint, recipientID string) error {
return c.client.makeRequest(http.MethodDelete, fmt.Sprintf("/contactdb/lists/%d/recipients/%s", listID, recipientID), nil, nil)
}