-
Notifications
You must be signed in to change notification settings - Fork 0
Add Stats API #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
piobeny
wants to merge
1
commit into
main
Choose a base branch
from
stats-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+560
−1
Open
Add Stats API #48
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
examples/java/io/mailtrap/examples/general/StatsExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package io.mailtrap.examples.general; | ||
|
|
||
| import io.mailtrap.api.stats.StatsFilter; | ||
| import io.mailtrap.config.MailtrapConfig; | ||
| import io.mailtrap.factory.MailtrapClientFactory; | ||
|
|
||
| public class StatsExample { | ||
|
|
||
| private static final String TOKEN = "<YOUR MAILTRAP TOKEN>"; | ||
| private static final long ACCOUNT_ID = 1L; | ||
|
|
||
| // Set these to the desired date range (format: YYYY-MM-DD) | ||
| private static final String START_DATE = "<START_DATE>"; | ||
| private static final String END_DATE = "<END_DATE>"; | ||
|
|
||
| public static void main(String[] args) { | ||
| final var config = new MailtrapConfig.Builder() | ||
| .token(TOKEN) | ||
| .build(); | ||
|
|
||
| final var client = MailtrapClientFactory.createMailtrapClient(config); | ||
|
|
||
| final var filter = StatsFilter.builder() | ||
| .startDate(START_DATE) | ||
| .endDate(END_DATE) | ||
| .build(); | ||
|
|
||
| System.out.println("=== Aggregated Stats ==="); | ||
| System.out.println(client.generalApi().stats().getStats(ACCOUNT_ID, filter)); | ||
|
|
||
| System.out.println("\n=== Stats by Domains ==="); | ||
| System.out.println(client.generalApi().stats().byDomain(ACCOUNT_ID, filter)); | ||
|
|
||
| System.out.println("\n=== Stats by Categories ==="); | ||
| System.out.println(client.generalApi().stats().byCategory(ACCOUNT_ID, filter)); | ||
|
|
||
| System.out.println("\n=== Stats by Email Service Providers ==="); | ||
| System.out.println(client.generalApi().stats().byEmailServiceProvider(ACCOUNT_ID, filter)); | ||
|
|
||
| System.out.println("\n=== Stats by Date ==="); | ||
| System.out.println(client.generalApi().stats().byDate(ACCOUNT_ID, filter)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package io.mailtrap.api.stats; | ||
|
|
||
| import io.mailtrap.model.response.stats.SendingStatGroupResponse; | ||
| import io.mailtrap.model.response.stats.SendingStatsResponse; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Interface representing the Mailtrap API for interaction with sending statistics | ||
| */ | ||
| public interface Stats { | ||
|
|
||
| /** | ||
| * Get aggregated sending stats | ||
| * | ||
| * @param accountId unique account ID | ||
| * @param filter stats filter parameters | ||
| * @return aggregated sending statistics | ||
| */ | ||
| SendingStatsResponse getStats(long accountId, StatsFilter filter); | ||
|
|
||
| /** | ||
| * Get sending stats grouped by domains | ||
| * | ||
| * @param accountId unique account ID | ||
| * @param filter stats filter parameters | ||
| * @return list of sending statistics grouped by domain | ||
| */ | ||
| List<SendingStatGroupResponse> byDomain(long accountId, StatsFilter filter); | ||
|
|
||
| /** | ||
| * Get sending stats grouped by categories | ||
| * | ||
| * @param accountId unique account ID | ||
| * @param filter stats filter parameters | ||
| * @return list of sending statistics grouped by category | ||
| */ | ||
| List<SendingStatGroupResponse> byCategory(long accountId, StatsFilter filter); | ||
|
|
||
| /** | ||
| * Get sending stats grouped by email service providers | ||
| * | ||
| * @param accountId unique account ID | ||
| * @param filter stats filter parameters | ||
| * @return list of sending statistics grouped by email service provider | ||
| */ | ||
| List<SendingStatGroupResponse> byEmailServiceProvider(long accountId, StatsFilter filter); | ||
|
|
||
| /** | ||
| * Get sending stats grouped by date | ||
| * | ||
| * @param accountId unique account ID | ||
| * @param filter stats filter parameters | ||
| * @return list of sending statistics grouped by date | ||
| */ | ||
| List<SendingStatGroupResponse> byDate(long accountId, StatsFilter filter); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package io.mailtrap.api.stats; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Data | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Builder | ||
| public class StatsFilter { | ||
| private String startDate; | ||
| private String endDate; | ||
| private List<Long> sendingDomainIds; | ||
| private List<String> sendingStreams; | ||
| private List<String> categories; | ||
| private List<String> emailServiceProviders; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package io.mailtrap.api.stats; | ||
|
|
||
| import io.mailtrap.Constants; | ||
| import io.mailtrap.api.apiresource.ApiResource; | ||
| import io.mailtrap.config.MailtrapConfig; | ||
| import io.mailtrap.http.RequestData; | ||
| import io.mailtrap.model.response.stats.SendingStatGroupResponse; | ||
| import io.mailtrap.model.response.stats.SendingStatsResponse; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import static io.mailtrap.http.RequestData.entry; | ||
|
|
||
| public class StatsImpl extends ApiResource implements Stats { | ||
|
|
||
| public StatsImpl(final MailtrapConfig config) { | ||
| super(config); | ||
| this.apiHost = Constants.GENERAL_HOST; | ||
| } | ||
|
|
||
| @Override | ||
| public SendingStatsResponse getStats(final long accountId, final StatsFilter filter) { | ||
| return httpClient.get( | ||
| buildUrl(accountId, ""), | ||
| new RequestData(buildStatsQueryParams(filter)), | ||
| SendingStatsResponse.class | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public List<SendingStatGroupResponse> byDomain(final long accountId, final StatsFilter filter) { | ||
| return httpClient.getList( | ||
| buildUrl(accountId, "/domains"), | ||
| new RequestData(buildStatsQueryParams(filter)), | ||
| SendingStatGroupResponse.class | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public List<SendingStatGroupResponse> byCategory(final long accountId, final StatsFilter filter) { | ||
| return httpClient.getList( | ||
| buildUrl(accountId, "/categories"), | ||
| new RequestData(buildStatsQueryParams(filter)), | ||
| SendingStatGroupResponse.class | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public List<SendingStatGroupResponse> byEmailServiceProvider(final long accountId, final StatsFilter filter) { | ||
| return httpClient.getList( | ||
| buildUrl(accountId, "/email_service_providers"), | ||
| new RequestData(buildStatsQueryParams(filter)), | ||
| SendingStatGroupResponse.class | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public List<SendingStatGroupResponse> byDate(final long accountId, final StatsFilter filter) { | ||
| return httpClient.getList( | ||
| buildUrl(accountId, "/date"), | ||
| new RequestData(buildStatsQueryParams(filter)), | ||
| SendingStatGroupResponse.class | ||
| ); | ||
| } | ||
|
|
||
| private String buildUrl(final long accountId, final String suffix) { | ||
| return String.format(apiHost + "/api/accounts/%d/stats%s", accountId, suffix); | ||
| } | ||
|
|
||
| private Map<String, ? extends Optional<?>> buildStatsQueryParams(final StatsFilter filter) { | ||
| if (filter == null) { | ||
| throw new IllegalArgumentException("filter must not be null"); | ||
| } | ||
| if (filter.getStartDate() == null || filter.getStartDate().trim().isEmpty()) { | ||
| throw new IllegalArgumentException("startDate must not be null or empty"); | ||
| } | ||
| if (filter.getEndDate() == null || filter.getEndDate().trim().isEmpty()) { | ||
| throw new IllegalArgumentException("endDate must not be null or empty"); | ||
| } | ||
|
|
||
| return RequestData.buildQueryParams( | ||
| entry("start_date", Optional.ofNullable(filter.getStartDate())), | ||
| entry("end_date", Optional.ofNullable(filter.getEndDate())), | ||
| entry("sending_domain_ids[]", Optional.ofNullable(filter.getSendingDomainIds())), | ||
| entry("sending_streams[]", Optional.ofNullable(filter.getSendingStreams())), | ||
| entry("categories[]", Optional.ofNullable(filter.getCategories())), | ||
| entry("email_service_providers[]", Optional.ofNullable(filter.getEmailServiceProviders())) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/io/mailtrap/model/response/stats/SendingStatGroupResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package io.mailtrap.model.response.stats; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class SendingStatGroupResponse { | ||
|
|
||
| private String name; | ||
| private Object value; | ||
| private SendingStatsResponse stats; | ||
|
|
||
| @JsonAnySetter | ||
| public void setDynamicField(String key, Object value) { | ||
| if (!"stats".equals(key)) { | ||
| this.name = key; | ||
| this.value = value; | ||
| } | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
src/main/java/io/mailtrap/model/response/stats/SendingStatsResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package io.mailtrap.model.response.stats; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class SendingStatsResponse { | ||
|
|
||
| @JsonProperty("delivery_count") | ||
| private int deliveryCount; | ||
|
|
||
| @JsonProperty("delivery_rate") | ||
| private double deliveryRate; | ||
|
|
||
| @JsonProperty("bounce_count") | ||
| private int bounceCount; | ||
|
|
||
| @JsonProperty("bounce_rate") | ||
| private double bounceRate; | ||
|
|
||
| @JsonProperty("open_count") | ||
| private int openCount; | ||
|
|
||
| @JsonProperty("open_rate") | ||
| private double openRate; | ||
|
|
||
| @JsonProperty("click_count") | ||
| private int clickCount; | ||
|
|
||
| @JsonProperty("click_rate") | ||
| private double clickRate; | ||
|
|
||
| @JsonProperty("spam_count") | ||
| private int spamCount; | ||
|
|
||
| @JsonProperty("spam_rate") | ||
| private double spamRate; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.