Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 97 additions & 85 deletions README.md

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/main/java/com/mailersend/sdk/MailerSendApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ public <T extends MailerSendResponse> T getRequest(String endpoint, Class<T> res
}

if (responseClass == MailerSendStringResponse.class) {
MailerSendStringResponse response = new MailerSendStringResponse();
response.responseString = responseObject.body().toString();
return (T) response;

T response = this.handleApiResponse(responseObject, responseClass);
((MailerSendStringResponse) response).responseString = responseObject.body().toString();

return response;
}


Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/mailersend/sdk/activities/Activities.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendApi;
Expand Down Expand Up @@ -64,15 +65,18 @@ public Activity getSingleActivity(String activityId) throws MailerSendException


/**
* Gets the activities for the given domain id
* Gets the first page of the activities of the last 7 days for the given domain id
*
* @param domainId a {@link java.lang.String} object.
* @throws com.mailersend.sdk.exceptions.MailerSendException
* @return a {@link com.mailersend.sdk.activities.ActivitiesList} object.
*/
public ActivitiesList getActivities(String domainId) throws MailerSendException {

return this.getActivities(domainId, 1, 25, null, null, null);
Date dateTo = new Date();
Date dateFrom = new Date(dateTo.getTime() - TimeUnit.DAYS.toMillis(7));

return this.getActivities(domainId, 1, 25, dateFrom, dateTo, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ public Email toEmail() {
email.html = html;
email.text = text;

Collections.addAll(email.tags, tags);
if (tags != null) {

Collections.addAll(email.tags, tags);
}

email.AddRecipient(recipient.toRecipient());

Expand Down
21 changes: 19 additions & 2 deletions src/main/java/com/mailersend/sdk/analytics/Analytics.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
**************************************************/
package com.mailersend.sdk.analytics;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
Expand Down Expand Up @@ -149,7 +151,11 @@ public AnalyticsByDateList getByDate(String groupBy, String[] events) throws Mai

ArrayList<String> params = new ArrayList<String>();

params.add(groupBy);
if (groupBy != null && !groupBy.isBlank()) {

// keeps accepting the raw "group_by=..." fragment that callers had to pass before
params.add(groupBy.startsWith("group_by=") ? groupBy : "group_by=".concat(urlEncode(groupBy)));
}

if (events != null && events.length > 0) {

Expand Down Expand Up @@ -300,10 +306,21 @@ private String arrayToUrlRequest(String[] values, String field) {
params = params.concat("&");
}

params = params.concat(field).concat("[]=").concat(values[i]);
params = params.concat(field).concat("[]=").concat(urlEncode(values[i]));
}

return params;
}


/**
* Url encodes a query parameter value
* @param value
* @return
*/
private String urlEncode(String value) {

return URLEncoder.encode(value, StandardCharsets.UTF_8);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public class AnalyticsByDate {
@SerializedName("survey_submitted")
public int surveySubmitted;

@SerializedName("suppressed")
public int suppressed;


/**
* <p>postDeserialize.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.time.temporal.TemporalAccessor;
import java.util.Date;

import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.mailersend.sdk.MailerSendResponse;

Expand All @@ -38,8 +39,17 @@ public class BlocklistMonitor extends MailerSendResponse {
@SerializedName("notify")
public boolean notify;

/**
* True when the address is listed on at least one blocklist. Derived from {@link #blocklistedCount}.
*/
public transient boolean blocklisted;

/**
* The number of blocklists the address is listed on.
*/
@SerializedName("blocklisted")
public boolean blocklisted;
@JsonAdapter(BlocklistedCountAdapter.class)
public Integer blocklistedCount;

@SerializedName("last_checks_count")
public int lastChecksCount;
Expand Down Expand Up @@ -88,6 +98,14 @@ public class BlocklistMonitor extends MailerSendResponse {
*/
public void postDeserialize() {
parseDates();

blocklisted = blocklistedCount != null && blocklistedCount > 0;

if (children != null) {
for (BlocklistMonitor child : children) {
child.postDeserialize();
}
}
}

private void parseDates() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public BlocklistMonitor createMonitor(String address) throws MailerSendException

BlocklistMonitorResponse response = api.postRequest(endpoint, json, BlocklistMonitorResponse.class);

response.monitor.postDeserialize();

return response.monitor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public BlocklistMonitor updateMonitor(String monitorId) throws MailerSendExcepti

BlocklistMonitorResponse response = api.putRequest(endpoint, json, BlocklistMonitorResponse.class);

response.monitor.postDeserialize();

return response.monitor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ public BlocklistMonitorsList getMonitors() throws MailerSendException {
sortByFilter = null;
orderFilter = null;

return api.getRequest(endpoint, BlocklistMonitorsList.class);
BlocklistMonitorsList response = api.getRequest(endpoint, BlocklistMonitorsList.class);

response.postDeserialize();

return response;
}

/**
Expand All @@ -154,6 +158,8 @@ public BlocklistMonitor getMonitor(String monitorId) throws MailerSendException

BlocklistMonitorResponse response = api.getRequest(endpoint, BlocklistMonitorResponse.class);

response.monitor.postDeserialize();

return response.monitor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,15 @@ public class BlocklistMonitorsList extends PaginatedResponse {

@SerializedName("data")
public BlocklistMonitor[] monitors;

/**
* Called after deserialization to post-process each monitor.
*/
public void postDeserialize() {
if (monitors != null) {
for (BlocklistMonitor monitor : monitors) {
monitor.postDeserialize();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*************************************************
* MailerSend Java SDK
* https://github.com/mailersend/mailersend-java
*
* @author MailerSend <support@mailersend.com>
* https://mailersend.com
**************************************************/
package com.mailersend.sdk.blocklistmonitoring;

import java.io.IOException;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

/**
* Reads the blocklisted count, which the create response returns as a boolean.
*/
class BlocklistedCountAdapter extends TypeAdapter<Integer> {

@Override
public void write(JsonWriter out, Integer value) throws IOException {
out.value(value);
}

@Override
public Integer read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.BOOLEAN) {
return in.nextBoolean() ? 1 : 0;
}

return in.nextInt();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
**************************************************/
package com.mailersend.sdk.dmarcmonitoring;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -336,15 +338,15 @@ private String prepareListParamsUrl() {
params.add("limit=".concat(String.valueOf(limitFilter)));

if (queryFilter != null) {
params.add("query=".concat(queryFilter));
params.add("query=".concat(urlEncode(queryFilter)));
}

if (sortByFilter != null) {
params.add("sort_by=".concat(sortByFilter));
params.add("sort_by=".concat(urlEncode(sortByFilter)));
}

if (orderFilter != null) {
params.add("order=".concat(orderFilter));
params.add("order=".concat(urlEncode(orderFilter)));
}

return buildQueryString(params);
Expand All @@ -361,23 +363,23 @@ private String prepareReportParamsUrl() {
params.add("limit=".concat(String.valueOf(limitFilter)));

if (dateFromFilter != null) {
params.add("date_from=".concat(dateFromFilter));
params.add("date_from=".concat(urlEncode(dateFromFilter)));
}

if (dateToFilter != null) {
params.add("date_to=".concat(dateToFilter));
params.add("date_to=".concat(urlEncode(dateToFilter)));
}

if (searchFilter != null) {
params.add("search=".concat(searchFilter));
params.add("search=".concat(urlEncode(searchFilter)));
}

if (categoryFilter != null) {
params.add("category=".concat(categoryFilter));
params.add("category=".concat(urlEncode(categoryFilter)));
}

if (reportSourceFilter != null) {
params.add("report_source=".concat(reportSourceFilter));
params.add("report_source=".concat(urlEncode(reportSourceFilter)));
}

return buildQueryString(params);
Expand All @@ -390,11 +392,11 @@ private String prepareReportParamsUrl() {
private String prepareReportSourcesParamsUrl() {
ArrayList<String> params = new ArrayList<String>();

params.add("date_from=".concat(dateFromFilter));
params.add("date_to=".concat(dateToFilter));
params.add("date_from=".concat(urlEncode(dateFromFilter)));
params.add("date_to=".concat(urlEncode(dateToFilter)));

if (statusFilter != null) {
params.add("status=".concat(statusFilter));
params.add("status=".concat(urlEncode(statusFilter)));
}

return buildQueryString(params);
Expand All @@ -411,4 +413,11 @@ private String buildQueryString(ArrayList<String> params) {
}
return requestParams;
}

/**
* Url encodes a query parameter value
*/
private String urlEncode(String value) {
return URLEncoder.encode(value, StandardCharsets.UTF_8);
}
}
22 changes: 3 additions & 19 deletions src/main/java/com/mailersend/sdk/domains/Domain.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
**************************************************/
package com.mailersend.sdk.domains;

import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

import com.google.gson.annotations.SerializedName;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.util.DateParser;

/**
* <p>Domain class.</p>
Expand Down Expand Up @@ -92,21 +90,7 @@ public void postDeserialize() {
*/
private void parseDates() {

TemporalAccessor ta;
Instant instant;

if (createdAtStr != null && !createdAtStr.isBlank()) {

ta = DateTimeFormatter.ISO_INSTANT.parse(createdAtStr);
instant = Instant.from(ta);
createdAt = Date.from(instant);
}

if (updatedAtStr != null && !updatedAtStr.isBlank()) {

ta = DateTimeFormatter.ISO_INSTANT.parse(updatedAtStr);
instant = Instant.from(ta);
updatedAt = Date.from(instant);
}
createdAt = DateParser.parse(createdAtStr);
updatedAt = DateParser.parse(updatedAtStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public Domain addDomain(String name) throws MailerSendException {

SingleDomainResponse response = api.postRequest(endpoint, json, SingleDomainResponse.class);

if (response.domain != null) {

response.domain.postDeserialize();
}

return response.domain;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class DomainDnsRecords {
* @deprecated Use {@link #dkimMs1} and {@link #dkimMs2} instead.
*/
@SerializedName("dkim")
@Deprecated
public DomainDnsAttribute dkim;

@SerializedName("dkim_ms1")
Expand Down
Loading