-
Notifications
You must be signed in to change notification settings - Fork 0
Implement Contact Exports API #41
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
39 changes: 39 additions & 0 deletions
39
examples/java/io/mailtrap/examples/contactexports/ContactExportsExample.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,39 @@ | ||
| package io.mailtrap.examples.contactexports; | ||
|
|
||
| import io.mailtrap.config.MailtrapConfig; | ||
| import io.mailtrap.factory.MailtrapClientFactory; | ||
| import io.mailtrap.model.request.contactexports.ContactExportFilter; | ||
| import io.mailtrap.model.request.contactexports.ContactExportFilterOperator; | ||
| import io.mailtrap.model.request.contactexports.CreateContactsExportRequest; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class ContactExportsExample { | ||
|
|
||
| private static final String TOKEN = "<YOUR MAILTRAP TOKEN>"; | ||
| private static final long ACCOUNT_ID = 1L; | ||
| private static final long FILTER_ID = 1L; | ||
|
|
||
| public static void main(String[] args) { | ||
| final var config = new MailtrapConfig.Builder() | ||
| .token(TOKEN) | ||
| .build(); | ||
|
|
||
| final var client = MailtrapClientFactory.createMailtrapClient(config); | ||
|
|
||
| final var exportFilter = ContactExportFilter.ids(ContactExportFilterOperator.EQUAL, FILTER_ID); | ||
|
|
||
| final var createExportRequest = new CreateContactsExportRequest(List.of(exportFilter)); | ||
|
|
||
| final var createExportResponse = client.contactsApi().contactExports() | ||
| .createContactExport(ACCOUNT_ID, createExportRequest); | ||
|
|
||
| System.out.println(createExportResponse); | ||
|
|
||
| var contactExportResponse = client.contactsApi().contactExports() | ||
| .getContactExport(ACCOUNT_ID, createExportResponse.getId()); | ||
|
|
||
| System.out.println(contactExportResponse); | ||
| } | ||
|
|
||
| } | ||
25 changes: 25 additions & 0 deletions
25
src/main/java/io/mailtrap/api/contactexports/ContactExports.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,25 @@ | ||
| package io.mailtrap.api.contactexports; | ||
|
|
||
| import io.mailtrap.model.request.contactexports.CreateContactsExportRequest; | ||
| import io.mailtrap.model.response.contactexports.ContactExportResponse; | ||
|
|
||
| public interface ContactExports { | ||
|
|
||
| /** | ||
| * Create a new Contact Export | ||
| * | ||
| * @param accountId unique account ID | ||
| * @param request payload | ||
| * @return created contact export | ||
| */ | ||
| ContactExportResponse createContactExport(long accountId, CreateContactsExportRequest request); | ||
|
|
||
| /** | ||
| * Get Contact Export | ||
| * | ||
| * @param accountId unique account ID | ||
| * @param exportId unique contact export ID | ||
| * @return contact export | ||
| */ | ||
| ContactExportResponse getContactExport(long accountId, long exportId); | ||
| } |
35 changes: 35 additions & 0 deletions
35
src/main/java/io/mailtrap/api/contactexports/ContactExportsImpl.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,35 @@ | ||
| package io.mailtrap.api.contactexports; | ||
|
|
||
| import io.mailtrap.Constants; | ||
| import io.mailtrap.api.apiresource.ApiResource; | ||
| import io.mailtrap.config.MailtrapConfig; | ||
| import io.mailtrap.http.RequestData; | ||
| import io.mailtrap.model.request.contactexports.CreateContactsExportRequest; | ||
| import io.mailtrap.model.response.contactexports.ContactExportResponse; | ||
|
|
||
| public class ContactExportsImpl extends ApiResource implements ContactExports { | ||
|
|
||
| public ContactExportsImpl(final MailtrapConfig config) { | ||
| super(config); | ||
| this.apiHost = Constants.GENERAL_HOST; | ||
| } | ||
|
|
||
| @Override | ||
| public ContactExportResponse createContactExport(final long accountId, final CreateContactsExportRequest request) { | ||
| return httpClient.post( | ||
| String.format(apiHost + "/api/accounts/%d/contacts/exports", accountId), | ||
| request, | ||
| new RequestData(), | ||
| ContactExportResponse.class | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public ContactExportResponse getContactExport(final long accountId, final long exportId) { | ||
| return httpClient.get( | ||
| String.format(apiHost + "/api/accounts/%d/contacts/exports/%d", accountId, exportId), | ||
| new RequestData(), | ||
| ContactExportResponse.class | ||
| ); | ||
| } | ||
| } |
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
28 changes: 28 additions & 0 deletions
28
src/main/java/io/mailtrap/model/request/contactexports/ContactExportFilter.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,28 @@ | ||
| package io.mailtrap.model.request.contactexports; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| import static io.mailtrap.model.request.contactexports.ContactExportFilterName.list_id; | ||
| import static io.mailtrap.model.request.contactexports.ContactExportFilterName.subscription_status; | ||
|
|
||
| @Getter | ||
| public class ContactExportFilter { | ||
|
|
||
| private final String name; | ||
| private final String operator; | ||
| private final Object value; | ||
|
|
||
| private ContactExportFilter(final String name, final String operator, final Object value) { | ||
| this.name = name; | ||
| this.operator = operator; | ||
| this.value = value; | ||
| } | ||
|
|
||
| public static ContactExportFilter ids(final ContactExportFilterOperator operator, final Long... ids) { | ||
|
vitalii-t marked this conversation as resolved.
Outdated
|
||
| return new ContactExportFilter(list_id.name(), operator.getValue(), ids); | ||
| } | ||
|
|
||
| public static ContactExportFilter subscriptionStatus(final ContactExportFilterOperator operator, final ContactExportFilterSubscriptionStatus status) { | ||
| return new ContactExportFilter(subscription_status.name(), operator.getValue(), status.getValue()); | ||
| } | ||
| } | ||
6 changes: 6 additions & 0 deletions
6
src/main/java/io/mailtrap/model/request/contactexports/ContactExportFilterName.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,6 @@ | ||
| package io.mailtrap.model.request.contactexports; | ||
|
|
||
| public enum ContactExportFilterName { | ||
| subscription_status, | ||
| list_id | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/io/mailtrap/model/request/contactexports/ContactExportFilterOperator.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,12 @@ | ||
| package io.mailtrap.model.request.contactexports; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum ContactExportFilterOperator { | ||
| EQUAL("equal"); | ||
|
|
||
| private final String value; | ||
| } |
13 changes: 13 additions & 0 deletions
13
.../java/io/mailtrap/model/request/contactexports/ContactExportFilterSubscriptionStatus.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,13 @@ | ||
| package io.mailtrap.model.request.contactexports; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum ContactExportFilterSubscriptionStatus { | ||
| SUBSCRIBED("subscribed"), | ||
| UNSUBSCRIBED("unsubscribed"); | ||
|
|
||
| private final String value; | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/io/mailtrap/model/request/contactexports/CreateContactsExportRequest.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,14 @@ | ||
| package io.mailtrap.model.request.contactexports; | ||
|
|
||
| import io.mailtrap.model.AbstractModel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public class CreateContactsExportRequest extends AbstractModel { | ||
|
|
||
| private List<ContactExportFilter> filters; | ||
| } |
21 changes: 21 additions & 0 deletions
21
src/main/java/io/mailtrap/model/response/contactexports/ContactExportResponse.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,21 @@ | ||
| package io.mailtrap.model.response.contactexports; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class ContactExportResponse { | ||
|
|
||
| private long id; | ||
|
|
||
| private ContactExportStatus status; | ||
|
|
||
| @JsonProperty("created_at") | ||
| private String createdAt; | ||
|
|
||
| @JsonProperty("updated_at") | ||
| private String updatedAt; | ||
|
|
||
| private String url; | ||
|
|
||
| } |
30 changes: 30 additions & 0 deletions
30
src/main/java/io/mailtrap/model/response/contactexports/ContactExportStatus.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,30 @@ | ||
| package io.mailtrap.model.response.contactexports; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonValue; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @RequiredArgsConstructor | ||
| public enum ContactExportStatus { | ||
|
|
||
| CREATED("created"), | ||
| STARTED("started"), | ||
| FINISHED("finished"); | ||
|
|
||
| private final String value; | ||
|
|
||
| @JsonValue | ||
| public String getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| @JsonCreator | ||
| public static ContactExportStatus fromValue(final String value) { | ||
| for (final ContactExportStatus status : ContactExportStatus.values()) { | ||
| if (status.value.equalsIgnoreCase(value)) { | ||
| return status; | ||
| } | ||
| } | ||
| throw new IllegalArgumentException("Unknown contact export status value: " + value); | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
src/test/java/io/mailtrap/api/contactexports/ContactExportsImplTest.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,65 @@ | ||
| package io.mailtrap.api.contactexports; | ||
|
|
||
| import io.mailtrap.Constants; | ||
| import io.mailtrap.config.MailtrapConfig; | ||
| import io.mailtrap.factory.MailtrapClientFactory; | ||
| import io.mailtrap.model.request.contactexports.ContactExportFilter; | ||
| import io.mailtrap.model.request.contactexports.ContactExportFilterOperator; | ||
| import io.mailtrap.model.request.contactexports.ContactExportFilterSubscriptionStatus; | ||
| import io.mailtrap.model.request.contactexports.CreateContactsExportRequest; | ||
| import io.mailtrap.model.response.contactexports.ContactExportResponse; | ||
| import io.mailtrap.model.response.contactexports.ContactExportStatus; | ||
| import io.mailtrap.testutils.BaseTest; | ||
| import io.mailtrap.testutils.DataMock; | ||
| import io.mailtrap.testutils.TestHttpClient; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
|
|
||
| public class ContactExportsImplTest extends BaseTest { | ||
|
|
||
| private ContactExports api; | ||
|
|
||
| @BeforeEach | ||
| public void init() { | ||
| final TestHttpClient httpClient = new TestHttpClient(List.of( | ||
| DataMock.build(Constants.GENERAL_HOST + "/api/accounts/" + accountId + "/contacts/exports", | ||
| "POST", "api/contactexports/createContactExportRequest.json", "api/contactexports/createContactExportResponse.json"), | ||
|
|
||
| DataMock.build(Constants.GENERAL_HOST + "/api/accounts/" + accountId + "/contacts/exports/" + exportId, | ||
| "GET", null, "api/contactexports/getContactExportResponse.json") | ||
| )); | ||
|
|
||
| final MailtrapConfig testConfig = new MailtrapConfig.Builder() | ||
| .httpClient(httpClient) | ||
| .token("dummy_token") | ||
| .build(); | ||
|
|
||
| api = MailtrapClientFactory.createMailtrapClient(testConfig).contactsApi().contactExports(); | ||
| } | ||
|
|
||
| @Test | ||
| void test_createContactExport_ids_filter() { | ||
| final var idsFilter = ContactExportFilter.ids(ContactExportFilterOperator.EQUAL, filterExportId); | ||
| final var subscriptionStatusFilter = ContactExportFilter.subscriptionStatus(ContactExportFilterOperator.EQUAL, ContactExportFilterSubscriptionStatus.SUBSCRIBED); | ||
|
|
||
| final var request = new CreateContactsExportRequest(List.of(idsFilter, subscriptionStatusFilter)); | ||
|
|
||
| final ContactExportResponse contactExportResponse = api.createContactExport(accountId, request); | ||
|
|
||
| assertEquals(exportId, contactExportResponse.getId()); | ||
| assertSame(ContactExportStatus.STARTED, contactExportResponse.getStatus()); | ||
| } | ||
|
|
||
| @Test | ||
| void test_getContactExport() { | ||
| final ContactExportResponse contactExport = api.getContactExport(accountId, exportId); | ||
|
|
||
| assertEquals(exportId, contactExport.getId()); | ||
| assertSame(ContactExportStatus.FINISHED, contactExport.getStatus()); | ||
| } | ||
| } |
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
16 changes: 16 additions & 0 deletions
16
src/test/resources/api/contactexports/createContactExportRequest.json
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,16 @@ | ||
| { | ||
| "filters": [ | ||
| { | ||
| "name": "list_id", | ||
| "operator": "equal", | ||
| "value": [ | ||
| 101 | ||
| ] | ||
| }, | ||
| { | ||
| "name": "subscription_status", | ||
| "operator": "equal", | ||
| "value": "subscribed" | ||
| } | ||
| ] | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/test/resources/api/contactexports/createContactExportResponse.json
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,7 @@ | ||
| { | ||
| "id": 1, | ||
| "status": "started", | ||
| "created_at": "2025-10-06T01:01:01Z", | ||
| "updated_at": "2025-10-06T01:01:01Z", | ||
| "url": null | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/test/resources/api/contactexports/getContactExportResponse.json
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,7 @@ | ||
| { | ||
| "id": 1, | ||
| "status": "finished", | ||
| "created_at": "2025-10-06T01:01:01Z", | ||
| "updated_at": "2025-10-06T01:03:21Z", | ||
| "url": null | ||
| } |
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.