From a08f27d38f7c68d720da9266b83ba05489d188a9 Mon Sep 17 00:00:00 2001 From: Yorick Holkamp Date: Tue, 3 Jan 2023 14:13:32 +0100 Subject: [PATCH] feat: add send_each_at header parameter --- examples/Example.java | 3 +++ src/main/java/com/sendgrid/smtpapi/SMTPAPI.java | 17 +++++++++++++++++ .../java/com/sendgrid/smtpapi/SMTPAPITest.java | 9 +++++++++ 3 files changed, 29 insertions(+) diff --git a/examples/Example.java b/examples/Example.java index e5556d2..676d561 100644 --- a/examples/Example.java +++ b/examples/Example.java @@ -68,6 +68,9 @@ public static void main(String[] args) { // [Scheduling](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) header.setSendAt(1416427645); + // [Per email scheduling](https://docs.sendgrid.com/for-developers/sending-email/scheduling-parameters) + header.addSendEachAt(1416427645); + //int sendAt = header.getSendAt(); // Get Headers diff --git a/src/main/java/com/sendgrid/smtpapi/SMTPAPI.java b/src/main/java/com/sendgrid/smtpapi/SMTPAPI.java index 61cdd9f..e888053 100644 --- a/src/main/java/com/sendgrid/smtpapi/SMTPAPI.java +++ b/src/main/java/com/sendgrid/smtpapi/SMTPAPI.java @@ -2,6 +2,7 @@ import java.util.ArrayList; +import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONException; @@ -203,7 +204,23 @@ public SMTPAPI setSendAt(int sendAt) throws JSONException { public int getSendAt() throws JSONException { return this.header.getInt("send_at"); + } + + public SMTPAPI addSendEachAt(int sendAt) throws JSONException { + if (!this.header.has("send_each_at")) { + this.header.put("send_each_at", new JSONArray()); + } + this.header.accumulate("send_each_at", sendAt); + return this; + } + public List getSendEachAt() throws JSONException { + JSONArray array = this.header.getJSONArray("send_each_at"); + List sendTimes = new ArrayList<>(); + for (int i = 0; i < array.length(); i++) { + sendTimes.add(array.getInt(i)); + } + return sendTimes; } // convert from string to code point array diff --git a/src/test/java/com/sendgrid/smtpapi/SMTPAPITest.java b/src/test/java/com/sendgrid/smtpapi/SMTPAPITest.java index 5a7395f..20cbf63 100644 --- a/src/test/java/com/sendgrid/smtpapi/SMTPAPITest.java +++ b/src/test/java/com/sendgrid/smtpapi/SMTPAPITest.java @@ -12,6 +12,7 @@ import java.io.*; import java.util.Calendar; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -151,6 +152,14 @@ public class SMTPAPITest { Assert.assertEquals(expected, test.getSendAt()); } + @Test public void testSetSendEachAt() throws JSONException { + int expected = 12345; + test.addSendEachAt(expected); + List output = test.getSendEachAt(); + Assert.assertEquals(1, output.size()); + Assert.assertEquals(expected, test.getSendEachAt().get(0), 0); + } + @Test public void testCopyrightDateRange() throws JSONException { int expectedYear = Calendar.getInstance().get(Calendar.YEAR); String copyRightLine = getCopyrightDateRangeLine("LICENSE");