Skip to content

Commit 723889f

Browse files
Merge pull request #143 from mailjet/migrate-to-okhttp
updates to OkHttpClient, hides the choosing of API version to the client's internals
2 parents 3c88306 + 430d2ed commit 723889f

115 files changed

Lines changed: 1006 additions & 993 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.

README.md

Lines changed: 91 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Check out all the resources and all the Java code examples in the [Offical Docum
2020

2121
## Table of contents
2222

23+
- [Release notes](#release-notes)
2324
- [Compatibility](#compatibility)
2425
- [Installation (Maven)](#installation-maven)
2526
- [Authentication](#authentication)
@@ -46,6 +47,12 @@ Check out all the resources and all the Java code examples in the [Offical Docum
4647
- [Contribute](#contribute)
4748

4849

50+
## Release notes
51+
v5.0.0
52+
- migrated to more reliable OkHttpClient
53+
- removed ApiVersion from the MailjetClient configuration: Now the client will determine the needed API version from the resource itself.
54+
- added ClientOptions builder to make configuration more explicit. If you have troubles with migration, please, check [tests](src/test/java/com/mailjet/client/SendIT.java) for more examples.
55+
4956
## Compatibility
5057

5158
This library requires Java version 1.8 or higher.
@@ -59,7 +66,7 @@ Add the following in your `pom.xml`
5966
<dependency>
6067
<groupId>com.mailjet</groupId>
6168
<artifactId>mailjet-client</artifactId>
62-
<version>4.5.0</version>
69+
<version>5.0.0</version>
6370
</dependency>
6471
</dependencies>
6572
```
@@ -78,7 +85,14 @@ export MJ_APIKEY_PRIVATE='your API secret'
7885
Initialize your [Mailjet][mailjet] Client:
7986

8087
```java
81-
MailjetClient client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"));
88+
89+
ClientOptions options = ClientOptions.builder()
90+
.apiKey(System.getenv("MJ_APIKEY_PUBLIC"))
91+
.apiSecretKey(System.getenv("MJ_APIKEY_PRIVATE"))
92+
.build();
93+
94+
MailjetClient client = new MailjetClient(options);
95+
8296
```
8397

8498
## Make your first call
@@ -93,7 +107,6 @@ import com.mailjet.client.MailjetClient;
93107
import com.mailjet.client.MailjetRequest;
94108
import com.mailjet.client.MailjetResponse;
95109
import com.mailjet.client.ClientOptions;
96-
import com.mailjet.client.resource.ApiVersion;
97110
import com.mailjet.client.resource.Emailv31;
98111
import org.json.JSONArray;
99112
import org.json.JSONObject;
@@ -102,10 +115,16 @@ public class MyClass {
102115
* Run:
103116
*/
104117
public static void main(String[] args) throws MailjetException {
105-
MailjetClient client;
106118
MailjetRequest request;
107119
MailjetResponse response;
108-
client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"), new ClientOptions(ApiVersion.V3));
120+
121+
ClientOptions options = ClientOptions.builder()
122+
.apiKey(System.getenv("MJ_APIKEY_PUBLIC"))
123+
.apiSecretKey(System.getenv("MJ_APIKEY_PRIVATE"))
124+
.build();
125+
126+
MailjetClient client = new MailjetClient(options);
127+
109128
request = new MailjetRequest(Emailv31.resource)
110129
.property(Emailv31.MESSAGES, new JSONArray()
111130
.put(new JSONObject()
@@ -131,15 +150,40 @@ public class MyClass {
131150
To instantiate the library you can use the following constructor:
132151

133152
```java
134-
MailjetClient client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"), new ClientOptions(ApiVersion.V3, "https://api.us.mailjet.com"));
135-
```
153+
ClientOptions options = ClientOptions.builder()
154+
.apiKey(System.getenv("MJ_APIKEY_PUBLIC"))
155+
.apiSecretKey(System.getenv("MJ_APIKEY_PRIVATE"))
156+
.build();
136157

137-
- `$MJ_APIKEY_PUBLIC` : public Mailjet API key
138-
- `$MJ_APIKEY_PRIVATE` : private Mailjet API key
139-
- `ClientOptions` : associative array describing the connection options (see Options bellow for full list)
158+
MailjetClient client = new MailjetClient(options);
159+
```
140160

141161
### Options
142162

163+
#### Set connection timeouts and log requests
164+
165+
You can pass a custom configured HttpClient to the Mailjet client to get request logs or custom timeouts:
166+
167+
```java
168+
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
169+
logging.setLevel(Level.BASIC);
170+
OkHttpClient customHttpClient = new OkHttpClient.Builder()
171+
.connectTimeout(60, TimeUnit.SECONDS)
172+
.readTimeout(60, TimeUnit.SECONDS)
173+
.writeTimeout(60, TimeUnit.SECONDS)
174+
.addInterceptor(logging)
175+
.build();
176+
177+
ClientOptions options = ClientOptions.builder()
178+
.apiKey(System.getenv("MJ_APIKEY_PUBLIC"))
179+
.apiSecretKey(System.getenv("MJ_APIKEY_PRIVATE"))
180+
.okHttpClient(customHttpClient)
181+
.build();
182+
183+
```
184+
185+
for more configuration options, please, refer to the [OkHttpClient documentation](https://square.github.io/okhttp/)
186+
143187
#### API Versioning
144188

145189
The Mailjet API is spread among three distinct versions:
@@ -148,13 +192,8 @@ The Mailjet API is spread among three distinct versions:
148192
- `v3.1` - Email Send API v3.1, which is the latest version of our Send API
149193
- `v4` - SMS API
150194

151-
Since most Email API endpoints are located under `v3`, it is set as the default one and does not need to be specified when making your request. For the others you need to specify the version using `ClientOptions`. For example, if using Send API `v3.1`:
195+
You can skip version specification during the request, as MailJet client will determine the needed API version by himself.
152196

153-
``` java
154-
155-
MailjetClient client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"), new ClientOptions(ApiVersion.V3_1));
156-
157-
```
158197

159198
For additional information refer to our [API Reference](https://dev.preprod.mailjet.com/reference/overview/versioning/).
160199

@@ -163,7 +202,13 @@ For additional information refer to our [API Reference](https://dev.preprod.mail
163202
The default base domain name for the Mailjet API is api.mailjet.com. You can modify this base URL by adding a different URL in `ClientOptions`:
164203

165204
```java
166-
MailjetClient client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"), new ClientOptions(ApiVersion.V3, "https://api.us.mailjet.com"));
205+
ClientOptions options = ClientOptions.builder()
206+
.baseUrl("https://api.us.mailjet.com")
207+
.apiKey(System.getenv("MJ_APIKEY_PUBLIC"))
208+
.apiSecretKey(System.getenv("MJ_APIKEY_PRIVATE"))
209+
.build();
210+
211+
MailjetClient client = new MailjetClient(options);
167212
```
168213

169214
If your account has been moved to Mailjet's **US architecture**, the URL you need to add is `https://api.us.mailjet.com`.
@@ -218,7 +263,7 @@ public class MyClass {
218263
MailjetClient client;
219264
MailjetRequest request;
220265
MailjetResponse response;
221-
client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"));
266+
client = new MailjetClient(ClientOptions.builder().apiKey(System.getenv("MJ_APIKEY_PUBLIC")).apiSecretKey(System.getenv("MJ_APIKEY_PRIVATE")).build());
222267
request = new MailjetRequest(Contact.resource)
223268
.property(Contact.EMAIL, "Mister@mailjet.com");
224269
response = client.post(request);
@@ -251,7 +296,7 @@ public class MyClass {
251296
MailjetClient client;
252297
MailjetRequest request;
253298
MailjetResponse response;
254-
client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"));
299+
client = new MailjetClient(ClientOptions.builder().apiKey(System.getenv("MJ_APIKEY_PUBLIC")).apiSecretKey(System.getenv("MJ_APIKEY_PRIVATE")).build());
255300
request = new MailjetRequest(ContactManagecontactslists.resource, ID)
256301
.property(ContactManagecontactslists.CONTACTSLISTS, new JSONArray()
257302
.put(new JSONObject()
@@ -292,7 +337,8 @@ public class MyClass {
292337
MailjetClient client;
293338
MailjetRequest request;
294339
MailjetResponse response;
295-
client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"));
340+
client = new MailjetClient(ClientOptions.builder().apiKey(System.getenv("MJ_APIKEY_PUBLIC")).apiSecretKey(System.getenv("MJ_APIKEY_PRIVATE")).build());
341+
296342
request = new MailjetRequest(Contact.resource);
297343
response = client.get(request);
298344
System.out.println(response.getStatus());
@@ -324,7 +370,8 @@ public class MyClass {
324370
MailjetClient client;
325371
MailjetRequest request;
326372
MailjetResponse response;
327-
client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"));
373+
client = new MailjetClient(ClientOptions.builder().apiKey(System.getenv("MJ_APIKEY_PUBLIC")).apiSecretKey(System.getenv("MJ_APIKEY_PRIVATE")).build());
374+
328375
request = new MailjetRequest(Contact.resource)
329376
.filter(Contact.ISEXCLUDEDFROMCAMPAIGNS, "false");
330377
response = client.get(request);
@@ -358,7 +405,8 @@ public class MyClass {
358405
MailjetClient client;
359406
MailjetRequest request;
360407
MailjetResponse response;
361-
client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"));
408+
client = new MailjetClient(ClientOptions.builder().apiKey(System.getenv("MJ_APIKEY_PUBLIC")).apiSecretKey(System.getenv("MJ_APIKEY_PRIVATE")).build());
409+
362410
request = new MailjetRequest(Contact.resource, ID);
363411
response = client.get(request);
364412
System.out.println(response.getStatus());
@@ -392,7 +440,8 @@ public class MyClass {
392440
MailjetClient client;
393441
MailjetRequest request;
394442
MailjetResponse response;
395-
client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"));
443+
client = new MailjetClient(ClientOptions.builder().apiKey(System.getenv("MJ_APIKEY_PUBLIC")).apiSecretKey(System.getenv("MJ_APIKEY_PRIVATE")).build());
444+
396445
request = new MailjetRequest(Contactdata.resource, ID)
397446
.property(Contactdata.DATA, new JSONArray()
398447
.put(new JSONObject()
@@ -432,7 +481,8 @@ public class MyClass {
432481
MailjetClient client;
433482
MailjetRequest request;
434483
MailjetResponse response;
435-
client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"));
484+
client = new MailjetClient(ClientOptions.builder().apiKey(System.getenv("MJ_APIKEY_PUBLIC")).apiSecretKey(System.getenv("MJ_APIKEY_PRIVATE")).build());
485+
436486
request = new MailjetRequest(Template.resource, ID);
437487
response = client.delete(request);
438488
System.out.println(response.getStatus());
@@ -445,63 +495,39 @@ public class MyClass {
445495

446496
### Token Authentication
447497

448-
Authentication for the SMS API endpoints is done using a bearer token. The bearer token is generated in the [SMS section](https://app.mailjet.com/sms) of your Mailjet account.
449-
450-
```java
451-
MailjetClient client = new MailjetClient(System.getenv("MJ_TOKEN"), new ClientOptions(ApiVersion.V4));
452-
```
498+
Authentication for the SMS API endpoints is done using a bearer token. The bearer token generated in the [SMS section](https://app.mailjet.com/sms) of your Mailjet account.
453499

454500
### Example request
455501

456-
Here's an example SMS API request:
502+
An example SMS API request:
457503

458504
``` java
459505

460506
MailjetClient client;
461507
MailjetRequest request;
462508
MailjetResponse response;
463509

464-
// Note how we set the version to v4 using ClientOptions and use an already generated token
465-
MailjetClient client = new MailjetClient(System.getenv("MJ_TOKEN"), new ClientOptions(ApiVersion.V4));
510+
MailjetClient mailjetClient = new MailjetClient(ClientOptions
511+
.builder()
512+
.bearerAccessToken(System.getenv("MJ_APITOKEN"))
513+
.build());
466514

467-
request = new MailjetRequest(Send.resource)
468-
.property(Send.From, "MJPilot")
469-
.property(Send.To, "+33600000000")
470-
.property(Send.Text, "Have a nice SMS flight with Mailjet!");
515+
String germanyPhoneNumber = "+4915207831169";
471516

472-
response = client.post(request);
517+
MailjetRequest mailjetRequest = new MailjetRequest(SmsSend.resource)
518+
.property(SmsSend.FROM, "MJPilot")
519+
.property(SmsSend.TO, germanyPhoneNumber)
520+
.property(SmsSend.TEXT, "Have a nice SMS flight with Mailjet!");
473521

474-
package com.my.project;
475-
import com.mailjet.client.errors.MailjetException;
476-
import com.mailjet.client.errors.MailjetSocketTimeoutException;
477-
import com.mailjet.client.MailjetClient;
478-
import com.mailjet.client.MailjetRequest;
479-
import com.mailjet.client.MailjetResponse;
480-
import com.mailjet.client.ClientOptions;
481-
import com.mailjet.client.resource.sms.SmsSend;
482-
import org.json.JSONArray;
483-
import org.json.JSONObject;
484-
public class MyClass {
485-
/**
486-
* Run:
487-
*/
488-
public static void main(String[] args) throws MailjetException {
489-
MailjetClient client;
490-
MailjetRequest request;
491-
MailjetResponse response;
492-
client = new MailjetClient(System.getenv("MJ_TOKEN"), new ClientOptions(ApiVersion.V4));
493-
request = new MailjetRequest(SmsSend.resource)
494-
.property(SmsSend.FROM, "MJPilot")
495-
.property(SmsSend.TO, "+33600000000")
496-
.property(SmsSend.TEXT, "Have a nice SMS flight with Mailjet!")
497-
response = client.post(request);
498-
System.out.println(response.getStatus());
499-
System.out.println(response.getData());
500-
}
501-
}
522+
// send the request
523+
MailjetResponse response = mailjetClient.post(mailjetRequest);
502524

525+
// assert response
526+
Assert.assertEquals(200, response.getStatus());
527+
Assert.assertEquals("Message is being sent", response.getData().getJSONObject(0).getJSONObject("Status").getString("Description"));
503528

504529
```
530+
Also, you can check [integration tests](src/test/java/com/mailjet/client/SendSmsIT.java) how to work with Mailjet client.
505531

506532
## Contribute
507533

pom.xml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<!--General configuration -->
77
<groupId>com.mailjet</groupId>
88
<artifactId>mailjet-client</artifactId>
9-
<version>4.6.0</version>
9+
<version>5.0.0</version>
1010
<packaging>jar</packaging>
1111
<name>Mailjet Client</name>
1212
<description>A Mailjet API Client</description>
@@ -27,7 +27,7 @@
2727
<connection>scm:git:https://github.com/mailjet/mailjet-apiv3-java.git</connection>
2828
<developerConnection>scm:git:https://github.com/mailjet/mailjet-apiv3-java.git</developerConnection>
2929
<url>git@github.com:mailjet/mailjet-apiv3-java.git</url>
30-
<tag>mailjet-client-4.6.0</tag>
30+
<tag>mailjet-client-5.0.0</tag>
3131
</scm>
3232
<!--License-->
3333
<licenses>
@@ -53,16 +53,23 @@
5353
</properties>
5454
<!--Dependencies-->
5555
<dependencies>
56+
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
5657
<dependency>
57-
<groupId>com.turbomanage.basic-http-client</groupId>
58-
<artifactId>http-client-java</artifactId>
59-
<version>0.89</version>
58+
<groupId>com.squareup.okhttp3</groupId>
59+
<artifactId>okhttp</artifactId>
60+
<version>4.9.0</version>
6061
</dependency>
6162
<dependency>
6263
<groupId>org.json</groupId>
6364
<artifactId>json</artifactId>
6465
<version>20190722</version>
6566
</dependency>
67+
<dependency>
68+
<groupId>org.projectlombok</groupId>
69+
<artifactId>lombok</artifactId>
70+
<version>1.18.16</version>
71+
<scope>provided</scope>
72+
</dependency>
6673
<dependency>
6774
<groupId>junit</groupId>
6875
<artifactId>junit</artifactId>
@@ -75,6 +82,14 @@
7582
<version>1.10.19</version>
7683
<scope>test</scope>
7784
</dependency>
85+
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/mockwebserver -->
86+
<dependency>
87+
<groupId>com.squareup.okhttp3</groupId>
88+
<artifactId>mockwebserver</artifactId>
89+
<version>4.9.0</version>
90+
<scope>test</scope>
91+
</dependency>
92+
7893
</dependencies>
7994
<!--Build Steps-->
8095
<build>

0 commit comments

Comments
 (0)