Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ You can find the [Mailtrap Java API reference](https://mailtrap.github.io/mailtr
- [Inboxes](examples/java/io/mailtrap/examples/inbound/InboundInboxesExample.java)
- [Messages](examples/java/io/mailtrap/examples/inbound/InboundMessagesExample.java)
- [Threads](examples/java/io/mailtrap/examples/inbound/InboundThreadsExample.java)
- [Forward Rules](examples/java/io/mailtrap/examples/inbound/InboundForwardRulesExample.java)

### Bulk Sending API

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.mailtrap.examples.inbound;

import io.mailtrap.config.MailtrapConfig;
import io.mailtrap.factory.MailtrapClientFactory;
import io.mailtrap.model.InboundForwardRuleMatchType;
import io.mailtrap.model.InboundForwardRuleOperator;
import io.mailtrap.model.request.inbound.CreateInboundForwardRuleRequest;
import io.mailtrap.model.request.inbound.InboundForwardRuleConditionInput;
import io.mailtrap.model.request.inbound.UpdateInboundForwardRuleRequest;
import io.mailtrap.model.response.inbound.InboundForwardRuleDestination;

import java.util.List;

public class InboundForwardRulesExample {

private static final String TOKEN = System.getenv("MAILTRAP_API_KEY");
private static final long INBOX_ID = Long.parseLong(System.getenv("MAILTRAP_INBOUND_INBOX_ID"));

public static void main(String[] args) {
final var config = new MailtrapConfig.Builder()
.token(TOKEN)
.build();

final var client = MailtrapClientFactory.createMailtrapClient(config);
final var forwardRules = client.inboundApi().forwardRules();

// List forward rules in an inbox.
final var allRules = forwardRules.getList(INBOX_ID);
System.out.println(allRules);

// Create a forward rule.
final var created = forwardRules.create(INBOX_ID, CreateInboundForwardRuleRequest.builder()
.name("Copy billing mail to finance")
.conditions(List.of(InboundForwardRuleConditionInput.builder()
.matchType(InboundForwardRuleMatchType.SENDER)
.operator(InboundForwardRuleOperator.ENDS_WITH)
.value("@billing.example.com")
.build()))
.destinations(List.of(new InboundForwardRuleDestination("finance@example.com")))
.build());
System.out.println(created);

// Get a forward rule by ID.
final var rule = forwardRules.getById(INBOX_ID, created.getId());
System.out.println(rule);

// Update a forward rule.
final var updated = forwardRules.update(INBOX_ID, created.getId(), UpdateInboundForwardRuleRequest.builder()
.destinations(List.of(
new InboundForwardRuleDestination("finance@example.com"),
new InboundForwardRuleDestination("accounting@example.com")))
.build());
System.out.println(updated);

// Delete a forward rule.
forwardRules.delete(INBOX_ID, created.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public static void main(String[] args) {
System.out.println("Total: " + page.getTotalCount());
page.getData().forEach(thread -> System.out.println(" " + thread.getId() + " " + thread.getSubject()));

final var matching = threads.list(INBOX_ID, null, "acme");
System.out.println("Matching \"acme\": " + matching.getTotalCount());

if (page.getData().isEmpty()) {
return;
}
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/io/mailtrap/api/inbound/InboundForwardRules.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.mailtrap.api.inbound;

import io.mailtrap.model.request.inbound.CreateInboundForwardRuleRequest;
import io.mailtrap.model.request.inbound.UpdateInboundForwardRuleRequest;
import io.mailtrap.model.response.inbound.InboundForwardRule;

import java.util.List;

/**
* Interface representing the Mailtrap Inbound Email API for managing forward rules.
*/
public interface InboundForwardRules {

/**
* List forward rules in an inbox.
*
* @param inboxId the inbox ID
* @return the list of forward rules
*/
List<InboundForwardRule> getList(long inboxId);

/**
* Get a forward rule by ID.
*
* @param inboxId the inbox ID
* @param ruleId the forward rule ID
* @return the forward rule
*/
InboundForwardRule getById(long inboxId, long ruleId);

/**
* Create a new forward rule.
*
* @param inboxId the inbox ID
* @param request the create request
* @return the created forward rule
*/
InboundForwardRule create(long inboxId, CreateInboundForwardRuleRequest request);

/**
* Update a forward rule.
*
* @param inboxId the inbox ID
* @param ruleId the forward rule ID
* @param request the update request
* @return the updated forward rule
*/
InboundForwardRule update(long inboxId, long ruleId, UpdateInboundForwardRuleRequest request);

/**
* Delete a forward rule.
*
* @param inboxId the inbox ID
* @param ruleId the forward rule ID
*/
void delete(long inboxId, long ruleId);
}
69 changes: 69 additions & 0 deletions src/main/java/io/mailtrap/api/inbound/InboundForwardRulesImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.mailtrap.api.inbound;

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.inbound.CreateInboundForwardRuleRequest;
import io.mailtrap.model.request.inbound.UpdateInboundForwardRuleRequest;
import io.mailtrap.model.response.inbound.InboundForwardRule;
import io.mailtrap.model.response.inbound.InboundForwardRuleResponse;
import io.mailtrap.model.response.inbound.InboundForwardRulesListResponse;

import java.util.List;

public class InboundForwardRulesImpl extends ApiResource implements InboundForwardRules {

public InboundForwardRulesImpl(final MailtrapConfig config) {
super(config);
this.apiHost = Constants.GENERAL_HOST;
}

@Override
public List<InboundForwardRule> getList(final long inboxId) {
return httpClient.get(
String.format(apiHost + "/api/inbound/inboxes/%d/forward_rules", inboxId),
new RequestData(),
InboundForwardRulesListResponse.class
).getData();
}

@Override
public InboundForwardRule getById(final long inboxId, final long ruleId) {
return httpClient.get(
String.format(apiHost + "/api/inbound/inboxes/%d/forward_rules/%d", inboxId, ruleId),
new RequestData(),
InboundForwardRuleResponse.class
).getData();
}

@Override
public InboundForwardRule create(final long inboxId, final CreateInboundForwardRuleRequest request) {
return httpClient.post(
String.format(apiHost + "/api/inbound/inboxes/%d/forward_rules", inboxId),
request,
new RequestData(),
InboundForwardRuleResponse.class
).getData();
}

@Override
public InboundForwardRule update(final long inboxId, final long ruleId,
final UpdateInboundForwardRuleRequest request) {
return httpClient.patch(
String.format(apiHost + "/api/inbound/inboxes/%d/forward_rules/%d", inboxId, ruleId),
request,
new RequestData(),
InboundForwardRuleResponse.class
).getData();
}

@Override
public void delete(final long inboxId, final long ruleId) {
httpClient.delete(
String.format(apiHost + "/api/inbound/inboxes/%d/forward_rules/%d", inboxId, ruleId),
new RequestData(),
Void.class
);
}
}
11 changes: 11 additions & 0 deletions src/main/java/io/mailtrap/api/inbound/InboundThreads.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ public interface InboundThreads {
*/
InboundThreadsListResponse list(long inboxId, String lastId);

/**
* List conversation threads in an inbox matching a search.
*
* @param inboxId the inbox ID
* @param lastId pagination cursor from a previous response ({@code null} for
* the first page)
* @param search text to search for ({@code null} for no filter)
* @return a page of threads
*/
InboundThreadsListResponse list(long inboxId, String lastId, String search);

/**
* Get a single thread with its messages embedded (oldest first).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ public InboundThreadsImpl(final MailtrapConfig config) {

@Override
public InboundThreadsListResponse list(final long inboxId, final String lastId) {
return list(inboxId, lastId, null);
}

@Override
public InboundThreadsListResponse list(final long inboxId, final String lastId, final String search) {
final var queryParams = RequestData.buildQueryParams(
entry("last_id", Optional.ofNullable(lastId))
entry("last_id", Optional.ofNullable(lastId)),
entry("search", Optional.ofNullable(search))
);
return httpClient.get(
String.format(apiHost + "/api/inbound/inboxes/%d/threads", inboxId),
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/mailtrap/client/api/MailtrapInboundApi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.mailtrap.client.api;

import io.mailtrap.api.inbound.InboundFolders;
import io.mailtrap.api.inbound.InboundForwardRules;
import io.mailtrap.api.inbound.InboundInboxes;
import io.mailtrap.api.inbound.InboundMessages;
import io.mailtrap.api.inbound.InboundThreads;
Expand All @@ -10,7 +11,7 @@

/**
* Groups the token-scoped Inbound Email API resources (folders, inboxes,
* messages, threads).
* messages, threads, forward rules).
*/
@Getter
@Accessors(fluent = true)
Expand All @@ -24,4 +25,6 @@ public class MailtrapInboundApi {
private final InboundMessages messages;

private final InboundThreads threads;

private final InboundForwardRules forwardRules;
}
4 changes: 3 additions & 1 deletion src/main/java/io/mailtrap/factory/MailtrapClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.mailtrap.api.emailcampaigns.EmailCampaignsImpl;
import io.mailtrap.api.emailtemplates.EmailTemplatesImpl;
import io.mailtrap.api.inbound.InboundFoldersImpl;
import io.mailtrap.api.inbound.InboundForwardRulesImpl;
import io.mailtrap.api.inbound.InboundInboxesImpl;
import io.mailtrap.api.inbound.InboundMessagesImpl;
import io.mailtrap.api.inbound.InboundThreadsImpl;
Expand Down Expand Up @@ -94,8 +95,9 @@ private static MailtrapInboundApi createInboundApi(final MailtrapConfig config)
final var inboxes = new InboundInboxesImpl(config);
final var messages = new InboundMessagesImpl(config, VALIDATOR);
final var threads = new InboundThreadsImpl(config);
final var forwardRules = new InboundForwardRulesImpl(config);

return new MailtrapInboundApi(folders, inboxes, messages, threads);
return new MailtrapInboundApi(folders, inboxes, messages, threads, forwardRules);
}

private static MailtrapEmailCampaignsApi createEmailCampaignsApi(final MailtrapConfig config) {
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/io/mailtrap/model/InboundForwardRuleMatchType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.mailtrap.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

public enum InboundForwardRuleMatchType {
SENDER("sender"),
RECIPIENT("recipient"),
HEADER("header");

private final String value;

InboundForwardRuleMatchType(String value) {
this.value = value;
}

@JsonValue
public String getValue() {
return value;
}

@JsonCreator
public static InboundForwardRuleMatchType fromValue(String value) {
for (InboundForwardRuleMatchType matchType : InboundForwardRuleMatchType.values()) {
if (matchType.value.equalsIgnoreCase(value)) {
return matchType;
}
}
throw new IllegalArgumentException("Unknown InboundForwardRuleMatchType value: " + value);
}
}
35 changes: 35 additions & 0 deletions src/main/java/io/mailtrap/model/InboundForwardRuleOperator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.mailtrap.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

public enum InboundForwardRuleOperator {
EQUAL("equal"),
NOT_EQUAL("not_equal"),
CONTAINS("contains"),
STARTS_WITH("starts_with"),
ENDS_WITH("ends_with"),
EMPTY("empty"),
NOT_EMPTY("not_empty");

private final String value;

InboundForwardRuleOperator(String value) {
this.value = value;
}

@JsonValue
public String getValue() {
return value;
}

@JsonCreator
public static InboundForwardRuleOperator fromValue(String value) {
for (InboundForwardRuleOperator operator : InboundForwardRuleOperator.values()) {
if (operator.value.equalsIgnoreCase(value)) {
return operator;
}
}
throw new IllegalArgumentException("Unknown InboundForwardRuleOperator value: " + value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.mailtrap.model.request.inbound;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.mailtrap.model.AbstractModel;
import io.mailtrap.model.response.inbound.InboundForwardRuleDestination;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CreateInboundForwardRuleRequest extends AbstractModel {

private String name;

private List<InboundForwardRuleConditionInput> conditions;

private List<InboundForwardRuleDestination> destinations;
}
Loading
Loading