forked from campaignmonitor/createsend-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLists.java
More file actions
executable file
·349 lines (318 loc) · 16.6 KB
/
Lists.java
File metadata and controls
executable file
·349 lines (318 loc) · 16.6 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* Copyright (c) 2011 Toby Brain
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.createsend;
import java.util.Date;
import javax.ws.rs.core.MultivaluedMap;
import com.createsend.models.PagedResult;
import com.createsend.models.lists.CustomField;
import com.createsend.models.lists.CustomFieldForCreate;
import com.createsend.models.lists.List;
import com.createsend.models.lists.Statistics;
import com.createsend.models.lists.UpdateFieldOptions;
import com.createsend.models.lists.Webhook;
import com.createsend.models.lists.WebhookTestFailureDetails;
import com.createsend.models.segments.Segment;
import com.createsend.models.subscribers.Subscriber;
import com.createsend.util.ErrorDeserialiser;
import com.createsend.util.JerseyClient;
import com.createsend.util.JerseyClientImpl;
import com.createsend.util.exceptions.CreateSendException;
import com.createsend.util.jersey.JsonProvider;
import com.sun.jersey.core.util.MultivaluedMapImpl;
/**
* Provides methods for accessing all <a href="http://www.campaignmonitor.com/api/lists/" target="_blank">
* List</a> resources in the Campaign Monitor API *
*/
public class Lists {
private String listID;
private JerseyClient client;
/**
* Constructor.
* Use this constructor for creating new lists.
*/
public Lists() {
this(null);
}
/**
* Constructor.
* Use this constructor when the list ID is already known
* @param listID The ID of the list to apply any calls to
*/
public Lists(String listID) {
this(listID, new JerseyClientImpl());
}
/**
* Constructor.
* @param listID The ID of the list to apply any calls to
* @param client The {@link com.createsend.util.JerseyClient} to use for API requests
*/
public Lists(String listID, JerseyClient client) {
setListID(listID);
this.client = client;
}
/**
* Sets the current list ID.
* @param listID The ID of the list to apply any calls to.
*/
public void setListID(String listID) {
this.listID = listID;
}
/**
* Gets the current list ID.
* @return The current list ID.
*/
public String getListID() {
return listID;
}
/**
* Creates a new empty subscriber list.
* After a successful call, the current list id property will be set the ID of the
* newly created list.
* @param clientID The ID of the client owning the new list
* @param list The details of the new list
* @return The ID of the newly created list
* @throws CreateSendException Thrown when the API responds with a HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#creating_a_list" target="_blank">
* Creating a list</a>
*/
public String create(String clientID, List list) throws CreateSendException {
listID = client.post(String.class, list, "lists", clientID + ".json");
return listID;
}
/**
* Updates the basic list details for an existing subscriber list
* @param list The new basic details for the list
* @throws CreateSendException Thrown when the API responds with a HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#updating_a_list" target="_blank">
* Updating a list</a>
*/
public void update(List list) throws CreateSendException {
client.put(list, "lists", listID + ".json");
}
/**
* Deletes the list with the specified ID
* @throws CreateSendException Thrown when the API responds with a HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#deleting_a_list" target="_blank">
* Deleting a list</a>
*/
public void delete() throws CreateSendException {
client.delete("lists", listID + ".json");
}
/**
* Gets the details of the list with the given ID
* @return The details of the list with the given ID
* @throws CreateSendException Raised when the API returns a HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#getting_list_details" target="_blank">
* Getting list details</a>
*/
public List details() throws CreateSendException {
return client.get(List.class, "lists", listID + ".json");
}
/**
* Gets subscriber statistics for the list with the specified ID
* @return Subscriber statistics for the list with the specified ID
* @throws CreateSendException Raised when the API responds with HTTP status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#getting_list_stats" target="_blank">
* Getting list stats</a>
*/
public Statistics stats() throws CreateSendException {
return client.get(Statistics.class, "lists", listID, "stats.json");
}
/**
* Gets the custom fields available for the list with the specified ID
* @return The custom fields available for the specified list
* @throws CreateSendException Raised when the API responds with HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#getting_list_custom_fields" target="_blank">
* Getting list custom fields</a>
*/
public CustomField[] customFields() throws CreateSendException {
return client.get(CustomField[].class, "lists", listID, "customfields.json");
}
/**
* Gets the segments available in the list with the specified ID
* @return The segments available in the specified list
* @throws CreateSendException Raised when the API responds with HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#getting_list_segments" target="_blank">
* Getting list segments</a>
*/
public Segment[] segments() throws CreateSendException {
return client.get(Segment[].class, "lists", listID, "segments.json");
}
/**
* Gets a paged collection of active subscribers who have subscribed to the list
* since the provided date.
* @param subscribedFrom The API will only return subscribers who became active on or after this date.
* This field is required
* @param page The page number or results to get. Use <code>null</code> for the default (page=1)
* @param pageSize The number of records to get on the current page. Use <code>null</code> for the default.
* @param orderField The field used to order the results by. Use <code>null</code> for the default.
* @param orderDirection The direction to order the results by. Use <code>null</code> for the default.
* @return The paged subscribers returned by the api call.
* @throws CreateSendException Thrown when the API responds with a HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#getting_active_subscribers" target="_blank">
* Getting active subscribers</a>
*/
public PagedResult<Subscriber> active(Date subscribedFrom,
Integer page, Integer pageSize, String orderField, String orderDirection) throws CreateSendException {
MultivaluedMap<String, String> queryString = new MultivaluedMapImpl();
queryString.add("date", JsonProvider.ApiDateFormat.format(subscribedFrom));
return client.getPagedResult(page, pageSize, orderField, orderDirection,
queryString, "lists", listID, "active.json");
}
/**
* Gets a paged collection of unsubscribed subscribers who have unsubscribed from the list
* since the provided date.
* @param subscribedFrom The API will only return subscribers who unsubscribed on or after this date.
* This field is required
* @param page The page number or results to get. Use <code>null</code> for the default (page=1)
* @param pageSize The number of records to get on the current page. Use <code>null</code> for the default.
* @param orderField The field used to order the results by. Use <code>null</code> for the default.
* @param orderDirection The direction to order the results by. Use <code>null</code> for the default.
* @return The paged subscribers returned by the api call.
* @throws CreateSendException Thrown when the API responds with a HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#getting_unsubscribed_subscribers" target="_blank">
* Getting unsubscribed subscribers</a>
*/
public PagedResult<Subscriber> unsubscribed(Date subscribedFrom,
Integer page, Integer pageSize, String orderField, String orderDirection) throws CreateSendException {
MultivaluedMap<String, String> queryString = new MultivaluedMapImpl();
queryString.add("date", JsonProvider.ApiDateFormat.format(subscribedFrom));
return client.getPagedResult(page, pageSize, orderField, orderDirection,
queryString, "lists", listID, "unsubscribed.json");
}
/**
* Gets a paged collection of bounced subscribers who have bounced out of the list
* since the provided date.
* @param subscribedFrom The API will only return subscribers who bounced out on or after this date.
* This field is required
* @param page The page number or results to get. Use <code>null</code> for the default (page=1)
* @param pageSize The number of records to get on the current page. Use <code>null</code> for the default.
* @param orderField The field used to order the results by. Use <code>null</code> for the default.
* @param orderDirection The direction to order the results by. Use <code>null</code> for the default.
* @return The paged subscribers returned by the api call.
* @throws CreateSendException Thrown when the API responds with a HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#getting_bounced_subscribers" target="_blank">
* Getting bounced subscribers</a>
*/
public PagedResult<Subscriber> bounced(Date subscribedFrom,
Integer page, Integer pageSize, String orderField, String orderDirection) throws CreateSendException {
MultivaluedMap<String, String> queryString = new MultivaluedMapImpl();
queryString.add("date", JsonProvider.ApiDateFormat.format(subscribedFrom));
return client.getPagedResult(page, pageSize, orderField, orderDirection,
queryString, "lists", listID, "bounced.json");
}
/**
* Creates a new custom field with the specified data
* @param customField The custom field options
* @return The Key of the newly created custom field
* @throws CreateSendException Thrown when the API responds with HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#creating_a_custom_field" target="_blank">
* Creating a custom field</a>
*/
public String createCustomField(CustomFieldForCreate customField) throws CreateSendException {
return client.post(String.class, customField, "lists", listID, "customfields.json");
}
/**
* Updates the available options for a Multi-Valued custom field.
* @param fieldKey The <em>Key</em> of the custom field to update. This must be surrounded by [].
* @param options The new options to use for the field.
* @throws CreateSendException Raised when the API responds with HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#updating_custom_field_options" target="_blank">
* Updating custom field options</a>
*/
public void updateCustomFieldOptions(String fieldKey, UpdateFieldOptions options)
throws CreateSendException {
client.put(options, "lists", listID, "customFields", fieldKey, "options.json");
}
/**
* Deletes the custom field with the specified key
* @param fieldKey The <em>Key</em> of the custom field to delete. This must be surrounded by [].
* @throws CreateSendException Thrown when the API responds with HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#deleting_a_custom_field" target="_blank">
* Deleting a custom field</a>
*/
public void deleteCustomField(String fieldKey) throws CreateSendException {
client.delete("lists", listID, "customFields", fieldKey + ".json");
}
/**
* Gets all webhooks which have been attached to events on the specified list
* @return The webhooks which have been attached to events for the specified list
* @throws CreateSendException Thrown when the API responds with HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#getting_list_webhooks" target="_blank">
* Getting list webhooks</a>
*/
public Webhook[] webhooks() throws CreateSendException {
return client.get(Webhook[].class, "lists", listID, "webhooks.json");
}
/**
* Creates a new webhook on the specified list.
* @param webhook The webhook details
* @return The ID of the newly created webhook
* @throws CreateSendException Thrown when the API responds with HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#creating_a_webhook" target="_blank">
* Creating a webhook</a>
*/
public String createWebhook(Webhook webhook) throws CreateSendException {
return client.post(String.class, webhook, "lists", listID, "webhooks.json");
}
/**
* Tests the specified webhook
* @param webhookID The ID of the webhook
* @throws CreateSendException Thrown when the API responds with HTTP Status >= 400. I.e the test fails
* @see <a href="http://www.campaignmonitor.com/api/lists/#testing_a_webhook" target="_blank">
* Testing a webhook</a>
*/
public void testWebhook(String webhookID) throws CreateSendException {
client.get(String.class, new ErrorDeserialiser<WebhookTestFailureDetails>(),
"lists", listID, "webhooks", webhookID, "test.json");
}
/**
* Deletes the specified webhook
* @param webhookID The ID of the webhook to delete
* @throws CreateSendException Raised when the API responds with HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#deleting_a_webhook" target="_blank">
* Deleting a webhook</a>
*/
public void deleteWebhook(String webhookID) throws CreateSendException {
client.delete("lists", listID, "webhooks", webhookID + ".json");
}
/**
* Activates the specified webhook.
* @param webhookID The ID of the webhook to activate
* @throws CreateSendException Thrown when the API responds with HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#activating_a_webhook" target="_blank">
* Activating a webhook</a>
*/
public void activateWebhook(String webhookID) throws CreateSendException {
client.put("", "lists", listID, "webhooks", webhookID, "activate.json");
}
/**
* Deactivates the specified webhook.
* @param webhookID The ID of the webhook to deactivate
* @throws CreateSendException Thrown when the API responds with HTTP Status >= 400
* @see <a href="http://www.campaignmonitor.com/api/lists/#deactivating_a_webhook" target="_blank">
* Activating a webhook</a>
*/
public void deactivateWebhook(String webhookID) throws CreateSendException {
client.put("", "lists", listID, "webhooks", webhookID, "activate.json");
}
}