Newer
Older
sendgrid-api / src / main / java / io / ljunggren / sendgrid / api / SendgridApi.java
package io.ljunggren.sendgrid.api;

import java.util.List;
import java.util.Map;

import io.ljunggren.sendgrid.api.exception.SendgridException;
import io.ljunggren.sendgrid.api.model.Statistics;
import io.ljunggren.sendgrid.api.service.Aggregation;
import io.ljunggren.sendgrid.api.service.EmailService;
import io.ljunggren.sendgrid.api.service.SuppressionService;
import io.ljunggren.sendgrid.api.service.UsageService;

/**
 * Public facade for the SendGrid REST API. Wraps the email
 * sending and usage statistics services so callers only need a
 * single API key and a single object.
 *
 * <p>Example:
 * <pre>{@code
 * SendgridApi api = new SendgridApi(apiKey);
 * api.sendEmail(from, to, subject, body);
 * }</pre>
 */
public class SendgridApi {

	private final EmailService emailService;
	private final UsageService usageService;
	private final SuppressionService suppressionService;

	/**
	 * Create a new {@code SendgridApi} using the given API key.
	 *
	 * @param apiKey the SendGrid API key
	 * @throws IllegalArgumentException if {@code apiKey} is null or blank
	 */
	public SendgridApi(String apiKey) {
		this(new EmailService(apiKey), new UsageService(apiKey), new SuppressionService(apiKey));
	}

	/**
	 * Package-private constructor primarily used by tests to
	 * inject mock services.
	 *
	 * @param emailService email service
	 * @param usageService usage service
	 */
	SendgridApi(EmailService emailService, UsageService usageService) {
		this(emailService, usageService, null);
	}

	/**
	 * Package-private constructor primarily used by tests to
	 * inject mock services.
	 *
	 * @param emailService       email service
	 * @param usageService       usage service
	 * @param suppressionService unsubscribe group suppressions (may be null in tests that don't use it)
	 */
	SendgridApi(EmailService emailService, UsageService usageService, SuppressionService suppressionService) {
		if (emailService == null || usageService == null) {
			throw new IllegalArgumentException("emailService and usageService must not be null");
		}
		this.emailService = emailService;
		this.usageService = usageService;
		this.suppressionService = suppressionService;
	}

	/**
	 * Send a plain-text email.
	 *
	 * @param from    sender address
	 * @param to      recipient address
	 * @param subject email subject
	 * @param body    plain-text body
	 * @throws SendgridException if the email cannot be sent
	 */
	public void sendEmail(String from, String to, String subject, String body) throws SendgridException {
		emailService.sendEmail(from, to, subject, body);
	}

	/**
	 * Send a SendGrid template email.
	 *
	 * @param templateId   SendGrid template id
	 * @param from         sender address
	 * @param to           recipient address
	 * @param templateData substitution variables
	 * @throws SendgridException if the email cannot be sent
	 */
	public void sendTemplateEmail(String templateId, String from, String to, Map<String, Object> templateData) throws SendgridException {
		emailService.sendTemplateEmail(templateId, from, to, templateData);
	}

	/**
	 * Send a SendGrid template email in an unsubscribe group: SendGrid adds the unsubscribe link and header, and
	 * skips people who unsubscribed from that group.
	 *
	 * @param templateId   SendGrid template id
	 * @param from         sender address
	 * @param to           recipient address
	 * @param templateData substitution variables
	 * @param asmGroupId   unsubscribe group id; null for none
	 * @throws SendgridException if the email cannot be sent
	 */
	public void sendTemplateEmail(String templateId, String from, String to, Map<String, Object> templateData,
			Integer asmGroupId) throws SendgridException {
		emailService.sendTemplateEmail(templateId, from, to, templateData, asmGroupId);
	}

	/**
	 * Take an address off an unsubscribe group's suppression list (it opted back in somewhere else).
	 *
	 * @param groupId unsubscribe group id
	 * @param email   the address
	 * @throws SendgridException on error
	 */
	public void deleteGroupSuppression(int groupId, String email) throws SendgridException {
		suppressionService.deleteGroupSuppression(groupId, email);
	}

	/**
	 * @return statistics for today only
	 * @throws SendgridException on error
	 */
	public List<Statistics> getDailyUsage() throws SendgridException {
		return usageService.getDailyUsage();
	}

	/**
	 * @return daily statistics for the current calendar month so far
	 * @throws SendgridException on error
	 */
	public List<Statistics> getMonthlyStatistics() throws SendgridException {
		return usageService.getMonthlyStatistics();
	}

	/**
	 * @param days number of days of history (inclusive of today)
	 * @return daily statistics for the last {@code days} days
	 * @throws SendgridException on error
	 */
	public List<Statistics> getStatistics(int days) throws SendgridException {
		return usageService.getStatistics(days);
	}

	/**
	 * @param days        number of days of history (inclusive of today)
	 * @param aggregation aggregation granularity
	 * @return statistics for the last {@code days} days, aggregated as requested
	 * @throws SendgridException on error
	 */
	public List<Statistics> getStatistics(int days, Aggregation aggregation) throws SendgridException {
		return usageService.getStatistics(days, aggregation);
	}

}