Skip to content

Commit a8cbe8d

Browse files
authored
Merge pull request #23 from railsware/feature/contacts-update-endpoint
Implemented update contact PATCH endpoint
2 parents 3b775e2 + 6a92b44 commit a8cbe8d

28 files changed

+333
-88
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ build/
3535
.vscode/
3636

3737
### Mac OS ###
38-
.DS_Store
38+
.DS_Store

LICENSE.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2024 Railsware Products Studio LLC
3+
Copyright (c) 2025 Railsware Products Studio LLC
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+18-15
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,31 @@ Quickly add email sending functionality to your Java application with Mailtrap.
88

99
## Java Version
1010

11-
Requires JDK 11 or higher
11+
Requires JDK 11 or higher.
1212

13-
## Usage
14-
15-
### Dependency
13+
## Install package
1614

17-
Maven dependency
15+
As Maven dependency:
1816

1917
```xml
2018

2119
<dependency>
2220
<groupId>io.mailtrap</groupId>
2321
<artifactId>mailtrap-java</artifactId>
24-
<version>0.0.1</version>
22+
<version>1.0.0</version>
2523
</dependency>
2624
```
2725

28-
Gradle Groovy dependency
26+
As Gradle Groovy dependency:
2927

3028
```groovy
31-
implementation 'io.mailtrap:mailtrap-java:0.0.1'
29+
implementation 'io.mailtrap:mailtrap-java:1.0.0'
3230
```
3331

34-
Gradle Kotlin DSL dependency
32+
As Gradle Kotlin DSL dependency:
3533

3634
```kotlin
37-
implementation("io.mailtrap:mailtrap-java:0.0.1")
35+
implementation("io.mailtrap:mailtrap-java:1.0.0")
3836
```
3937

4038
## Usage
@@ -72,14 +70,14 @@ public class MailtrapJavaSDKTest {
7270
.text("Welcome to Mailtrap Sending!")
7371
.build();
7472

73+
// Send email using Mailtrap Sending API
7574
try {
7675
System.out.println(client.send(mail));
7776
} catch (Exception e) {
7877
System.out.println("Caught exception : " + e);
7978
}
8079

81-
// OR send email to the Mailtrap Sandbox
82-
80+
// Or send email using Mailtrap Testing API
8381
try {
8482
long inboxId = 1000001L;
8583

@@ -98,7 +96,7 @@ public class MailtrapJavaSDKTest {
9896

9997
System.out.println(client.send(mail));
10098

101-
// Or use directly Testing API to send email to Sandbox
99+
// Or use Testing API directly
102100
System.out.println(client.testingApi().emails().send(mail, inboxId));
103101
} catch (Exception e) {
104102
System.out.println("Caught exception : " + e);
@@ -127,18 +125,23 @@ You can find the API reference [here](https://railsware.github.io/mailtrap-java/
127125
- [Error handling](examples/java/io/mailtrap/examples/sending/Errors.java)
128126
- [Send email using template](examples/java/io/mailtrap/examples/sending/Template.java)
129127

130-
### Email testing API
128+
### Email Testing API
131129

132130
- [Attachments](examples/java/io/mailtrap/examples/testing/Attachments.java)
133131
- [Inboxes](examples/java/io/mailtrap/examples/testing/Inboxes.java)
134132
- [Messages](examples/java/io/mailtrap/examples/testing/Messages.java)
135133
- [Projects](examples/java/io/mailtrap/examples/testing/Projects.java)
136134
- [Send mail using template](examples/java/io/mailtrap/examples/testing/Email.java)
137135

138-
### Bulk sending API
136+
### Bulk Sending API
139137

140138
- [Send mail](examples/java/io/mailtrap/examples/bulk/BulkSend.java)
141139

140+
### Contacts API
141+
142+
- [Contacts](examples/java/io/mailtrap/examples/contacts/Contacts.java)
143+
- [Contact lists](examples/java/io/mailtrap/examples/contact_lists/ContactLists.java)
144+
142145
## Contributing
143146

144147
Bug reports and pull requests are welcome on [GitHub](https://github.com/railsware/mailtrap-java). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md).

docs/docfx.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
],
1616
"dest": "_site"
1717
}
18-
}
18+
}

docs/getting-started.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,39 @@ Follow these few simple steps to add Mailtrap API functionality into your Java p
44

55
# Prerequisites
66

7-
Make sure your project uses JDK 11 or higher
7+
Make sure your project uses JDK 11 or higher.
88

99
## Setup
1010

1111
### 1. Add the library as a Maven/Gradle dependency
12-
Maven dependency
12+
13+
Maven dependency:
1314

1415
```xml
1516

1617
<dependency>
1718
<groupId>io.mailtrap</groupId>
1819
<artifactId>mailtrap-java</artifactId>
19-
<version>*latest-version*</version>
20+
<version>1.0.0</version>
2021
</dependency>
2122
```
2223

23-
Gradle Groovy dependency
24+
Gradle Groovy dependency:
2425

2526
```groovy
26-
implementation 'io.mailtrap:mailtrap-java:*latest-version*'
27+
implementation 'io.mailtrap:mailtrap-java:1.0.0'
2728
```
2829

29-
Gradle Kotlin DSL dependency
30+
Gradle Kotlin DSL dependency:
3031

3132
```kotlin
32-
implementation("io.mailtrap:mailtrap-java:*latest-version*")
33+
implementation("io.mailtrap:mailtrap-java:1.0.0")
3334
```
3435

35-
### 2. Register/login into Mailtrap account
36-
Obtain API token to configure and use MailtrapClient
36+
### 2. Authentication
37+
38+
- Register/login into Mailtrap
39+
- Obtain an API token [with respective permissions and access level](https://help.mailtrap.io/article/103-api-tokens) to configure and use MailtrapClient.
3740

3841
### 3. Configuration and usage
3942

@@ -73,4 +76,4 @@ public class Minimal {
7376
}
7477
}
7578
}
76-
```
79+
```

