Skip to content

Commit 328bcb6

Browse files
authored
Merge pull request #51 from mailtrap/MT-21864-add-missing-endpoints
Add missing endpoints
2 parents e8a7ffc + 940bf3a commit 328bcb6

62 files changed

Lines changed: 1550 additions & 3 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.envrc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
export GPG_TTY=$(tty)
22
export JAVA_TOOL_OPTIONS='-Duser.language=en -Duser.country=US'
3+
export MAILTRAP_ACCOUNT_ID="op://Mailtrap Dev/Mailtrap SDK Dev API Key/account_id"
4+
export MAILTRAP_ORGANIZATION_ID="op://Mailtrap Dev/Mailtrap SDK Dev API Key/organization_id"
5+
export MAILTRAP_API_KEY="op://Mailtrap Dev/Mailtrap SDK Dev API Key/account_api_token"
6+
export MAILTRAP_ORGANIZATION_API_KEY="op://Mailtrap Dev/Mailtrap SDK Dev API Key/organization_api_token"

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ build/
3636

3737
### Mac OS ###
3838
.DS_Store
39+
.idea/

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,12 @@ You can find the [Mailtrap Java API reference](https://mailtrap.github.io/mailtr
332332
- [Accounts](examples/java/io/mailtrap/examples/general/AccountsExample.java)
333333
- [Permissions](examples/java/io/mailtrap/examples/general/PermissionsExample.java)
334334
- [Billing](examples/java/io/mailtrap/examples/general/BillingExample.java)
335+
- [API Tokens](examples/java/io/mailtrap/examples/general/ApiTokensExample.java)
336+
- [Webhooks](examples/java/io/mailtrap/examples/webhooks/WebhooksExample.java)
337+
338+
### Organizations API
339+
340+
- [Sub-Accounts](examples/java/io/mailtrap/examples/organizations/SubAccountsExample.java)
335341

336342
### Sending API
337343

‎examples/java/io/mailtrap/examples/contacts/ContactsExample.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public static void main(String[] args) {
3535

3636
System.out.println(createResponse);
3737

38+
final var getResponse = client.contactsApi().contacts()
39+
.getContact(ACCOUNT_ID, EMAIL);
40+
41+
System.out.println(getResponse);
42+
3843
final var updateRequest = new UpdateContactRequest(
3944
new UpdateContact(EMAIL, Map.of("first_name", "Nick"), List.of(LIST_1_ID, LIST_2_ID),
4045
Collections.emptyList(), UNSUBSCRIBED));
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.mailtrap.examples.general;
2+
3+
import io.mailtrap.config.MailtrapConfig;
4+
import io.mailtrap.factory.MailtrapClientFactory;
5+
import io.mailtrap.model.AccessLevel;
6+
import io.mailtrap.model.ResourceType;
7+
import io.mailtrap.model.request.apitokens.ApiTokenResource;
8+
import io.mailtrap.model.request.apitokens.CreateApiTokenRequest;
9+
10+
import java.util.List;
11+
12+
public class ApiTokensExample {
13+
14+
private static final String TOKEN = "<YOUR MAILTRAP TOKEN>";
15+
private static final long ACCOUNT_ID = 1L;
16+
17+
public static void main(String[] args) {
18+
final var config = new MailtrapConfig.Builder()
19+
.token(TOKEN)
20+
.build();
21+
22+
final var client = MailtrapClientFactory.createMailtrapClient(config);
23+
24+
// The full token value is returned only on creation — store it securely.
25+
final var createRequest = new CreateApiTokenRequest(
26+
"My token",
27+
List.of(new ApiTokenResource(ResourceType.ACCOUNT, ACCOUNT_ID, AccessLevel.VIEWER)));
28+
29+
final var createdToken = client.generalApi().apiTokens()
30+
.createApiToken(ACCOUNT_ID, createRequest);
31+
System.out.println(createdToken);
32+
33+
final var tokenId = createdToken.getId();
34+
35+
final var allTokens = client.generalApi().apiTokens().getAllApiTokens(ACCOUNT_ID);
36+
System.out.println(allTokens);
37+
38+
final var token = client.generalApi().apiTokens().getApiToken(ACCOUNT_ID, tokenId);
39+
System.out.println(token);
40+
41+
// Reset expires the existing token and returns a new one with the same permissions.
42+
// The new token value is only returned here.
43+
final var resetToken = client.generalApi().apiTokens().resetApiToken(ACCOUNT_ID, tokenId);
44+
System.out.println(resetToken);
45+
46+
client.generalApi().apiTokens().deleteApiToken(ACCOUNT_ID, resetToken.getId());
47+
}
48+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.mailtrap.examples.organizations;
2+
3+
import io.mailtrap.config.MailtrapConfig;
4+
import io.mailtrap.factory.MailtrapClientFactory;
5+
import io.mailtrap.model.request.subaccounts.CreateSubAccountRequest;
6+
import io.mailtrap.model.request.subaccounts.SubAccountInput;
7+
8+
public class SubAccountsExample {
9+
10+
private static final String TOKEN = "<YOUR MAILTRAP TOKEN>";
11+
private static final long ORGANIZATION_ID = 1L;
12+
private static final String SUB_ACCOUNT_NAME = "Acme Marketing";
13+
14+
public static void main(String[] args) {
15+
final var config = new MailtrapConfig.Builder()
16+
.token(TOKEN)
17+
.build();
18+
19+
final var client = MailtrapClientFactory.createMailtrapClient(config);
20+
21+
// Requires sub account management permissions for the organization.
22+
final var allSubAccounts = client.organizationsApi().subAccounts().getSubAccounts(ORGANIZATION_ID);
23+
System.out.println(allSubAccounts);
24+
25+
final var createRequest = new CreateSubAccountRequest(new SubAccountInput(SUB_ACCOUNT_NAME));
26+
final var createdSubAccount = client.organizationsApi().subAccounts()
27+
.createSubAccount(ORGANIZATION_ID, createRequest);
28+
System.out.println(createdSubAccount);
29+
}
30+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.mailtrap.examples.webhooks;
2+
3+
import io.mailtrap.config.MailtrapConfig;
4+
import io.mailtrap.factory.MailtrapClientFactory;
5+
import io.mailtrap.model.SendingStream;
6+
import io.mailtrap.model.WebhookEventType;
7+
import io.mailtrap.model.WebhookPayloadFormat;
8+
import io.mailtrap.model.WebhookType;
9+
import io.mailtrap.model.request.webhooks.CreateWebhookRequest;
10+
import io.mailtrap.model.request.webhooks.UpdateWebhookRequest;
11+
import io.mailtrap.model.request.webhooks.WebhookInput;
12+
13+
import java.util.List;
14+
15+
public class WebhooksExample {
16+
17+
private static final String TOKEN = "<YOUR MAILTRAP TOKEN>";
18+
private static final long ACCOUNT_ID = 1L;
19+
private static final String WEBHOOK_URL = "https://example.com/webhooks/mailtrap";
20+
private static final String UPDATED_WEBHOOK_URL = "https://example.com/webhooks/mailtrap/v2";
21+
22+
public static void main(String[] args) {
23+
final var config = new MailtrapConfig.Builder()
24+
.token(TOKEN)
25+
.build();
26+
27+
final var client = MailtrapClientFactory.createMailtrapClient(config);
28+
29+
final var createRequest = new CreateWebhookRequest(
30+
WebhookInput.builder()
31+
.url(WEBHOOK_URL)
32+
.webhookType(WebhookType.EMAIL_SENDING)
33+
.active(true)
34+
.payloadFormat(WebhookPayloadFormat.JSON)
35+
.sendingStream(SendingStream.TRANSACTIONAL)
36+
.eventTypes(List.of(
37+
WebhookEventType.DELIVERY,
38+
WebhookEventType.BOUNCE,
39+
WebhookEventType.OPEN,
40+
WebhookEventType.CLICK,
41+
WebhookEventType.UNSUBSCRIBE))
42+
.build());
43+
44+
// `signing_secret` is only returned on creation — store it securely.
45+
final var createResponse = client.generalApi().webhooks()
46+
.createWebhook(ACCOUNT_ID, createRequest);
47+
System.out.println(createResponse);
48+
49+
final var webhookId = createResponse.getData().getId();
50+
51+
final var allWebhooks = client.generalApi().webhooks().getAllWebhooks(ACCOUNT_ID);
52+
System.out.println(allWebhooks);
53+
54+
final var webhook = client.generalApi().webhooks().getWebhook(ACCOUNT_ID, webhookId);
55+
System.out.println(webhook);
56+
57+
final var updateRequest = new UpdateWebhookRequest(
58+
WebhookInput.builder()
59+
.url(UPDATED_WEBHOOK_URL)
60+
.active(false)
61+
.eventTypes(List.of(WebhookEventType.DELIVERY, WebhookEventType.BOUNCE))
62+
.build());
63+
64+
final var updateResponse = client.generalApi().webhooks()
65+
.updateWebhook(ACCOUNT_ID, webhookId, updateRequest);
66+
System.out.println(updateResponse);
67+
68+
final var deleteResponse = client.generalApi().webhooks().deleteWebhook(ACCOUNT_ID, webhookId);
69+
System.out.println(deleteResponse);
70+
}
71+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.mailtrap.api.apitokens;
2+
3+
import io.mailtrap.model.request.apitokens.CreateApiTokenRequest;
4+
import io.mailtrap.model.response.apitokens.ApiToken;
5+
import io.mailtrap.model.response.apitokens.ApiTokenWithToken;
6+
7+
import java.util.List;
8+
9+
public interface ApiTokens {
10+
11+
/**
12+
* List all API tokens visible to the current API token.
13+
*
14+
* @param accountId unique account ID
15+
* @return list of API tokens
16+
*/
17+
List<ApiToken> getAllApiTokens(long accountId);
18+
19+
/**
20+
* Create a new API token for the account with the given name and resource permissions.
21+
* The full token value is returned only on creation.
22+
*
23+
* @param accountId unique account ID
24+
* @param request token name and resource permissions
25+
* @return created token, including the full token value
26+
*/
27+
ApiTokenWithToken createApiToken(long accountId, CreateApiTokenRequest request);
28+
29+
/**
30+
* Get a single API token by id.
31+
*
32+
* @param accountId unique account ID
33+
* @param id API token ID
34+
* @return API token
35+
*/
36+
ApiToken getApiToken(long accountId, long id);
37+
38+
/**
39+
* Permanently delete an API token.
40+
*
41+
* @param accountId unique account ID
42+
* @param id API token ID
43+
*/
44+
void deleteApiToken(long accountId, long id);
45+
46+
/**
47+
* Reset an API token. Expires the requested token and creates a new one with the same
48+
* permissions; the new token value is returned only once.
49+
*
50+
* @param accountId unique account ID
51+
* @param id API token ID
52+
* @return new token, including the full token value
53+
*/
54+
ApiTokenWithToken resetApiToken(long accountId, long id);
55+
56+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.mailtrap.api.apitokens;
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.AbstractModel;
8+
import io.mailtrap.model.request.apitokens.CreateApiTokenRequest;
9+
import io.mailtrap.model.response.apitokens.ApiToken;
10+
import io.mailtrap.model.response.apitokens.ApiTokenWithToken;
11+
12+
import java.util.List;
13+
14+
public class ApiTokensImpl extends ApiResource implements ApiTokens {
15+
16+
public ApiTokensImpl(final MailtrapConfig config) {
17+
super(config);
18+
this.apiHost = Constants.GENERAL_HOST;
19+
}
20+
21+
@Override
22+
public List<ApiToken> getAllApiTokens(final long accountId) {
23+
return httpClient.getList(
24+
String.format(apiHost + "/api/accounts/%d/api_tokens", accountId),
25+
new RequestData(),
26+
ApiToken.class
27+
);
28+
}
29+
30+
@Override
31+
public ApiTokenWithToken createApiToken(final long accountId, final CreateApiTokenRequest request) {
32+
return httpClient.post(
33+
String.format(apiHost + "/api/accounts/%d/api_tokens", accountId),
34+
request,
35+
new RequestData(),
36+
ApiTokenWithToken.class
37+
);
38+
}
39+
40+
@Override
41+
public ApiToken getApiToken(final long accountId, final long id) {
42+
return httpClient.get(
43+
String.format(apiHost + "/api/accounts/%d/api_tokens/%d", accountId, id),
44+
new RequestData(),
45+
ApiToken.class
46+
);
47+
}
48+
49+
@Override
50+
public void deleteApiToken(final long accountId, final long id) {
51+
httpClient.delete(
52+
String.format(apiHost + "/api/accounts/%d/api_tokens/%d", accountId, id),
53+
new RequestData(),
54+
Void.class
55+
);
56+
}
57+
58+
@Override
59+
public ApiTokenWithToken resetApiToken(final long accountId, final long id) {
60+
return httpClient.post(
61+
String.format(apiHost + "/api/accounts/%d/api_tokens/%d/reset", accountId, id),
62+
(AbstractModel) null,
63+
new RequestData(),
64+
ApiTokenWithToken.class
65+
);
66+
}
67+
}

‎src/main/java/io/mailtrap/api/contacts/Contacts.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.mailtrap.model.request.contacts.CreateContactRequest;
44
import io.mailtrap.model.request.contacts.UpdateContactRequest;
55
import io.mailtrap.model.response.contacts.CreateContactResponse;
6+
import io.mailtrap.model.response.contacts.GetContactResponse;
67
import io.mailtrap.model.response.contacts.UpdateContactResponse;
78

89
public interface Contacts {
@@ -16,6 +17,15 @@ public interface Contacts {
1617
*/
1718
CreateContactResponse createContact(long accountId, CreateContactRequest request);
1819

20+
/**
21+
* Get contact using id or email (URL encoded)
22+
*
23+
* @param accountId unique account ID
24+
* @param idOrEmail contact ID or Email
25+
* @return contact details
26+
*/
27+
GetContactResponse getContact(long accountId, String idOrEmail);
28+
1929
/**
2030
* Delete contact using id or email (URL encoded)
2131
*

0 commit comments

Comments
 (0)