Skip to content

Commit 9dfd60d

Browse files
authored
Merge pull request #34 from railsware/feature/contact-fields
Implemented Contact Fields API
2 parents ea7c63f + ee34b68 commit 9dfd60d

File tree

17 files changed

+425
-1
lines changed

17 files changed

+425
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.mailtrap.examples.contactfields;
2+
3+
import io.mailtrap.config.MailtrapConfig;
4+
import io.mailtrap.factory.MailtrapClientFactory;
5+
import io.mailtrap.model.ContactFieldDataType;
6+
import io.mailtrap.model.request.contactfields.CreateContactFieldRequest;
7+
import io.mailtrap.model.request.contactfields.UpdateContactFieldRequest;
8+
9+
import java.util.Collections;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
public class ContactFields {
14+
15+
private static final String TOKEN = "<YOUR MAILTRAP TOKEN>";
16+
private static final long ACCOUNT_ID = 1L;
17+
18+
public static void main(String[] args) {
19+
final var config = new MailtrapConfig.Builder()
20+
.token(TOKEN)
21+
.build();
22+
23+
final var client = MailtrapClientFactory.createMailtrapClient(config);
24+
25+
var createResponse = client.contactsApi().contactFields()
26+
.createContactField(ACCOUNT_ID, new CreateContactFieldRequest("Contact name", ContactFieldDataType.TEXT, "merge-tag"));
27+
28+
System.out.println(createResponse);
29+
30+
var updateResponse = client.contactsApi().contactFields()
31+
.updateContactField(ACCOUNT_ID, createResponse.getId(), new UpdateContactFieldRequest("Updated name", "updated-merge-tag"));
32+
33+
System.out.println(updateResponse);
34+
35+
var allContactFields = client.contactsApi().contactFields()
36+
.getAllContactFields(ACCOUNT_ID);
37+
38+
System.out.println(allContactFields);
39+
40+
var contactField = client.contactsApi().contactFields()
41+
.getContactField(ACCOUNT_ID, createResponse.getId());
42+
43+
System.out.println(contactField);
44+
45+
client.contactsApi().contactFields()
46+
.deleteContactField(ACCOUNT_ID, createResponse.getId());
47+
}
48+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.mailtrap.api.contactfields;
2+
3+
import io.mailtrap.model.request.contactfields.CreateContactFieldRequest;
4+
import io.mailtrap.model.request.contactfields.UpdateContactFieldRequest;
5+
import io.mailtrap.model.response.contactfields.ContactFieldResponse;
6+
7+
import java.util.List;
8+
9+
public interface ContactFields {
10+
11+
/**
12+
* Get all Contact Fields existing in your account
13+
*
14+
* @param accountId unique account ID
15+
* @return existing contact fields
16+
*/
17+
List<ContactFieldResponse> getAllContactFields(long accountId);
18+
19+
/**
20+
* Create new Contact Fields (up to 40)
21+
*
22+
* @param accountId unique account ID
23+
* @param request contact field data
24+
* @return attributes of the created contact field
25+
*/
26+
ContactFieldResponse createContactField(long accountId, CreateContactFieldRequest request);
27+
28+
/**
29+
* Get Contact Field by id
30+
*
31+
* @param accountId unique account ID
32+
* @param fieldId Unique Contact Field ID
33+
* @return attributes of the contact field
34+
*/
35+
ContactFieldResponse getContactField(long accountId, long fieldId);
36+
37+
/**
38+
* Update existing Contact Field
39+
*
40+
* @param accountId unique account ID
41+
* @param fieldId Unique Contact Field ID
42+
* @param request update data. You cannot change data_type of the field
43+
* @return attributes of the contact field
44+
*/
45+
ContactFieldResponse updateContactField(long accountId, long fieldId, UpdateContactFieldRequest request);
46+
47+
/**
48+
* Delete existing Contact Field.
49+
* You cannot delete a Contact Field which is used in Automations, Email Campaigns, and in conditions of Contact Segments
50+
*
51+
* @param accountId unique account ID
52+
* @param fieldId Unique Contact Field ID
53+
*/
54+
void deleteContactField(long accountId, long fieldId);
55+
56+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package io.mailtrap.api.contactfields;
2+
3+
import io.mailtrap.CustomValidator;
4+
import io.mailtrap.api.apiresource.ApiResourceWithValidation;
5+
import io.mailtrap.config.MailtrapConfig;
6+
import io.mailtrap.http.RequestData;
7+
import io.mailtrap.model.request.contactfields.CreateContactFieldRequest;
8+
import io.mailtrap.model.request.contactfields.UpdateContactFieldRequest;
9+
import io.mailtrap.model.response.contactfields.ContactFieldResponse;
10+
11+
import java.util.List;
12+
13+
import static io.mailtrap.Constants.GENERAL_HOST;
14+
15+
public class ContactFieldsImpl extends ApiResourceWithValidation implements ContactFields {
16+
17+
public ContactFieldsImpl(MailtrapConfig config, CustomValidator customValidator) {
18+
super(config, customValidator);
19+
this.apiHost = GENERAL_HOST;
20+
}
21+
22+
@Override
23+
public List<ContactFieldResponse> getAllContactFields(long accountId) {
24+
return httpClient.getList(
25+
String.format(apiHost + "/api/accounts/%d/contacts/fields", accountId),
26+
new RequestData(),
27+
ContactFieldResponse.class
28+
);
29+
}
30+
31+
@Override
32+
public ContactFieldResponse createContactField(long accountId, CreateContactFieldRequest request) {
33+
34+
validateRequestBodyAndThrowException(request);
35+
36+
return httpClient.post(
37+
String.format(apiHost + "/api/accounts/%d/contacts/fields", accountId),
38+
request,
39+
new RequestData(),
40+
ContactFieldResponse.class
41+
);
42+
}
43+
44+
@Override
45+
public ContactFieldResponse getContactField(long accountId, long fieldId) {
46+
return httpClient.get(
47+
String.format(apiHost + "/api/accounts/%d/contacts/fields/%d", accountId, fieldId),
48+
new RequestData(),
49+
ContactFieldResponse.class
50+
);
51+
}
52+
53+
@Override
54+
public ContactFieldResponse updateContactField(long accountId, long fieldId, UpdateContactFieldRequest request) {
55+
56+
validateRequestBodyAndThrowException(request);
57+
58+
return httpClient.patch(
59+
String.format(apiHost + "/api/accounts/%d/contacts/fields/%d", accountId, fieldId),
60+
request,
61+
new RequestData(),
62+
ContactFieldResponse.class
63+
);
64+
}
65+
66+
@Override
67+
public void deleteContactField(long accountId, long fieldId) {
68+
httpClient.delete(
69+
String.format(apiHost + "/api/accounts/%d/contacts/fields/%d", accountId, fieldId),
70+
new RequestData(),
71+
Void.class
72+
);
73+
}
74+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.mailtrap.client.api;
22

3+
import io.mailtrap.api.contactfields.ContactFields;
34
import io.mailtrap.api.contactimports.ContactImports;
45
import io.mailtrap.api.contactlists.ContactLists;
56
import io.mailtrap.api.contacts.Contacts;
@@ -17,4 +18,5 @@ public class MailtrapContactsApi {
1718
private final ContactLists contactLists;
1819
private final Contacts contacts;
1920
private final ContactImports contactImports;
21+
private final ContactFields contactFields;
2022
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.mailtrap.api.attachments.AttachmentsImpl;
77
import io.mailtrap.api.billing.BillingImpl;
88
import io.mailtrap.api.bulkemails.BulkEmailsImpl;
9+
import io.mailtrap.api.contactfields.ContactFieldsImpl;
910
import io.mailtrap.api.contactimports.ContactImportsImpl;
1011
import io.mailtrap.api.contactlists.ContactListsImpl;
1112
import io.mailtrap.api.contacts.ContactsImpl;
@@ -55,8 +56,9 @@ private static MailtrapContactsApi createContactsApi(MailtrapConfig config, Cust
5556
final var contactLists = new ContactListsImpl(config);
5657
final var contacts = new ContactsImpl(config);
5758
final var contactImports = new ContactImportsImpl(config, customValidator);
59+
final var contactFields = new ContactFieldsImpl(config, customValidator);
5860

59-
return new MailtrapContactsApi(contactLists, contacts, contactImports);
61+
return new MailtrapContactsApi(contactLists, contacts, contactImports, contactFields);
6062
}
6163

6264
private static MailtrapGeneralApi createGeneralApi(MailtrapConfig config) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.mailtrap.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
6+
public enum ContactFieldDataType {
7+
TEXT("text"),
8+
INTEGER("integer"),
9+
FLOAT("float"),
10+
BOOLEAN("boolean"),
11+
DATE("date");
12+
13+
private final String value;
14+
15+
ContactFieldDataType(String value) {
16+
this.value = value;
17+
}
18+
19+
@JsonValue
20+
public String getValue() {
21+
return value;
22+
}
23+
24+
@JsonCreator
25+
public static ContactFieldDataType fromValue(String value) {
26+
for (ContactFieldDataType type : ContactFieldDataType.values()) {
27+
if (type.value.equalsIgnoreCase(value)) {
28+
return type;
29+
}
30+
}
31+
throw new IllegalArgumentException("Unknown value: " + value);
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.mailtrap.model.request.contactfields;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import io.mailtrap.model.AbstractModel;
5+
import io.mailtrap.model.ContactFieldDataType;
6+
import jakarta.validation.constraints.Size;
7+
import lombok.AllArgsConstructor;
8+
import lombok.Getter;
9+
10+
@Getter
11+
@AllArgsConstructor
12+
public class CreateContactFieldRequest extends AbstractModel {
13+
14+
@Size(max = 80)
15+
private String name;
16+
17+
@JsonProperty("data_type")
18+
private ContactFieldDataType dataType;
19+
20+
@Size(max = 80)
21+
@JsonProperty("merge_tag")
22+
private String mergeTag;
23+
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.mailtrap.model.request.contactfields;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import io.mailtrap.model.AbstractModel;
5+
import jakarta.validation.constraints.Size;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Getter;
8+
9+
@Getter
10+
@AllArgsConstructor
11+
public class UpdateContactFieldRequest extends AbstractModel {
12+
13+
@Size(max = 80)
14+
private String name;
15+
16+
@Size(max = 80)
17+
@JsonProperty("merge_tag")
18+
private String mergeTag;
19+
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.mailtrap.model.response.contactfields;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import io.mailtrap.model.ContactFieldDataType;
5+
import lombok.Data;
6+
7+
@Data
8+
public class ContactFieldResponse {
9+
10+
private long id;
11+
12+
private String name;
13+
14+
@JsonProperty("data_type")
15+
private ContactFieldDataType dataType;
16+
17+
@JsonProperty("merge_tag")
18+
private String mergeTag;
19+
20+
}

0 commit comments

Comments
 (0)