docs/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ Welcome to the documentation portal for the official <a href="https://mailtrap.i
77

88
This client allows you to quickly and easily integrate your Java application with Mailtrap API.
99

10-
1110
## Table of contents
1211

1312
- [Getting Started](getting-started.md) - quick overview on how to integrate Mailtrap into your project
1413
- [Usage Examples](usage-examples.md)
1514
- [API Reference](javadocs.md) - Detailed API Reference
1615

1716
## License
18-
Licensed under the <a href="https://github.com/railsware/mailtrap-java/blob/main/LICENSE.txt" target="_blank">MIT License</a>.
17+
18+
Licensed under the <a href="https://github.com/railsware/mailtrap-java/blob/main/LICENSE.txt" target="_blank">MIT License</a>.

docs/toc.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
- name: Usage Examples
66
href: usage-examples.md
77
- name: API Reference
8-
href: javadocs.md
8+
href: javadocs.md

docs/usage-examples.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Detailed Usage Examples
22

3-
Code examples, that covers all possible APIs can be found <a href="https://github.com/railsware/mailtrap-java/tree/main/examples/java/io/mailtrap/examples" target="_blank">here</a>
3+
Code examples covering all APIs can be found <a href="https://github.com/railsware/mailtrap-java/tree/main/examples/java/io/mailtrap/examples" target="_blank">here</a>.

examples/java/io/mailtrap/examples/contacts/Contacts.java

+21-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import io.mailtrap.config.MailtrapConfig;
44
import io.mailtrap.factory.MailtrapClientFactory;
5-
import io.mailtrap.model.request.contacts.Contact;
5+
import io.mailtrap.model.request.contacts.CreateContact;
66
import io.mailtrap.model.request.contacts.CreateContactRequest;
7+
import io.mailtrap.model.request.contacts.UpdateContact;
8+
import io.mailtrap.model.request.contacts.UpdateContactRequest;
79

10+
import java.util.Collections;
811
import java.util.List;
912
import java.util.Map;
1013

