.mvn Initial commit 1 year ago
src Code review and quality improvements across the codebase 6 months ago
.gitignore Initial commit 1 year ago
README.md Code review and quality improvements across the codebase 6 months ago
pom.xml Code review and quality improvements across the codebase 6 months ago
README.md

Sendgrid API

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:

  • Sending plain-text and dynamic-template emails
  • Pulling email-sending statistics from the /stats endpoint

Requirements

  • Java 17+
  • Maven 3.9+

Installation

The library is published as a Maven artifact:

<dependency>
    <groupId>io.ljunggren</groupId>
    <artifactId>sendgrid-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

Usage

Instantiate

SendgridApi api = new SendgridApi(apiKey);

The API key must be non-null and non-blank; otherwise the constructor throws IllegalArgumentException.

Send a plain-text email

api.sendEmail(from, to, subject, body);

Send a dynamic-template email

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.

Fetch usage statistics

// 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);

Error handling

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

Building

mvn clean install

Running tests

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.

Project layout

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

Notes

  • Statistics.date is exposed as java.time.LocalDate. The service registers the JavaTimeModule so deserialization works out of the box.
  • The package-private constructors on SendgridApi, EmailService, and UsageService exist purely to enable mocking in tests; the public API surface still takes only an API key.