All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.krenfro.sendgrid.asm.SuppressionManager Maven / Gradle / Ivy

There is a newer version: 0.0.4
Show newest version
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>> typeRef = new TypeReference>>() {};
            Map> map = jackson.readValue(json, typeRef);
            suppressions.addAll(map.get("suppressions"));
        }
        catch(HttpResponseException ex){
            throw new IOException(ex);
        }
        return suppressions;
    }

    public void save(String email, List suppressions) throws IOException{
        Set previouslySuppressed = new HashSet<>();
        for (Suppression s: retrieve(email)){
            if (s.isSuppressed()){
                previouslySuppressed.add(s.getId());
            }
        }
        for (Suppression s: suppressions){
            if (s.isSuppressed() && !previouslySuppressed.contains(s.getId())){
                add(s.getId(), email);
            }
            else if(!s.isSuppressed() && previouslySuppressed.contains(s.getId())){
                remove(s.getId(), email);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy