com.github.krenfro.sendgrid.asm.SuppressionManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sendgrid-asm-java Show documentation
Show all versions of sendgrid-asm-java Show documentation
SendGrid Advanced Suppression Manager (ASM) Java Client
package com.github.krenfro.sendgrid.asm;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.util.*;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
/**
* SuppressionManager are email addresses that can be added to Groups to prevent
* certain types of emails from being delivered to those addresses.
*
* https://sendgrid.com/docs/API_Reference/Web_API_v3/Advanced_Suppression_Manager/suppressions.html
*/
public class SuppressionManager extends SendGrid{
public SuppressionManager(String username, String password){
super(username, password);
}
public SuppressionManager(String apiKey){
super(apiKey);
}
public List add(Group group, String ... email) throws IOException{
return add(group.getId(), email);
}
public List add(int groupId, String ... email) throws IOException{
List suppressions = new ArrayList<>();
Map map = new HashMap<>();
map.put("recipient_emails", email);
String payload = jackson.writeValueAsString(map);
Request post = Request.Post(baseUrl + "/groups/" + groupId + "/suppressions")
.addHeader("Accept", "application/json")
.addHeader("Authorization", authHeader);
try{
String json = post
.bodyString(payload, ContentType.APPLICATION_JSON)
.execute().returnContent().asString();
JsonNode array = jackson.readTree(json).path("recipient_emails");
if (array.isArray()){
for (final JsonNode entry : array) {
suppressions.add(entry.asText());
}
}
}
catch(HttpResponseException ex){
throw new IOException(ex);
}
return suppressions;
}
public void remove(Group group, String ... email) throws IOException{
remove(group.getId(), email);
}
public void remove(int groupId, String ... email) throws IOException{
for (String individual: email){
Request delete = Request.Delete(baseUrl + "/groups/" + groupId + "/suppressions/" + individual)
.addHeader("Accept", "application/json")
.addHeader("Authorization", authHeader);
try{
delete.execute();
}
catch(HttpResponseException ex){
throw new IOException(ex);
}
}
}
public List retrieve(Group group) throws IOException{
return retrieve(group.getId());
}
public List retrieve(int groupId) throws IOException{
List suppressions = new ArrayList<>();
Request get = Request.Get(baseUrl + "/groups/" + groupId + "/suppressions")
.addHeader("Accept", "application/json")
.addHeader("Authorization", authHeader);
try{
String json = get.execute().returnContent().asString();
JsonNode array = jackson.readTree(json);
if (array.isArray()){
for (final JsonNode entry : array) {
suppressions.add(entry.asText());
}
}
}
catch(HttpResponseException ex){
throw new IOException(ex);
}
return suppressions;
}
public List retrieve(String email) throws IOException{
List suppressions = new ArrayList<>();
Request get = Request.Get(baseUrl + "/suppressions/" + email)
.addHeader("Accept", "application/json")
.addHeader("Authorization", authHeader);
try{
String json = get.execute().returnContent().asString();
TypeReference