Skip to content

Commit 240a5e9

Browse files
committed
Merge pull request #20 from DominicGunn/sparkpost_clean_up
Sparkpost clean up
2 parents c44d838 + d847d0c commit 240a5e9

File tree

8 files changed

+72
-135
lines changed

8 files changed

+72
-135
lines changed

libs/sparkpost-lib/src/main/java/com/sparkpost/Client.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
package com.sparkpost;
33

4+
import com.sparkpost.model.AddressAttributes;
5+
46
import java.util.List;
57
import java.util.Map;
68

7-
import com.sparkpost.model.AddressAttributes;
8-
99
/**
1010
* The Client class stores everything specific to the SparkPost client:<BR>
1111
* <ul>
@@ -77,7 +77,7 @@ public void setFromEmail(String fromEmail) {
7777
this.fromEmail = fromEmail;
7878
}
7979

80-
public void sendMessage(String termplateId, List<AddressAttributes> recipients, Map<String, String> args) {
80+
public void sendMessage(String templateId, List<AddressAttributes> recipients, Map<String, String> args) {
8181

8282
}
8383

libs/sparkpost-lib/src/main/java/com/sparkpost/model/Base.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
*/
1111
public class Base {
1212

13+
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
14+
private static final GsonBuilder GSON_BUILDER = new GsonBuilder().setDateFormat(DATE_TIME_FORMAT);
15+
1316
/**
1417
* Generate JSON for this request
1518
*
@@ -25,12 +28,11 @@ public String toJson() {
2528
* @return json of object
2629
*/
2730
public String toJson(boolean prettyPrint) {
28-
GsonBuilder gsonBuilder = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss");
2931
Gson gson;
3032
if (prettyPrint) {
31-
gson = gsonBuilder.setPrettyPrinting().create();
33+
gson = GSON_BUILDER.setPrettyPrinting().create();
3234
} else {
33-
gson = gsonBuilder.create();
35+
gson = GSON_BUILDER.create();
3436
}
3537

3638
return gson.toJson(this);

libs/sparkpost-lib/src/main/java/com/sparkpost/model/TemplateSubstitutionData.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11

22
package com.sparkpost.model;
33

4-
import java.util.HashMap;
5-
import java.util.Map;
6-
74
import com.google.gson.annotations.SerializedName;
85
import com.yepher.jsondoc.annotations.Description;
9-
106
import lombok.Data;
117
import lombok.EqualsAndHashCode;
128

9+
import java.util.HashMap;
10+
import java.util.Map;
11+
1312
/**
1413
* DTO for storing substitution data (list of key=value).
1514
*/
1615
@Data
1716
@EqualsAndHashCode(callSuper = true)
1817
public class TemplateSubstitutionData extends Base {
1918

20-
@Description(value = "Data the will be substituted into the template", sample = {"Dictionary of ssubstitution data"})
19+
@Description(value = "Data the will be substituted into the template", sample = {"Dictionary of substitution data"})
2120
@SerializedName("substitution_data")
2221
private Map<String, String> substitutionData = new HashMap<String, String>();
2322

libs/sparkpost-lib/src/main/java/com/sparkpost/model/responses/Response.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
@EqualsAndHashCode(callSuper = true)
1919
public class Response extends Base {
2020

21+
private static final Gson GSON = new Gson();
22+
2123
@Description(value = "The URI of the request", sample = {""})
2224
private String request = null;
2325

@@ -38,15 +40,13 @@ public class Response extends Base {
3840
private String responseBody = null;
3941

4042
public static <T extends Response> T decode(Response response, Type typeOfT) {
41-
Gson gson = new Gson();
42-
4343
T newResponse = null;
4444

4545
// Make sure this is a JSON response before we try and decode with GSON
4646
if (response.getContentType() != null && response.getContentType().toLowerCase().startsWith("application/json")) {
47-
newResponse = gson.fromJson(response.getResponseBody(), typeOfT);
47+
newResponse = GSON.fromJson(response.getResponseBody(), typeOfT);
4848
} else {
49-
newResponse = gson.fromJson("{}", typeOfT);
49+
newResponse = GSON.fromJson("{}", typeOfT);
5050
}
5151

5252
if (newResponse != null) {

libs/sparkpost-lib/src/main/java/com/sparkpost/resources/Endpoint.java

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ private void addString(String name, String value) {
2121
this.uriBuilder.addParameter(name, value);
2222
}
2323

24+
public Endpoint addCommonParams(String from, String to, String domains, String campaigns, String templates, String metrics,
25+
String timezone, String limit, String orderBy) {
26+
addParam("from", from);
27+
addParam("to", to);
28+
addParam("domains", domains);
29+
addParam("campaigns", campaigns);
30+
addParam("templates", templates);
31+
addParam("metrics", metrics);
32+
addParam("timezone", timezone);
33+
addParam("limit", limit);
34+
addParam("order_by", orderBy);
35+
return this;
36+
}
37+
2438
public Endpoint addParam(String name, String val) {
2539
if (val != null) {
2640
addString(name, val);

libs/sparkpost-lib/src/main/java/com/sparkpost/resources/ResourceMetrics.java

+31-111
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,7 @@ public static Response getDeliverabilityMetricsSummary(
3030
String timezone) throws SparkPostException {
3131

3232
Endpoint ep = new Endpoint("metrics/deliverability");
33-
ep.addParam("from", from);
34-
ep.addParam("to", to);
35-
ep.addParam("domains", domains);
36-
ep.addParam("campaigns", campaigns);
37-
ep.addParam("templates", templates);
38-
ep.addParam("metrics", metrics);
39-
ep.addParam("timezone", timezone);
33+
ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, null, null);
4034

4135
Response response = conn.get(ep.toString());
4236
return response;
@@ -52,18 +46,10 @@ public static Response getDeliverabilityMetricsByDomain(
5246
String metrics,
5347
String timezone,
5448
String limit,
55-
String order_by) throws SparkPostException {
49+
String orderBy) throws SparkPostException {
5650

5751
Endpoint ep = new Endpoint("metrics/deliverability/domain");
58-
ep.addParam("from", from);
59-
ep.addParam("to", to);
60-
ep.addParam("domains", domains);
61-
ep.addParam("campaigns", campaigns);
62-
ep.addParam("templates", templates);
63-
ep.addParam("metrics", metrics);
64-
ep.addParam("timezone", timezone);
65-
ep.addParam("limit", limit);
66-
ep.addParam("order_by", order_by);
52+
ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, limit, orderBy);
6753

6854
Response response = conn.get(ep.toString());
6955
return response;
@@ -80,18 +66,10 @@ public static Response getDeliverabilityMetricsByCampaign(
8066
String metrics,
8167
String timezone,
8268
String limit,
83-
String order_by) throws SparkPostException {
69+
String orderBy) throws SparkPostException {
8470

8571
Endpoint ep = new Endpoint("metrics/deliverability/campaign");
86-
ep.addParam("from", from);
87-
ep.addParam("to", to);
88-
ep.addParam("domains", domains);
89-
ep.addParam("campaigns", campaigns);
90-
ep.addParam("templates", templates);
91-
ep.addParam("metrics", metrics);
92-
ep.addParam("timezone", timezone);
93-
ep.addParam("limit", limit);
94-
ep.addParam("order_by", order_by);
72+
ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, limit, orderBy);
9573

9674
Response response = conn.get(ep.toString());
9775
return response;
@@ -107,18 +85,11 @@ public static Response getDeliverabilityMetricsByTemplate(
10785
String metrics,
10886
String timezone,
10987
String limit,
110-
String order_by) throws SparkPostException {
88+
String orderBy) throws SparkPostException {
11189

11290
Endpoint ep = new Endpoint("metrics/deliverability/template");
113-
ep.addParam("from", from);
114-
ep.addParam("to", to);
115-
ep.addParam("domains", domains);
116-
ep.addParam("campaigns", campaigns);
117-
ep.addParam("templates", templates);
118-
ep.addParam("metrics", metrics);
119-
ep.addParam("timezone", timezone);
120-
ep.addParam("limit", limit);
121-
ep.addParam("order_by", order_by);
91+
ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, limit, orderBy);
92+
12293

12394
Response response = conn.get(ep.toString());
12495
return response;
@@ -134,18 +105,11 @@ public static Response getDeliverabilityMetricsByWatchedDomain(
134105
String metrics,
135106
String timezone,
136107
String limit,
137-
String order_by) throws SparkPostException {
108+
String orderBy) throws SparkPostException {
138109

139110
Endpoint ep = new Endpoint("metrics/deliverability/watched-domain");
140-
ep.addParam("from", from);
141-
ep.addParam("to", to);
142-
ep.addParam("domains", domains);
143-
ep.addParam("campaigns", campaigns);
144-
ep.addParam("templates", templates);
145-
ep.addParam("metrics", metrics);
146-
ep.addParam("timezone", timezone);
147-
ep.addParam("limit", limit);
148-
ep.addParam("order_by", order_by);
111+
ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, limit, orderBy);
112+
149113

150114
Response response = conn.get(ep.toString());
151115
return response;
@@ -163,14 +127,9 @@ public static Response getTimeSeriesMetrics(
163127
String timezone) throws SparkPostException {
164128

165129
Endpoint ep = new Endpoint("metrics/deliverability/time-series");
166-
ep.addParam("from", from);
167-
ep.addParam("to", to);
168-
ep.addParam("domains", domains);
169-
ep.addParam("campaigns", campaigns);
170-
ep.addParam("templates", templates);
130+
171131
ep.addParam("precision", precision);
172-
ep.addParam("metrics", metrics);
173-
ep.addParam("timezone", timezone);
132+
ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, null, null);
174133

175134
Response response = conn.get(ep.toString());
176135
return response;
@@ -188,14 +147,9 @@ public static Response getBounceReasonMetrics(
188147
String limit) throws SparkPostException {
189148

190149
Endpoint ep = new Endpoint("metrics/deliverability/bounce-reason");
191-
ep.addParam("from", from);
192-
ep.addParam("to", to);
193-
ep.addParam("domains", domains);
194-
ep.addParam("campaigns", campaigns);
195-
ep.addParam("templates", templates);
196-
ep.addParam("metrics", metrics);
197-
ep.addParam("timezone", timezone);
150+
198151
ep.addParam("limit", limit);
152+
ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, null, null);
199153

200154
Response response = conn.get(ep.toString());
201155
return response;
@@ -213,14 +167,9 @@ public static Response getBounceReasonMetricsByDomain(
213167
String limit) throws SparkPostException {
214168

215169
Endpoint ep = new Endpoint("metrics/deliverability/bounce-reason/domain");
216-
ep.addParam("from", from);
217-
ep.addParam("to", to);
218-
ep.addParam("domains", domains);
219-
ep.addParam("campaigns", campaigns);
220-
ep.addParam("templates", templates);
221-
ep.addParam("metrics", metrics);
222-
ep.addParam("timezone", timezone);
170+
223171
ep.addParam("limit", limit);
172+
ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, null, null);
224173

225174
Response response = conn.get(ep.toString());
226175
return response;
@@ -238,14 +187,9 @@ public static Response getBounceClassificationMetrics(
238187
String limit) throws SparkPostException {
239188

240189
Endpoint ep = new Endpoint("metrics/deliverability/bounce-classification");
241-
ep.addParam("from", from);
242-
ep.addParam("to", to);
243-
ep.addParam("domains", domains);
244-
ep.addParam("campaigns", campaigns);
245-
ep.addParam("templates", templates);
246-
ep.addParam("metrics", metrics);
247-
ep.addParam("timezone", timezone);
190+
248191
ep.addParam("limit", limit);
192+
ep.addCommonParams(from, to, domains, campaigns, templates, metrics, timezone, null, null);
249193

250194
Response response = conn.get(ep.toString());
251195
return response;
@@ -262,13 +206,9 @@ public static Response getRejectionReasonMetrics(
262206
String limit) throws SparkPostException {
263207

264208
Endpoint ep = new Endpoint("metrics/deliverability/rejection-reason");
265-
ep.addParam("from", from);
266-
ep.addParam("to", to);
267-
ep.addParam("domains", domains);
268-
ep.addParam("campaigns", campaigns);
269-
ep.addParam("templates", templates);
270-
ep.addParam("timezone", timezone);
209+
271210
ep.addParam("limit", limit);
211+
ep.addCommonParams(from, to, domains, campaigns, templates, null, timezone, null, null);
272212

273213
Response response = conn.get(ep.toString());
274214
return response;
@@ -285,13 +225,9 @@ public static Response getRejectionReasonMetricsByDomain(
285225
String limit) throws SparkPostException {
286226

287227
Endpoint ep = new Endpoint("metrics/deliverability/rejection-reason/domain");
288-
ep.addParam("from", from);
289-
ep.addParam("to", to);
290-
ep.addParam("domains", domains);
291-
ep.addParam("campaigns", campaigns);
292-
ep.addParam("templates", templates);
293-
ep.addParam("timezone", timezone);
228+
294229
ep.addParam("limit", limit);
230+
ep.addCommonParams(from, to, domains, campaigns, templates, null, timezone, null, null);
295231

296232
Response response = conn.get(ep.toString());
297233
return response;
@@ -308,13 +244,9 @@ public static Response getDelayReasonMetrics(
308244
String limit) throws SparkPostException {
309245

310246
Endpoint ep = new Endpoint("metrics/deliverability/delay-reason");
311-
ep.addParam("from", from);
312-
ep.addParam("to", to);
313-
ep.addParam("domains", domains);
314-
ep.addParam("campaigns", campaigns);
315-
ep.addParam("templates", templates);
316-
ep.addParam("timezone", timezone);
247+
317248
ep.addParam("limit", limit);
249+
ep.addCommonParams(from, to, domains, campaigns, templates, null, timezone, null, null);
318250

319251
Response response = conn.get(ep.toString());
320252
return response;
@@ -331,13 +263,9 @@ public static Response getDelayReasonMetricsByDomain(
331263
String limit) throws SparkPostException {
332264

333265
Endpoint ep = new Endpoint("metrics/deliverability/delay-reason/domain");
334-
ep.addParam("from", from);
335-
ep.addParam("to", to);
336-
ep.addParam("domains", domains);
337-
ep.addParam("campaigns", campaigns);
338-
ep.addParam("templates", templates);
339-
ep.addParam("timezone", timezone);
266+
340267
ep.addParam("limit", limit);
268+
ep.addCommonParams(from, to, domains, campaigns, templates, null, timezone, null, null);
341269

342270
Response response = conn.get(ep.toString());
343271
return response;
@@ -354,13 +282,9 @@ public static Response getEngagementDetails(
354282
String limit) throws SparkPostException {
355283

356284
Endpoint ep = new Endpoint("metrics/deliverability/link-name");
357-
ep.addParam("from", from);
358-
ep.addParam("to", to);
359-
ep.addParam("timezone", timezone);
360-
ep.addParam("metrics", metrics);
361-
ep.addParam("campaigns", campaigns);
362-
ep.addParam("templates", templates);
285+
363286
ep.addParam("limit", limit);
287+
ep.addCommonParams(from, to, null, campaigns, templates, metrics, timezone, null, null);
364288

365289
Response response = conn.get(ep.toString());
366290
return response;
@@ -376,12 +300,8 @@ public static Response getDeliveriesByAttempt(
376300
String timezone) throws SparkPostException {
377301

378302
Endpoint ep = new Endpoint("metrics/deliverability/attempt");
379-
ep.addParam("from", from);
380-
ep.addParam("to", to);
381-
ep.addParam("domains", domains);
382-
ep.addParam("campaigns", campaigns);
383-
ep.addParam("templates", templates);
384-
ep.addParam("timezone", timezone);
303+
ep.addCommonParams(from, to, domains, campaigns, templates, null, timezone, null, null);
304+
385305
Response response = conn.get(ep.toString());
386306
return response;
387307
}

0 commit comments

Comments
 (0)