## Sendgrid API

A small, opinionated Java client for a subset of the
[SendGrid v3 REST API](https://www.twilio.com/docs/sendgrid). 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:

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

### Usage

#### Instantiate

```java
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

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

#### Send a dynamic-template email

```java
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

```java
// 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`                                |
