Permalink
Browse files

Implementing Bounce etc

  • Loading branch information...
k-shunji
k-shunji committed May 11, 2013
1 parent b2be4de commit f6c8f1d6cda92b62dd78c9fd58f9ff6e57988ada
@@ -60,78 +60,105 @@ private void checkResponse(String json) throws SendGridException {
if (map.get("error") != null || map.get("errors") != null) {
throw new SendGridException(map);
}
- if (!"success".equals(map.get("message"))) {
- throw new SendGridException(json);
+ String msg = (String)map.get("message");
+ if (!"success".equals(msg)) {
+ throw new SendGridException(msg == null ? json : msg);
}
}
//Blocks
- public List<Block> getBlocks(Block.Get request) {
- return null;
+ public List<Block> getBlocks(Block.Get request) throws IOException, SendGridException {
+ String json = doRequest("/blocks.get.json", request);
+ List<Map<String, Object>> list = JsonUtils.parseArray(json);
+ List<Block> ret = new ArrayList<Block>();
+ for (Map<String, Object> map : list) {
+ ret.add(new Block(map));
+ }
+ return ret;
}
- public void deleteBlocks(Block.Delete request) {
+ public void deleteBlocks(Block.Delete request) throws IOException, SendGridException {
+ String json = doRequest("/blocks.delete.json", request);
+ checkResponse(json);
}
//Bounce
- public List<Bounce> getBounces(Bounce.Get request) {
- return null;
+ public List<Bounce> getBounces(Bounce.Get request) throws IOException, SendGridException {
+ String json = doRequest("/bounces.get.json", request);
+ List<Map<String, Object>> list = JsonUtils.parseArray(json);
+ List<Bounce> ret = new ArrayList<Bounce>();
+ for (Map<String, Object> map : list) {
+ ret.add(new Bounce(map));
+ }
+ return ret;
+ }
+
+ public void deleteBounces(Bounce.Delete request) throws IOException, SendGridException {
+ String json = doRequest("/bounces.delete.json", request);
+ checkResponse(json);
}
- public void deleteBounces(Bounce.Delete request) {
+ public int countBounces() throws IOException, SendGridException {
+ return countBounces(new Bounce.Count());
}
- public int countBounces(Bounce.Count request) {
- return 0;
+ public int countBounces(Bounce.Count request) throws IOException, SendGridException {
+ String json = doRequest("/bounces.count.json", request);
+ Map<String, Object> map = JsonUtils.parse(json);
+ Double count = (Double)map.get("count");
+ if (count == null) {
+ throw new SendGridException(json);
+ }
+ return count.intValue();
}
//Filter commands
- public List<App> getAvailableApps() {
+ public List<App> getAvailableApps() throws IOException, SendGridException {
return null;
}
- public void activateApp(String name) {
+ public void activateApp(String name) throws IOException, SendGridException {
}
- public void deactivateApp(String name) {
+ public void deactivateApp(String name) throws IOException, SendGridException {
}
- public void setupApp(App app) {
+ public void setupApp(App app) throws IOException, SendGridException {
}
- public App getAppSettings(String name) {
+ public App getAppSettings(String name) throws IOException, SendGridException {
return null;
}
//Individual apps
- public List<String> getAddressWhilteList() {
+ public List<String> getAddressWhilteList() throws IOException, SendGridException {
return null;
}
- public void setAddressWhiteList(List<String> list) {
+ public void setAddressWhiteList(List<String> list) throws IOException, SendGridException {
}
- public String getBcc() {
+ public String getBcc() throws IOException, SendGridException {
return null;
}
- public void setBcc(String bcc) {
+ public void setBcc(String bcc) throws IOException, SendGridException {
}
- public boolean isEnableClickTrackingInPlainText() {
+ public boolean isEnableClickTrackingInPlainText() throws IOException, SendGridException {
return false;
}
- public void setEnableClickTrackingInPlainText(boolean b) {
+ public void setEnableClickTrackingInPlainText(boolean b) throws IOException, SendGridException {
}
- public void setDomainKeys(String domain, boolean sender) {
+ public void setDomainKeys(String domain, boolean sender) throws IOException, SendGridException {
}
- public void setDKIM(String domain, boolean useFrom) {
+ public void setDKIM(String domain, boolean useFrom) throws IOException, SendGridException {
}
- public void setEventNotification(String url, List<Event> enableEvents) {
+ public void setEventNotification(String url, List<Event> enableEvents) throws IOException, SendGridException {
}
//InvalidEmails
@@ -145,7 +172,7 @@ public void setEventNotification(String url, List<Event> enableEvents) {
return ret;
}
- public void deleteInvalidEmails(InvalidEmail.Delete request) {
+ public void deleteInvalidEmails(InvalidEmail.Delete request) throws IOException, SendGridException {
}
//Mail
@@ -171,28 +198,28 @@ public void mail(WebMail mail, File... attachements) throws IOException, SendGri
//Parse WebHook Settings - NOT IMPLEMENTED
//Profile
- public Profile getProfile() {
+ public Profile getProfile() throws IOException, SendGridException {
return null;
}
- public void setProfile(Profile profile) {
+ public void setProfile(Profile profile) throws IOException, SendGridException {
}
- public void setPassword(String newPassword) {
+ public void setPassword(String newPassword) throws IOException, SendGridException {
}
- public void setUsername(String newUsername) {
+ public void setUsername(String newUsername) throws IOException, SendGridException {
}
- public void setEmail(String newEmail) {
+ public void setEmail(String newEmail) throws IOException, SendGridException {
}
//SpamReports
- public List<SpamReport> getSpamReports(SpamReport.Get request) {
+ public List<SpamReport> getSpamReports(SpamReport.Get request) throws IOException, SendGridException {
return null;
}
- public void deleteSpamReports(SpamReport.Delete request) {
+ public void deleteSpamReports(SpamReport.Delete request) throws IOException, SendGridException {
}
//Statistics
@@ -219,13 +246,13 @@ public void deleteSpamReports(SpamReport.Delete request) {
}
//Unsubscribles
- public List<Unsubscribe> getUnsubscribes(Unsubscribe.Get request) {
+ public List<Unsubscribe> getUnsubscribes(Unsubscribe.Get request) throws IOException, SendGridException {
return null;
}
- public void deleteUnsubscribes(Unsubscribe.Delete request) {
+ public void deleteUnsubscribes(Unsubscribe.Delete request) throws IOException, SendGridException {
}
- public void addUnsubscribes(Unsubscribe.Add request) {
+ public void addUnsubscribes(Unsubscribe.Add request) throws IOException, SendGridException {
}
}
@@ -16,8 +16,18 @@ protected AbstractGetRequest(boolean includeDateParameter) {
public Date getStartDate() { return doGetDate("start_date");}
public Date getEndDate() { return doGetDate("end_date");}
public void setStartAndEndDate(Date start, Date end) {
- doSetDate("start_date", start);
- doSetDate("end_date", end);
+ if (start == null && end == null) {
+ throw new IllegalArgumentException();
+ }
+ if (start != null && end != null && start.getTime() > end.getTime()) {
+ throw new IllegalArgumentException("Start date must be before the end date");
+ }
+ if (start != null) {
+ doSetDate("start_date", start);
+ }
+ if (end != null) {
+ doSetDate("end_date", end);
+ }
}
}
@@ -1,12 +1,25 @@
package jp.co.flect.sendgrid.model;
-public class Block {
+import java.util.Date;
+import java.util.Map;
+
+public class Block extends AbstractModel {
- public static class Get {
-
+ public Block(Map<String, Object> map) {
+ super(map);
}
+ public String getStatus() { return doGetString("status");}
+ public String getReason() { return doGetString("reason");}
+ public Date getCreated() { return doGetDate("created", true);}
+ public String getEmail() { return doGetString("email");}
- public static class Delete {
+ public static class Get extends BasicGetRequest {
+ }
+
+ public static class Delete extends AbstractRequest {
+
+ public String getEmail() { return doGetString("email");}
+ public void setEmail(String s) { doSetString("email", s);}
}
}
@@ -1,15 +1,71 @@
package jp.co.flect.sendgrid.model;
-public class Bounce {
+import java.util.Date;
+import java.util.Map;
+
+public class Bounce extends AbstractModel {
+
+ public enum Type {
+ HARD,
+ SOFT
+ }
- public static class Get {
+ public Bounce(Map<String, Object> map) {
+ super(map);
+ }
+
+ public String getStatus() { return doGetString("status");}
+ public String getReason() { return doGetString("reason");}
+ public Date getCreated() { return doGetDate("created", true);}
+ public String getEmail() { return doGetString("email");}
+
+ public static class Get extends BasicGetRequest {
+
+ public Bounce.Type getType() {
+ String s = doGetString("type");
+ return s == null ? null : Type.valueOf(s.toUpperCase());
+ }
+ public void setType(Bounce.Type type) {
+ doSetString("type", type.toString().toLowerCase());
+ }
}
- public static class Delete {
+ public static class Count extends AbstractRequest {
+
+ public Date getStartDate() { return doGetDate("start_date");}
+ public Date getEndDate() { return doGetDate("end_date");}
+ public void setStartAndEndDate(Date start, Date end) {
+ if (start == null && end == null) {
+ throw new IllegalArgumentException();
+ }
+ if (start != null && end != null && start.getTime() > end.getTime()) {
+ throw new IllegalArgumentException("Start date must be before the end date");
+ }
+ if (start != null) {
+ doSetDate("start_date", start);
+ }
+ if (end != null) {
+ doSetDate("end_date", end);
+ }
+ }
+
+
+ public Bounce.Type getType() {
+ String s = doGetString("type");
+ return s == null ? null : Type.valueOf(s.toUpperCase());
+ }
+
+ public void setType(Bounce.Type type) {
+ doSetString("type", type.toString().toLowerCase());
+ }
+
}
- public static class Count {
+ public static class Delete extends Count {
+
+ public String getEmail() { return doGetString("email");}
+ public void setEmail(String s) { doSetString("email", s);}
}
}
@@ -0,0 +1,51 @@
+package jp.co.flect.sendgrid;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+import org.junit.Test;
+import org.junit.BeforeClass;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.Properties;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Date;
+import java.util.Map;
+import java.util.HashMap;
+
+import jp.co.flect.sendgrid.model.Block;
+
+import static jp.co.flect.sendgrid.SendGridTest.USERNAME;
+import static jp.co.flect.sendgrid.SendGridTest.PASSWORD;
+import static jp.co.flect.sendgrid.SendGridTest.MAIL_FROM;
+import static jp.co.flect.sendgrid.SendGridTest.MAIL_TO;
+
+public class BlockTest {
+
+ @Test
+ public void invalidMails() throws Exception {
+ SendGridClient client = new SendGridClient(USERNAME, PASSWORD);
+ Block.Get request = new Block.Get();
+ List<Block> list = client.getBlocks(request);
+ assertTrue(list.size() == 0);
+ }
+
+ @Test
+ public void delete() throws Exception {
+ SendGridClient client = new SendGridClient(USERNAME, PASSWORD);
+ Block.Delete request = new Block.Delete();
+ request.setEmail("hoge@flect.co.jp");
+
+ try {
+ client.deleteBlocks(request);
+ fail();
+ } catch (SendGridException e) {
+ }
+ }
+
+}
Oops, something went wrong.

0 comments on commit f6c8f1d

Please sign in to comment.