diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7c1a0f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/target/ +/.settings/ +/.classpath +/.project +/build/ +/.idea/ +*.iml +node_modules +**/resources/public diff --git a/.mvn/jvm.config b/.mvn/jvm.config new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/.mvn/jvm.config diff --git a/.mvn/maven.config b/.mvn/maven.config new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/.mvn/maven.config diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..d0f1001 --- /dev/null +++ b/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + io.ljunggren + sendgrid-api + 0.0.1-SNAPSHOT + jar + + sendgrid-api + + + UTF-8 + 17 + + + + + + org.junit + junit-bom + 5.11.0 + pom + import + + + + + + + com.sendgrid + sendgrid-java + 4.10.3 + + + io.ljunggren + json-utils + 0.0.1-SNAPSHOT + + + commons-io + commons-io + 2.20.0 + + + org.projectlombok + lombok + 1.18.38 + provided + + + org.junit.jupiter + junit-jupiter-params + test + + + org.junit.jupiter + junit-jupiter-api + test + + + + diff --git a/src/main/java/io/ljunggren/sendgrid/api/SendgridApi.java b/src/main/java/io/ljunggren/sendgrid/api/SendgridApi.java new file mode 100644 index 0000000..d5656c1 --- /dev/null +++ b/src/main/java/io/ljunggren/sendgrid/api/SendgridApi.java @@ -0,0 +1,38 @@ +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.DailyUsage; +import io.ljunggren.sendgrid.api.model.Statistics; +import io.ljunggren.sendgrid.api.service.EmailService; +import io.ljunggren.sendgrid.api.service.UsageService; + +public class SendgridApi { + + private EmailService emailService; + private UsageService usageService; + + public SendgridApi(String apiKey) { + this.emailService = new EmailService(apiKey); + this.usageService = new UsageService(apiKey); + } + + public void sendEmail(String from, String to, String subject, String body) throws SendgridException { + emailService.sendEmail(from, to, subject, body); + } + + public void sendTemplateEmail(String templateId, String from, String to, Map templateData) throws SendgridException { + emailService.sendTemplateEmail(templateId, from, to, templateData); + } + + public DailyUsage getDailyUsage() throws SendgridException { + return usageService.getDailyUsage(); + } + + public List getStatistics(int days) throws SendgridException { + return usageService.getStatistics(days); + } + +} diff --git a/src/main/java/io/ljunggren/sendgrid/api/exception/SendgridException.java b/src/main/java/io/ljunggren/sendgrid/api/exception/SendgridException.java new file mode 100644 index 0000000..bc306d6 --- /dev/null +++ b/src/main/java/io/ljunggren/sendgrid/api/exception/SendgridException.java @@ -0,0 +1,15 @@ +package io.ljunggren.sendgrid.api.exception; + +public class SendgridException extends Exception { + + private static final long serialVersionUID = -2630051469419822905L; + + public SendgridException(String message) { + super(message); + } + + public SendgridException(Exception e) { + super(e); + } + +} diff --git a/src/main/java/io/ljunggren/sendgrid/api/model/DailyUsage.java b/src/main/java/io/ljunggren/sendgrid/api/model/DailyUsage.java new file mode 100644 index 0000000..69ac24a --- /dev/null +++ b/src/main/java/io/ljunggren/sendgrid/api/model/DailyUsage.java @@ -0,0 +1,16 @@ +package io.ljunggren.sendgrid.api.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.Data; + +@Data +@JsonInclude(Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class DailyUsage { + + private Metrics metrics; + +} diff --git a/src/main/java/io/ljunggren/sendgrid/api/model/Metrics.java b/src/main/java/io/ljunggren/sendgrid/api/model/Metrics.java new file mode 100644 index 0000000..73664f3 --- /dev/null +++ b/src/main/java/io/ljunggren/sendgrid/api/model/Metrics.java @@ -0,0 +1,42 @@ +package io.ljunggren.sendgrid.api.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.Data; + +@Data +@JsonInclude(Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class Metrics { + + private int blocks; + @JsonProperty("bounce_drops") + private int bounceDrops; + private int bounces; + private int clicks; + private int deferred; + private int delivered; + @JsonProperty("invalid_emails") + private int invalidEmails; + private int opens; + private int processed; + private int requests; + @JsonProperty("spam_report_drops") + private int spamReportDrops; + @JsonProperty("spam_reports") + private int spamReports; + @JsonProperty("unique_clicks") + private int uniqueClicks; + @JsonProperty("unique_opens") + private int uniqueOpens; + @JsonProperty("unsubscribe_drops") + private int unsubscribeDrops; + private int unsubscribes; + + // transient + private int dailyLimit; + +} diff --git a/src/main/java/io/ljunggren/sendgrid/api/model/Stat.java b/src/main/java/io/ljunggren/sendgrid/api/model/Stat.java new file mode 100644 index 0000000..5e267c5 --- /dev/null +++ b/src/main/java/io/ljunggren/sendgrid/api/model/Stat.java @@ -0,0 +1,16 @@ +package io.ljunggren.sendgrid.api.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.Data; + +@Data +@JsonInclude(Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class Stat { + + private Metrics metrics; + +} diff --git a/src/main/java/io/ljunggren/sendgrid/api/model/Statistics.java b/src/main/java/io/ljunggren/sendgrid/api/model/Statistics.java new file mode 100644 index 0000000..1b38a7b --- /dev/null +++ b/src/main/java/io/ljunggren/sendgrid/api/model/Statistics.java @@ -0,0 +1,22 @@ +package io.ljunggren.sendgrid.api.model; + +import java.util.Date; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.Data; + +@Data +@JsonInclude(Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class Statistics { + + @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd") + private Date date; + private List stats; + +} \ No newline at end of file diff --git a/src/main/java/io/ljunggren/sendgrid/api/service/BaseService.java b/src/main/java/io/ljunggren/sendgrid/api/service/BaseService.java new file mode 100644 index 0000000..b9522eb --- /dev/null +++ b/src/main/java/io/ljunggren/sendgrid/api/service/BaseService.java @@ -0,0 +1,13 @@ +package io.ljunggren.sendgrid.api.service; + +import com.sendgrid.SendGrid; + +public class BaseService { + + protected SendGrid sendGrid; + + protected BaseService(String apiKey) { + this.sendGrid = new SendGrid(apiKey); + } + +} diff --git a/src/main/java/io/ljunggren/sendgrid/api/service/EmailService.java b/src/main/java/io/ljunggren/sendgrid/api/service/EmailService.java new file mode 100644 index 0000000..9a4c466 --- /dev/null +++ b/src/main/java/io/ljunggren/sendgrid/api/service/EmailService.java @@ -0,0 +1,58 @@ +package io.ljunggren.sendgrid.api.service; + +import java.io.IOException; +import java.util.Map; + +import com.sendgrid.Method; +import com.sendgrid.Request; +import com.sendgrid.Response; +import com.sendgrid.helpers.mail.Mail; +import com.sendgrid.helpers.mail.objects.Content; +import com.sendgrid.helpers.mail.objects.Email; +import com.sendgrid.helpers.mail.objects.Personalization; + +import io.ljunggren.sendgrid.api.exception.SendgridException; + +public class EmailService extends BaseService { + + public EmailService(String apiKey) { + super(apiKey); + } + + public void sendEmail(String from, String to, String subject, String body) throws SendgridException { + Mail mail = new Mail( + new Email(from), + subject, + new Email(to), + new Content("text/plain", body)); + sendEmail(mail); + } + + public void sendTemplateEmail(String templateId, String from, String to, Map templateData) throws SendgridException { + Mail mail = new Mail(); + mail.setTemplateId(templateId); + mail.setFrom(new Email(from)); + Personalization personalization = new Personalization(); + personalization.addTo(new Email(to)); + templateData.entrySet().stream().forEach(entry -> personalization.addDynamicTemplateData(entry.getKey(), entry.getValue())); + mail.addPersonalization(personalization); + sendEmail(mail); + } + + private void sendEmail(Mail mail) throws SendgridException { + try { + Request request = new Request(); + request.setMethod(Method.POST); + request.setEndpoint("mail/send"); + request.setBody(mail.build()); + Response response = sendGrid.api(request); + if (response.getStatusCode() == 202) { + return; + } + throw new SendgridException("Error sending email: " + response.getBody()); + } catch (IOException e) { + throw new SendgridException(e.getMessage()); + } + } + +} diff --git a/src/main/java/io/ljunggren/sendgrid/api/service/UsageService.java b/src/main/java/io/ljunggren/sendgrid/api/service/UsageService.java new file mode 100644 index 0000000..b957e5e --- /dev/null +++ b/src/main/java/io/ljunggren/sendgrid/api/service/UsageService.java @@ -0,0 +1,57 @@ +package io.ljunggren.sendgrid.api.service; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.List; + +import com.sendgrid.Method; +import com.sendgrid.Request; +import com.sendgrid.Response; + +import io.ljunggren.json.utils.JsonUtils; +import io.ljunggren.sendgrid.api.exception.SendgridException; +import io.ljunggren.sendgrid.api.model.DailyUsage; +import io.ljunggren.sendgrid.api.model.Statistics; + +public class UsageService extends BaseService { + + public UsageService(String apiKey) { + super(apiKey); + } + + public DailyUsage getDailyUsage() throws SendgridException { + List statisticses = getStatistics(0); + if (statisticses == null) { + throw new SendgridException("Could not fetch SendGrid statistics"); + } + DailyUsage dailyUsage = new DailyUsage(); + dailyUsage.setMetrics(statisticses.get(0).getStats().get(0).getMetrics()); + return dailyUsage; + } + + public List getStatistics(int days) throws SendgridException { + LocalDate endDate = LocalDate.now(); + LocalDate startDate = endDate.minusDays(days); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + return getStatistics("day", + startDate.format(formatter), + endDate.format(formatter) + ); + } + + private List getStatistics(String aggregate, String startDate, String endDate) throws SendgridException { + Request request = new Request(); + request.setMethod(Method.GET); + request.setEndpoint("stats"); + request.addQueryParam("aggregated_by", aggregate); + request.addQueryParam("start_date", startDate); + request.addQueryParam("end_date", endDate); + try { + Response response = sendGrid.api(request); + return JsonUtils.jsonToList(response.getBody(), Statistics.class); + } catch (Exception e) { + throw new SendgridException(e); + } + } + +} diff --git a/src/test/java/io/ljunggren/sendgrid/api/model/StatisticsTest.java b/src/test/java/io/ljunggren/sendgrid/api/model/StatisticsTest.java new file mode 100644 index 0000000..f63d7e6 --- /dev/null +++ b/src/test/java/io/ljunggren/sendgrid/api/model/StatisticsTest.java @@ -0,0 +1,26 @@ +package io.ljunggren.sendgrid.api.model; + +import static org.junit.jupiter.api.Assertions.*; + +import java.io.IOException; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.junit.jupiter.api.Test; + +import io.ljunggren.json.utils.JsonUtils; + +class StatisticsTest { + + private String readFromResources(String file) throws IOException { + return IOUtils.toString(this.getClass().getResourceAsStream(file), "UTF-8"); + } + + @Test + void instantiateTest() throws IOException { + String json = readFromResources("/io/ljunggren/sendgrid/api/model/stats.json"); + List statisticses = JsonUtils.jsonToList(json, Statistics.class); + assertEquals(2, statisticses.size()); + } + +} diff --git a/src/test/resources/io/ljunggren/sendgrid/api/model/stats.json b/src/test/resources/io/ljunggren/sendgrid/api/model/stats.json new file mode 100644 index 0000000..e2b9f04 --- /dev/null +++ b/src/test/resources/io/ljunggren/sendgrid/api/model/stats.json @@ -0,0 +1,45 @@ +[ { + "date" : "2025-10-18", + "stats" : [ { + "metrics" : { + "blocks" : 0, + "bounce_drops" : 0, + "bounces" : 0, + "clicks" : 0, + "deferred" : 0, + "delivered" : 0, + "invalid_emails" : 0, + "opens" : 0, + "processed" : 0, + "requests" : 0, + "spam_report_drops" : 0, + "spam_reports" : 0, + "unique_clicks" : 0, + "unique_opens" : 0, + "unsubscribe_drops" : 0, + "unsubscribes" : 0 + } + } ] +}, { + "date" : "2025-10-19", + "stats" : [ { + "metrics" : { + "blocks" : 0, + "bounce_drops" : 0, + "bounces" : 0, + "clicks" : 0, + "deferred" : 0, + "delivered" : 1, + "invalid_emails" : 0, + "opens" : 0, + "processed" : 1, + "requests" : 1, + "spam_report_drops" : 0, + "spam_reports" : 0, + "unique_clicks" : 0, + "unique_opens" : 0, + "unsubscribe_drops" : 0, + "unsubscribes" : 0 + } + } ] +} ] \ No newline at end of file