Skip to content

Commit 379140f

Browse files
author
Chris Wilson
committed
Adding support for Message Event API
1 parent c582964 commit 379140f

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
package com.sparkpost.samples;
3+
4+
import java.io.IOException;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Set;
8+
9+
import org.apache.log4j.Level;
10+
import org.apache.log4j.Logger;
11+
12+
import com.sparkpost.Client;
13+
import com.sparkpost.exception.SparkPostErrorServerResponseException;
14+
import com.sparkpost.exception.SparkPostException;
15+
import com.sparkpost.model.responses.MessageEventsResponse;
16+
import com.sparkpost.resources.ResourceMessageEvents;
17+
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
18+
import com.sparkpost.transport.IRestConnection;
19+
import com.sparkpost.transport.RestConnection;
20+
21+
/**
22+
* Note: This code has a lot of redundancy so it is easy to copy and paste into your own code base as a starting point.
23+
* Also, This code does not do any of the error checking that should be done in production code.
24+
*/
25+
public class MessageEventSearchSample extends SparkPostBaseApp {
26+
27+
static final Logger logger = Logger.getLogger(CreateTemplateSimple.class);
28+
29+
public static void main(String[] args) throws SparkPostException, IOException, InterruptedException {
30+
Logger.getRootLogger().setLevel(Level.DEBUG);
31+
32+
MessageEventSearchSample app = new MessageEventSearchSample();
33+
app.doSearch();
34+
}
35+
36+
private void doSearch() throws SparkPostException, IOException, InterruptedException {
37+
boolean dumpResult = false;
38+
Client client = this.newConfiguredClient();
39+
IRestConnection connection = new RestConnection(client, getEndPoint());
40+
41+
MessageEventsResponse response = null;
42+
do {
43+
if (response == null) {
44+
response = ResourceMessageEvents.searchMessageEvents(connection, 10);
45+
} else {
46+
try {
47+
response = ResourceMessageEvents.nextMessageEvents(connection, response);
48+
} catch (SparkPostErrorServerResponseException e) {
49+
if (e.getResponseCode() == 429) {
50+
// We hit rate limit. Retry request again after a delay
51+
System.out.println("Hit the rate limit!!! Will retry after delay...");
52+
Thread.sleep(30000);
53+
continue;
54+
}
55+
throw e;
56+
}
57+
}
58+
59+
if (response == null) {
60+
return;
61+
} else if (response.getResponseCode() != 200) {
62+
System.err.println(response);
63+
return;
64+
}
65+
66+
if (dumpResult) {
67+
System.out.println("Response: " + response);
68+
69+
List<Map<String, Object>> results = response.getResults();
70+
for (Map<String, Object> result : results) {
71+
System.out.println("\nResult (" + result.get("type") + ")");
72+
Set<String> keySet = result.keySet();
73+
for (String key : keySet) {
74+
// Print out result
75+
System.out.println("\t" + key + " = " + result.get(key));
76+
}
77+
}
78+
}
79+
System.out.println("Total Count: " + response.getTotalCount());
80+
System.out.println("Links: " + response.getLinks());
81+
System.out.println("Has Next Page: " + response.hasNext());
82+
83+
} while (response != null && response.hasNext());
84+
}
85+
86+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
package com.sparkpost.model.responses;
3+
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
import com.google.gson.annotations.SerializedName;
8+
import com.yepher.jsondoc.annotations.Description;
9+
10+
import lombok.Data;
11+
import lombok.EqualsAndHashCode;
12+
13+
@Data
14+
@EqualsAndHashCode(callSuper = true)
15+
public class MessageEventsResponse extends Response {
16+
17+
// {"results":[],"total_count":0,"links":[]}
18+
@Description(value = "results", sample = {"\"results\": [ {\"count_injected\": 633,...}]"})
19+
@SerializedName("results")
20+
private List<Map<String, Object>> results;
21+
22+
@Description(value = "links", sample = {"{ \"href\": \"/api/v1/message-events\", \"rel\": \"message-events\", \"method\": \"GET\" }"})
23+
@SerializedName("links")
24+
private List<Map<String, String>> links;
25+
26+
@Description(value = "total_count", sample = {"{ \"total_count\": 0 }"})
27+
@SerializedName("total_count")
28+
private int totalCount;
29+
30+
public boolean hasNext() {
31+
String next = nextPageUrl();
32+
return next != null && nextPageUrl().length() > 0;
33+
}
34+
35+
public String nextPageUrl() {
36+
if (this.links.size() == 0) {
37+
return "";
38+
}
39+
40+
for (Map<String, String> element : this.links) {
41+
String value = element.get("rel");
42+
if ("next".equalsIgnoreCase(value)) {
43+
return element.get("href");
44+
}
45+
}
46+
47+
return "";
48+
}
49+
50+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
package com.sparkpost.resources;
3+
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
6+
7+
import com.sparkpost.exception.SparkPostException;
8+
import com.sparkpost.model.responses.MessageEventsResponse;
9+
import com.sparkpost.model.responses.Response;
10+
import com.sparkpost.transport.IRestConnection;
11+
12+
/**
13+
* Resource collection that is a 1-to-1 match to the Metrics SparkPost API.<br>
14+
* <br>
15+
* See <a href="https://developers.sparkpost.com/api/message-events.html/">Message Events
16+
* API</a>
17+
*/
18+
public class ResourceMessageEvents {
19+
20+
public static MessageEventsResponse searchMessageEvents(IRestConnection conn) throws SparkPostException {
21+
Endpoint ep = new Endpoint("message-events");
22+
Response response = conn.get(ep);
23+
24+
MessageEventsResponse messageResponse = MessageEventsResponse.decode(response, MessageEventsResponse.class);
25+
return messageResponse;
26+
}
27+
28+
public static MessageEventsResponse searchMessageEvents(IRestConnection conn, int perPage) throws SparkPostException {
29+
Endpoint ep = new Endpoint("message-events");
30+
ep.addParam("per_page", perPage);
31+
Response response = conn.get(ep);
32+
33+
MessageEventsResponse messageResponse = MessageEventsResponse.decode(response, MessageEventsResponse.class);
34+
return messageResponse;
35+
}
36+
37+
public static MessageEventsResponse nextMessageEvents(IRestConnection conn, MessageEventsResponse page) throws SparkPostException {
38+
if (page.hasNext() == false) {
39+
return null;
40+
}
41+
42+
String next = page.nextPageUrl();
43+
try {
44+
URI uri = new URI(next);
45+
46+
String path = uri.getPath();
47+
path = path.replaceFirst("/api/v1/", "");
48+
Endpoint ep = new Endpoint(path);
49+
50+
String query = uri.getQuery();
51+
if (query.length() > 0) {
52+
String[] split = query.split("&");
53+
for (String element : split) {
54+
String[] fields = element.split("=");
55+
ep.addParam(fields[0], fields[1]);
56+
}
57+
}
58+
59+
Response response = conn.get(ep);
60+
61+
MessageEventsResponse messageResponse = MessageEventsResponse.decode(response, MessageEventsResponse.class);
62+
return messageResponse;
63+
} catch (URISyntaxException e) {
64+
throw new SparkPostException(e);
65+
}
66+
}
67+
68+
}

0 commit comments

Comments
 (0)