Skip to content

Commit de8564c

Browse files
committed
Implemented Suppressions API
1 parent 9dfd60d commit de8564c

File tree

12 files changed

+326
-2
lines changed

12 files changed

+326
-2
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.mailtrap.examples.suppressions;
2+
3+
import io.mailtrap.config.MailtrapConfig;
4+
import io.mailtrap.factory.MailtrapClientFactory;
5+
6+
public class Suppressions {
7+
8+
private static final String TOKEN = "<YOUR MAILTRAP TOKEN>";
9+
private static final long ACCOUNT_ID = 1L;
10+
private static final String EMAIL = "[email protected]";
11+
12+
public static void main(String[] args) {
13+
final var config = new MailtrapConfig.Builder()
14+
.token(TOKEN)
15+
.build();
16+
17+
final var client = MailtrapClientFactory.createMailtrapClient(config);
18+
19+
var searchResponse = client.sendingApi().suppressions()
20+
.search(ACCOUNT_ID, EMAIL);
21+
22+
System.out.println(searchResponse);
23+
24+
if (!searchResponse.isEmpty()) {
25+
var deletedSuppression = client.sendingApi().suppressions()
26+
.deleteSuppression(ACCOUNT_ID, searchResponse.get(0).getId());
27+
28+
System.out.println(deletedSuppression);
29+
}
30+
}
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.mailtrap.api.suppressions;
2+
3+
import io.mailtrap.model.response.suppressions.SuppressionsResponse;
4+
5+
import java.util.List;
6+
7+
public interface Suppressions {
8+
9+
/**
10+
* List and search suppressions by email. The endpoint returns up to 1000 suppressions per request.
11+
*
12+
* @param accountId - unique account ID
13+
* @param email - search suppressions for this email
14+
* @return a list of suppressions
15+
*/
16+
List<SuppressionsResponse> search(long accountId, String email);
17+
18+
/**
19+
* Delete a suppression by ID. Mailtrap will no longer prevent sending to this email unless it's recorded in suppressions again.
20+
*
21+
* @param accountId - unique account ID
22+
* @param suppressionId - unique suppression ID
23+
* @return the attributes of the deleted suppression
24+
*/
25+
SuppressionsResponse deleteSuppression(long accountId, String suppressionId);
26+
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.mailtrap.api.suppressions;
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.response.suppressions.SuppressionsResponse;
8+
9+
import java.net.URLEncoder;
10+
import java.nio.charset.Charset;
11+
import java.util.List;
12+
import java.util.Optional;
13+
14+
import static io.mailtrap.http.RequestData.entry;
15+
16+
public class SuppressionsImpl extends ApiResource implements Suppressions {
17+
18+
public SuppressionsImpl(MailtrapConfig config) {
19+
super(config);
20+
this.apiHost = Constants.GENERAL_HOST;
21+
}
22+
23+
@Override
24+
public List<SuppressionsResponse> search(long accountId, String email) {
25+
var queryParams = RequestData.buildQueryParams(entry("email", Optional.ofNullable(email)));
26+
27+
return
28+
httpClient.getList(
29+
String.format(apiHost + "/api/accounts/%d/suppressions", accountId),
30+
new RequestData(queryParams),
31+
SuppressionsResponse.class
32+
);
33+
}
34+
35+
@Override
36+
public SuppressionsResponse deleteSuppression(long accountId, String suppressionId) {
37+
return
38+
httpClient.delete(
39+
String.format(apiHost + "/api/accounts/%d/suppressions/%s", accountId, URLEncoder.encode(suppressionId, Charset.defaultCharset())),
40+
new RequestData(),
41+
SuppressionsResponse.class
42+
);
43+
}
44+
}

src/main/java/io/mailtrap/client/api/MailtrapEmailSendingApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.mailtrap.api.sendingdomains.SendingDomains;
44
import io.mailtrap.api.sendingemails.SendingEmails;
5+
import io.mailtrap.api.suppressions.Suppressions;
56
import lombok.Getter;
67
import lombok.RequiredArgsConstructor;
78
import lombok.experimental.Accessors;
@@ -15,4 +16,5 @@
1516
public class MailtrapEmailSendingApi {
1617
private final SendingEmails emails;
1718
private final SendingDomains domains;
19+
private final Suppressions suppressions;
1820
}

src/main/java/io/mailtrap/factory/MailtrapClientFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.mailtrap.api.projects.ProjectsImpl;
1717
import io.mailtrap.api.sendingdomains.SendingDomainsImpl;
1818
import io.mailtrap.api.sendingemails.SendingEmailsImpl;
19+
import io.mailtrap.api.suppressions.SuppressionsImpl;
1920
import io.mailtrap.api.testingemails.TestingEmailsImpl;
2021
import io.mailtrap.client.MailtrapClient;
2122
import io.mailtrap.client.api.*;
@@ -73,8 +74,9 @@ private static MailtrapGeneralApi createGeneralApi(MailtrapConfig config) {
7374
private static MailtrapEmailSendingApi createSendingApi(MailtrapConfig config, CustomValidator customValidator) {
7475
final var emails = new SendingEmailsImpl(config, customValidator);
7576
final var domains = new SendingDomainsImpl(config);
77+
final var suppressions = new SuppressionsImpl(config);
7678

77-
return new MailtrapEmailSendingApi(emails, domains);
79+
return new MailtrapEmailSendingApi(emails, domains, suppressions);
7880
}
7981

8082
private static MailtrapEmailTestingApi createTestingApi(MailtrapConfig config, CustomValidator customValidator) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.mailtrap.model.response.suppressions;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
6+
public enum SendingStream {
7+
ANY("any"),
8+
TRANSACTIONAL("transactional"),
9+
BULK("bulk");
10+
11+
private final String value;
12+
13+
SendingStream(String value) {
14+
this.value = value;
15+
}
16+
17+
@JsonValue
18+
public String getValue() {
19+
return value;
20+
}
21+
22+
@JsonCreator
23+
public static SendingStream fromValue(String value) {
24+
for (SendingStream type : SendingStream.values()) {
25+
if (type.value.equalsIgnoreCase(value)) {
26+
return type;
27+
}
28+
}
29+
throw new IllegalArgumentException("Unknown value: " + value);
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.mailtrap.model.response.suppressions;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
6+
public enum SuppressionType {
7+
HARD_BOUNCE("hard bounce"),
8+
SPAM_COMPLAINT("spam complaint"),
9+
UNSUBSCRIPTION("unsubscription"),
10+
MANUAL_IMPORT("manual import");
11+
12+
private final String value;
13+
14+
SuppressionType(String value) {
15+
this.value = value;
16+
}
17+
18+
@JsonValue
19+
public String getValue() {
20+
return value;
21+
}
22+
23+
@JsonCreator
24+
public static SuppressionType fromValue(String value) {
25+
for (SuppressionType type : SuppressionType.values()) {
26+
if (type.value.equalsIgnoreCase(value)) {
27+
return type;
28+
}
29+
}
30+
throw new IllegalArgumentException("Unknown value: " + value);
31+
}
32+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.mailtrap.model.response.suppressions;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.Data;
5+
6+
import java.time.OffsetDateTime;
7+
8+
@Data
9+
public class SuppressionsResponse {
10+
11+
@JsonProperty("id")
12+
private String id;
13+
14+
@JsonProperty("type")
15+
private SuppressionType type;
16+
17+
@JsonProperty("created_at")
18+
private OffsetDateTime createdAt;
19+
20+
@JsonProperty("email")
21+
private String email;
22+
23+
@JsonProperty("sending_stream")
24+
private SendingStream sendingStream;
25+
26+
@JsonProperty("domain_name")
27+
private String domainName;
28+
29+
@JsonProperty("message_bounce_category")
30+
private String messageBounceCategory;
31+
32+
@JsonProperty("message_category")
33+
private String messageCategory;
34+
35+
@JsonProperty("message_client_ip")
36+
private String messageClientIp;
37+
38+
@JsonProperty("message_created_at")
39+
private OffsetDateTime messageCreatedAt;
40+
41+
@JsonProperty("message_outgoing_ip")
42+
private String messageOutgoingIp;
43+
44+
@JsonProperty("message_recipient_mx_name")
45+
private String messageRecipientMxName;
46+
47+
@JsonProperty("message_sender_email")
48+
private String messageSenderEmail;
49+
50+
@JsonProperty("message_subject")
51+
private String messageSubject;
52+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.mailtrap.api.suppressions;
2+
3+
import io.mailtrap.Constants;
4+
import io.mailtrap.config.MailtrapConfig;
5+
import io.mailtrap.factory.MailtrapClientFactory;
6+
import io.mailtrap.model.response.suppressions.SendingStream;
7+
import io.mailtrap.model.response.suppressions.SuppressionType;
8+
import io.mailtrap.model.response.suppressions.SuppressionsResponse;
9+
import io.mailtrap.testutils.BaseTest;
10+
import io.mailtrap.testutils.DataMock;
11+
import io.mailtrap.testutils.TestHttpClient;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
21+
class SuppressionsImplTest extends BaseTest {
22+
23+
private Suppressions api;
24+
25+
@BeforeEach
26+
public void init() {
27+
TestHttpClient httpClient = new TestHttpClient(List.of(
28+
DataMock.build(
29+
Constants.GENERAL_HOST + "/api/accounts/" + accountId + "/suppressions",
30+
"GET", null, "api/suppressions/searchSuppressions.json",
31+
Map.of("email", email)
32+
),
33+
DataMock.build(
34+
Constants.GENERAL_HOST + "/api/accounts/" + accountId + "/suppressions/" + suppressionIdEncoded,
35+
"DELETE", null, "api/suppressions/deleteSuppression.json"
36+
)
37+
));
38+
39+
MailtrapConfig testConfig = new MailtrapConfig.Builder()
40+
.httpClient(httpClient)
41+
.token("dummy_token")
42+
.build();
43+
44+
api = MailtrapClientFactory.createMailtrapClient(testConfig).sendingApi().suppressions();
45+
}
46+
47+
@Test
48+
void test_search() {
49+
List<SuppressionsResponse> searchResponse = api.search(accountId, email);
50+
51+
assertEquals(1, searchResponse.size());
52+
assertEquals(suppressionId, searchResponse.get(0).getId());
53+
assertEquals(email, searchResponse.get(0).getEmail());
54+
assertEquals(SendingStream.BULK, searchResponse.get(0).getSendingStream());
55+
assertEquals(SuppressionType.SPAM_COMPLAINT, searchResponse.get(0).getType());
56+
}
57+
58+
@Test
59+
void test_deleteSuppression() {
60+
SuppressionsResponse deleted = api.deleteSuppression(accountId, suppressionId);
61+
62+
assertNotNull(deleted);
63+
assertEquals(suppressionId, deleted.getId());
64+
assertEquals(email, deleted.getEmail());
65+
assertEquals(SendingStream.BULK, deleted.getSendingStream());
66+
assertEquals(SuppressionType.SPAM_COMPLAINT, deleted.getType());
67+
}
68+
}

src/test/java/io/mailtrap/testutils/BaseTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ public class BaseTest {
1818
protected final String contactUUID = "018dd5e3-f6d2-7c00-8f9b-e5c3f2d8a132";
1919
protected final String contactUUIDEncoded = URLEncoder.encode(contactUUID, Charset.defaultCharset());
2020
protected final long importId = 1L;
21-
protected final long fieldId = 1L;
2221
protected final long getFieldId = 777L;
2322
protected final long updateFieldId = 999L;
2423
protected final long deleteFieldId = 1111L;
24+
protected final String suppressionId = "2fe148b8-b019-431f-ab3f-107663fdf868";
25+
protected final String suppressionIdEncoded = URLEncoder.encode(suppressionId, Charset.defaultCharset());
2526
}

0 commit comments

Comments
 (0)