@@ -15,19 +18,31 @@ public class Contacts {
1518
private static final long LIST_1_ID = 1L;
1619
private static final long LIST_2_ID = 2L;
1720
private static final String EMAIL = "[email protected]";
21+
private static final boolean UNSUBSCRIBED = false;
1822

1923
public static void main(String[] args) {
2024
final var config = new MailtrapConfig.Builder()
21-
.token(TOKEN)
22-
.build();
25+
.token(TOKEN)
26+
.build();
2327

2428
final var client = MailtrapClientFactory.createMailtrapClient(config);
2529

26-
var request = new CreateContactRequest(new Contact(EMAIL, Map.of("first_name", "Nick"), List.of(LIST_1_ID, LIST_2_ID)));
30+
var createRequest = new CreateContactRequest(
31+
new CreateContact(EMAIL, Map.of("first_name", "Nick"), List.of(LIST_1_ID, LIST_2_ID)));
2732

28-
var response = client.contactsApi().contacts().createContact(ACCOUNT_ID, request);
33+
var createResponse = client.contactsApi().contacts()
34+
.createContact(ACCOUNT_ID, createRequest);
2935

30-
System.out.println(response);
36+
System.out.println(createResponse);
37+
38+
var updateRequest = new UpdateContactRequest(
39+
new UpdateContact(EMAIL, Map.of("first_name", "Nick"), List.of(LIST_1_ID, LIST_2_ID),
40+
Collections.emptyList(), UNSUBSCRIBED));
41+
42+
var updateResponse = client.contactsApi().contacts()
43+
.updateContact(ACCOUNT_ID, EMAIL, updateRequest);
44+
45+
System.out.println(updateResponse);
3146

3247
client.contactsApi().contacts().deleteContact(ACCOUNT_ID, EMAIL);
3348
}

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.mailtrap</groupId>
88
<artifactId>mailtrap-java</artifactId>
9-
<version>0.0.1</version>
9+
<version>1.0.0</version>
1010

1111
<name>Mailtrap Java SDK</name>
1212
<description>The official Mailtrap SDK for Java provides Java APIs
@@ -236,4 +236,4 @@
236236
</plugins>
237237
</build>
238238

239-
</project>
239+
</project>
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
package io.mailtrap.api.contacts;
22

33
import io.mailtrap.model.request.contacts.CreateContactRequest;
4+
import io.mailtrap.model.request.contacts.UpdateContactRequest;
45
import io.mailtrap.model.response.contacts.CreateContactResponse;
6+
import io.mailtrap.model.response.contacts.UpdateContactResponse;
57

68
public interface Contacts {
79

8-
/**
9-
* Create a new contact
10-
*
11-
* @param accountId unique account ID
12-
* @param request required contact data
13-
* @return created contact
14-
*/
15-
CreateContactResponse createContact(long accountId, CreateContactRequest request);
10+
/**
11+
* Create a new contact
12+
*
13+
* @param accountId unique account ID
14+
* @param request required contact data
15+
* @return created contact
16+
*/
17+
CreateContactResponse createContact(long accountId, CreateContactRequest request);
1618

17-
/**
18-
* Delete contact using id or email (URL encoded)
19-
*
20-
* @param accountId unique account ID
21-
* @param idOrEmail contact ID or Email
22-
*/
23-
void deleteContact(long accountId, String idOrEmail);
19+
/**
20+
* Delete contact using id or email (URL encoded)
21+
*
22+
* @param accountId unique account ID
23+
* @param idOrEmail contact ID or Email
24+
*/
25+
void deleteContact(long accountId, String idOrEmail);
26+
27+
/**
28+
* Update contact using id or email (URL encoded)
29+
*
30+
* @param accountId unique account ID
31+
* @param idOrEmail contact ID or Email
32+
* @param request request body
33+
*/
34+
UpdateContactResponse updateContact(long accountId, String idOrEmail,
35+
UpdateContactRequest request);
2436

2537
}

src/main/java/io/mailtrap/api/contacts/ContactsImpl.java

+36-23
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,48 @@
55
import io.mailtrap.config.MailtrapConfig;
66
import io.mailtrap.http.RequestData;
77
import io.mailtrap.model.request.contacts.CreateContactRequest;
8+
import io.mailtrap.model.request.contacts.UpdateContactRequest;
89
import io.mailtrap.model.response.contacts.CreateContactResponse;
9-
10+
import io.mailtrap.model.response.contacts.UpdateContactResponse;
1011
import java.net.URLEncoder;
1112
import java.nio.charset.Charset;
12-
import java.util.Base64;
1313

1414
public class ContactsImpl extends ApiResource implements Contacts {
1515

16-
public ContactsImpl(MailtrapConfig config) {
17-
super(config);
18-
this.apiHost = Constants.GENERAL_HOST;
19-
}
16+
public ContactsImpl(MailtrapConfig config) {
17+
super(config);
18+
this.apiHost = Constants.GENERAL_HOST;
19+
}
20+
21+
@Override
22+
public CreateContactResponse createContact(long accountId, CreateContactRequest request) {
23+
return httpClient.post(
24+
String.format(apiHost + "/api/accounts/%s/contacts", accountId),
25+
request,
26+
new RequestData(),
27+
CreateContactResponse.class
28+
);
29+
}
2030

21-
@Override
22-
public CreateContactResponse createContact(long accountId, CreateContactRequest request) {
23-
return httpClient.post(
24-
String.format(apiHost + "/api/accounts/%s/contacts", accountId),
25-
request,
26-
new RequestData(),
27-
CreateContactResponse.class
28-
);
29-
}
31+
@Override
32+
public void deleteContact(long accountId, String idOrEmail) {
33+
httpClient.delete(
34+
String.format(apiHost + "/api/accounts/%s/contacts/%s", accountId,
35+
URLEncoder.encode(idOrEmail, Charset.defaultCharset())),
36+
new RequestData(),
37+
Void.class
38+
);
39+
}
3040

31-
@Override
32-
public void deleteContact(long accountId, String idOrEmail) {
33-
httpClient.delete(
34-
String.format(apiHost + "/api/accounts/%s/contacts/%s", accountId, URLEncoder.encode(idOrEmail, Charset.defaultCharset())),
35-
new RequestData(),
36-
Void.class
37-
);
38-
}
41+
@Override
42+
public UpdateContactResponse updateContact(long accountId, String idOrEmail,
43+
UpdateContactRequest request) {
44+
return httpClient.patch(
45+
String.format(apiHost + "/api/accounts/%s/contacts/%s", accountId,
46+
URLEncoder.encode(idOrEmail, Charset.defaultCharset())),
47+
request,
48+
new RequestData(),
49+
UpdateContactResponse.class
50+
);
51+
}
3952
}

0 commit comments

Comments
 (0)