|
| 1 | +package io.mailtrap.api.emaillogs; |
| 2 | + |
| 3 | +import io.mailtrap.Constants; |
| 4 | +import io.mailtrap.api.apiresource.ApiResource; |
| 5 | +import io.mailtrap.config.MailtrapConfig; |
| 6 | +import io.mailtrap.http.RequestData; |
| 7 | +import io.mailtrap.model.request.emaillogs.EmailLogFilter; |
| 8 | +import io.mailtrap.model.request.emaillogs.EmailLogsListFilters; |
| 9 | +import io.mailtrap.model.response.emaillogs.EmailLogsListResponse; |
| 10 | +import io.mailtrap.model.response.emaillogs.EmailLogMessage; |
| 11 | + |
| 12 | +import java.net.URLEncoder; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Collection; |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.List; |
| 18 | +import java.util.stream.Collectors; |
| 19 | + |
| 20 | +public class EmailLogsImpl extends ApiResource implements EmailLogs { |
| 21 | + |
| 22 | + public EmailLogsImpl(final MailtrapConfig config) { |
| 23 | + super(config); |
| 24 | + this.apiHost = Constants.GENERAL_HOST; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public EmailLogsListResponse list(final long accountId, final String searchAfter, |
| 29 | + final EmailLogsListFilters filters) { |
| 30 | + final String queryString = buildQueryString(searchAfter, filters); |
| 31 | + final String url = String.format("%s/api/accounts/%d/email_logs", apiHost, accountId) |
| 32 | + + (queryString.isEmpty() ? "" : "?" + queryString); |
| 33 | + |
| 34 | + return httpClient.get(url, new RequestData(), EmailLogsListResponse.class); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public EmailLogMessage get(final long accountId, final String sendingMessageId) { |
| 39 | + if (sendingMessageId == null || sendingMessageId.isBlank()) { |
| 40 | + throw new IllegalArgumentException("sendingMessageId must not be null or blank"); |
| 41 | + } |
| 42 | + final String url = String.format("%s/api/accounts/%d/email_logs/%s", apiHost, accountId, sendingMessageId); |
| 43 | + return httpClient.get(url, new RequestData(), EmailLogMessage.class); |
| 44 | + } |
| 45 | + |
| 46 | + private static String buildQueryString(final String searchAfter, final EmailLogsListFilters filters) { |
| 47 | + final List<String> params = new ArrayList<>(); |
| 48 | + |
| 49 | + if (searchAfter != null && !searchAfter.isBlank()) { |
| 50 | + params.add(enc("search_after") + "=" + enc(searchAfter)); |
| 51 | + } |
| 52 | + |
| 53 | + if (filters != null) { |
| 54 | + appendFilter(params, "sent_after", filters.getSentAfter()); |
| 55 | + appendFilter(params, "sent_before", filters.getSentBefore()); |
| 56 | + appendOperatorValue(params, "to", filters.getTo()); |
| 57 | + appendOperatorValue(params, "from", filters.getFrom()); |
| 58 | + appendOperatorValue(params, "subject", filters.getSubject()); |
| 59 | + appendOperatorValue(params, "status", filters.getStatus()); |
| 60 | + appendOperatorValue(params, "events", filters.getEvents()); |
| 61 | + appendOperatorValue(params, "clicks_count", filters.getClicksCount()); |
| 62 | + appendOperatorValue(params, "opens_count", filters.getOpensCount()); |
| 63 | + appendOperatorValue(params, "client_ip", filters.getClientIp()); |
| 64 | + appendOperatorValue(params, "sending_ip", filters.getSendingIp()); |
| 65 | + appendOperatorValue(params, "email_service_provider_response", filters.getEmailServiceProviderResponse()); |
| 66 | + appendOperatorValue(params, "email_service_provider", filters.getEmailServiceProvider()); |
| 67 | + appendOperatorValue(params, "recipient_mx", filters.getRecipientMx()); |
| 68 | + appendOperatorValue(params, "category", filters.getCategory()); |
| 69 | + appendOperatorValue(params, "sending_domain_id", filters.getSendingDomainId()); |
| 70 | + appendOperatorValue(params, "sending_stream", filters.getSendingStream()); |
| 71 | + } |
| 72 | + |
| 73 | + return String.join("&", params); |
| 74 | + } |
| 75 | + |
| 76 | + private static void appendFilter(final List<String> params, final String key, final String value) { |
| 77 | + if (value != null && !value.isBlank()) { |
| 78 | + params.add(enc("filters[" + key + "]") + "=" + enc(value)); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static void appendOperatorValue(final List<String> params, final String field, final EmailLogFilter spec) { |
| 83 | + if (spec == null) |
| 84 | + return; |
| 85 | + final String operator = spec.getOperatorString(); |
| 86 | + if (operator == null || operator.isBlank()) |
| 87 | + return; |
| 88 | + params.add(enc("filters[" + field + "][operator]") + "=" + enc(operator)); |
| 89 | + final Object value = spec.getValue(); |
| 90 | + if (value != null) { |
| 91 | + for (final String v : toValueList(value)) { |
| 92 | + params.add(enc("filters[" + field + "][value]") + "=" + enc(String.valueOf(v))); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static List<String> toValueList(final Object value) { |
| 98 | + if (value instanceof Collection<?> c) { |
| 99 | + return c.stream() |
| 100 | + .filter(v -> v != null) |
| 101 | + .map(String::valueOf) |
| 102 | + .collect(Collectors.toList()); |
| 103 | + } |
| 104 | + return Collections.singletonList(String.valueOf(value)); |
| 105 | + } |
| 106 | + |
| 107 | + private static String enc(final String s) { |
| 108 | + return URLEncoder.encode(s, StandardCharsets.UTF_8); |
| 109 | + } |
| 110 | +} |
0 commit comments