| .mvn | 1 year ago | ||
| src | 6 months ago | ||
| .gitignore | 1 year ago | ||
| README.md | 6 months ago | ||
| pom.xml | 6 months ago | ||
A small, opinionated Java client for a subset of the SendGrid v3 REST API. It wraps the official sendgrid-java SDK behind an easy-to-use facade for two common use cases:
/stats endpointThe library is published as a Maven artifact:
<dependency>
<groupId>io.ljunggren</groupId>
<artifactId>sendgrid-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
SendgridApi api = new SendgridApi(apiKey);
The API key must be non-null and non-blank; otherwise the constructor throws IllegalArgumentException.
api.sendEmail(from, to, subject, body);
api.sendTemplateEmail(templateId, from, to, templateData);
templateData is a Map<String, Object> whose keys map to {{ variable }} placeholders in the SendGrid template. It may be null or empty.
// Today only. List<Statistics> today = api.getDailyUsage(); // Day-by-day stats for the current calendar month so far. List<Statistics> monthToDate = api.getMonthlyStatistics(); // Day-by-day stats for the last N days (inclusive of today). List<Statistics> lastWeek = api.getStatistics(7); // Stats aggregated by week or month. List<Statistics> byWeek = api.getStatistics(28, Aggregation.WEEK); List<Statistics> byMonth = api.getStatistics(90, Aggregation.MONTH);
All operations throw SendgridException, a checked exception, when the underlying SendGrid call fails. The exception always preserves the original cause when one exists, so wrapping IO or parse errors do not lose the stack trace.
| Failure mode | Behavior |
|---|---|
| Network / IO error | SendgridException with the IOException as cause |
| Email send returns non-202 | SendgridException whose message includes status + body |
| Stats endpoint returns non-2xx | SendgridException whose message includes status + body |
| Stats response cannot be parsed | SendgridException with the parse error as cause |
| Null/blank required argument | IllegalArgumentException |
mvn clean install
mvn test
The project ships with a JUnit 5 test suite covering the public facade, both services, the model classes, and the exception types. Mockito is used to stub the underlying SendGrid client so tests run fully offline.
src/main/java/io/ljunggren/sendgrid/api/
SendgridApi.java -- public facade
exception/SendgridException -- checked exception
model/ -- Jackson-bound DTOs (Statistics, Stat, Metrics)
service/ -- EmailService, UsageService, BaseService, Aggregation
src/test/java/ -- JUnit 5 + Mockito tests
Statistics.date is exposed as java.time.LocalDate. The service registers the JavaTimeModule so deserialization works out of the box.SendgridApi, EmailService, and UsageService exist purely to enable mocking in tests; the public API surface still takes only an API key.