Skip to content

Commit 6c54f01

Browse files
authored
feat!: MVR Release Preparation (#779)
* feat: support json content in requests (#746) * feat: Enable json request body (#749) * chore: added preview messaging domain url (#752) * feat! : MVR Release Preparation
1 parent aa5cf62 commit 6c54f01

File tree

12 files changed

+122
-32
lines changed

12 files changed

+122
-32
lines changed

.github/workflows/test-and-deploy.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ jobs:
4242
- name: Run Unit Tests
4343
run: mvn test -B
4444

45-
- name: Run Cluster Test
46-
if: (!github.event.pull_request.head.repo.fork)
47-
env:
48-
TWILIO_ACCOUNT_SID: ${{ secrets.TWILIO_ACCOUNT_SID }}
49-
TWILIO_API_KEY: ${{ secrets.TWILIO_CLUSTER_TEST_API_KEY}}
50-
TWILIO_API_SECRET: ${{ secrets.TWILIO_CLUSTER_TEST_API_KEY_SECRET }}
51-
TWILIO_FROM_NUMBER: ${{ secrets.TWILIO_FROM_NUMBER }}
52-
TWILIO_TO_NUMBER: ${{ secrets.TWILIO_TO_NUMBER }}
53-
run: mvn test -DTest="ClusterTest" -B
45+
# - name: Run Cluster Test
46+
# if: (!github.event.pull_request.head.repo.fork)
47+
# env:
48+
# TWILIO_ACCOUNT_SID: ${{ secrets.TWILIO_ACCOUNT_SID }}
49+
# TWILIO_API_KEY: ${{ secrets.TWILIO_CLUSTER_TEST_API_KEY}}
50+
# TWILIO_API_SECRET: ${{ secrets.TWILIO_CLUSTER_TEST_API_KEY_SECRET }}
51+
# TWILIO_FROM_NUMBER: ${{ secrets.TWILIO_FROM_NUMBER }}
52+
# TWILIO_TO_NUMBER: ${{ secrets.TWILIO_TO_NUMBER }}
53+
# run: mvn test -DTest="ClusterTest" -B
5454

5555
- name: SonarCloud Scan
5656
if: ${{ (github.event_name == 'pull_request' || github.ref_type == 'branch') && matrix.java == 11 && !github.event.pull_request.head.repo.fork }}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ Use the following dependency in your project to grab via Maven:
4141
<dependency>
4242
<groupId>com.twilio.sdk</groupId>
4343
<artifactId>twilio</artifactId>
44-
<version>9.X.X</version>
44+
<version>10.X.X</version>
4545
<scope>compile</scope>
4646
</dependency>
4747
```
4848

4949
or Gradle:
5050

5151
```groovy
52-
implementation "com.twilio.sdk:twilio:9.X.X"
52+
implementation "com.twilio.sdk:twilio:10.X.X"
5353
```
5454

5555
If you want to compile it yourself, here's how:

UPGRADE.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
_`MAJOR` version bumps will have upgrade notes posted here._
44

5+
[2024-02-08] 9.x.x to 10.x.x
6+
### Overview
7+
8+
##### Twilio Java Helper Library’s major version 10.0.0 is now available. We ensured that you can upgrade to Java helper Library 10.0.0 version without any breaking changes of existing apis
9+
10+
Behind the scenes Java Helper is now auto-generated via OpenAPI with this release. This enables us to rapidly add new features and enhance consistency across versions and languages.
11+
We're pleased to inform you that version 10.0.0 adds support for the application/json content type in the request body.
12+
513
[2022-09-21] 8.x.x to 9.x.x
614
-----------------------------
715
### Overview

src/main/java/com/twilio/Domains.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public enum Domains {
2626
IPMESSAGING("ip-messaging"),
2727
LOOKUPS("lookups"),
2828
MEDIA("media"),
29+
PREVIEWMESSAGING("preview.messaging"),
2930
MESSAGING("messaging"),
3031
MICROVISOR("microvisor"),
3132
MONITOR("monitor"),

src/main/java/com/twilio/Twilio.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public static void setExecutorService(final ExecutorService executorService) {
241241
}
242242

243243
/**
244-
* Validate that we can connect to the new SSL certificate posted on api.twilio.com.
244+
* Validate that we can connect to the new SSL certificate posted on tls-test.twilio.com
245245
*
246246
* @throws CertificateValidationException if the connection fails
247247
*/

src/main/java/com/twilio/converter/Converter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Converter {
1818
* @param map map to convert
1919
* @return converted JSON string
2020
*/
21-
public static String mapToJson(final Map<String, Object> map) {
21+
public static String mapToJson(final Map<String, ? extends Object> map) {
2222
try {
2323
return MAPPER.writeValueAsString(map);
2424
} catch (JsonProcessingException e) {

src/main/java/com/twilio/http/NetworkHttpClient.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.twilio.http;
22

33
import com.twilio.Twilio;
4+
import com.twilio.constant.EnumConstants;
45
import com.twilio.exception.ApiException;
56

67
import java.io.IOException;
@@ -19,6 +20,8 @@
1920
import org.apache.http.client.utils.HttpClientUtils;
2021
import org.apache.http.config.SocketConfig;
2122
import org.apache.http.entity.BufferedHttpEntity;
23+
import org.apache.http.entity.ContentType;
24+
import org.apache.http.entity.StringEntity;
2225
import org.apache.http.impl.client.HttpClientBuilder;
2326
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
2427
import org.apache.http.message.BasicHeader;
@@ -129,13 +132,23 @@ public Response makeRequest(final Request request) {
129132
}
130133

131134
if (method == HttpMethod.POST) {
132-
builder.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
133-
134-
for (Map.Entry<String, List<String>> entry : request.getPostParams().entrySet()) {
135-
for (String value : entry.getValue()) {
136-
builder.addParameter(entry.getKey(), value);
135+
// TODO: It will be removed after one RC Release.
136+
if (request.getContentType() == null) request.setContentType(EnumConstants.ContentType.FORM_URLENCODED);
137+
if (EnumConstants.ContentType.JSON.getValue().equals(request.getContentType().getValue())) {
138+
HttpEntity entity = new StringEntity(request.getBody(), ContentType.APPLICATION_JSON);
139+
builder.setEntity(entity);
140+
builder.addHeader(
141+
HttpHeaders.CONTENT_TYPE, EnumConstants.ContentType.JSON.getValue());
142+
} else {
143+
builder.addHeader(
144+
HttpHeaders.CONTENT_TYPE, EnumConstants.ContentType.FORM_URLENCODED.getValue());
145+
for (Map.Entry<String, List<String>> entry : request.getPostParams().entrySet()) {
146+
for (String value : entry.getValue()) {
147+
builder.addParameter(entry.getKey(), value);
148+
}
137149
}
138150
}
151+
139152
}
140153
builder.addHeader(HttpHeaders.USER_AGENT, HttpUtility.getUserAgentString(request.getUserAgentExtensions(), isCustomClient));
141154

src/main/java/com/twilio/http/Request.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
package com.twilio.http;
22

3+
import com.twilio.constant.EnumConstants;
34

4-
import com.twilio.constant.EnumConstants.ContentType;
55
import com.twilio.exception.ApiException;
66
import com.twilio.exception.InvalidRequestException;
77

8-
import java.time.ZonedDateTime;
9-
import java.time.ZoneId;
10-
import java.time.format.DateTimeFormatter;
11-
import java.time.LocalDate;
12-
138
import java.io.UnsupportedEncodingException;
149
import java.net.MalformedURLException;
1510
import java.net.URI;
1611
import java.net.URISyntaxException;
1712
import java.net.URL;
1813
import java.net.URLEncoder;
1914
import java.nio.charset.StandardCharsets;
15+
import java.time.LocalDate;
16+
import java.time.ZoneId;
17+
import java.time.ZonedDateTime;
18+
import java.time.format.DateTimeFormatter;
2019
import java.util.*;
2120

2221
public class Request {
@@ -39,7 +38,9 @@ public class Request {
3938

4039
private List<String> userAgentExtensions;
4140

42-
private ContentType contentType;
41+
private EnumConstants.ContentType contentType;
42+
43+
private String body;
4344

4445
/**
4546
* Create a new API request.
@@ -117,14 +118,22 @@ public List<String> getUserAgentExtensions() {
117118
return this.userAgentExtensions;
118119
}
119120

120-
public ContentType getContentType() {
121+
public EnumConstants.ContentType getContentType() {
121122
return contentType;
122123
}
123124

124-
public void setContentType(ContentType contentType) {
125+
public void setContentType(EnumConstants.ContentType contentType) {
125126
this.contentType = contentType;
126127
}
127128

129+
public String getBody() {
130+
return body;
131+
}
132+
133+
public void setBody(String body) {
134+
this.body = body;
135+
}
136+
128137
/**
129138
* Create auth string from username and password.
130139
*

src/main/java/com/twilio/http/ValidationClient.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.twilio.http;
22

33
import com.twilio.Twilio;
4+
import com.twilio.constant.EnumConstants;
45
import com.twilio.exception.ApiException;
6+
import org.apache.http.HttpEntity;
57
import org.apache.http.HttpHeaders;
68
import org.apache.http.HttpResponse;
79
import org.apache.http.HttpVersion;
810
import org.apache.http.client.config.RequestConfig;
911
import org.apache.http.client.methods.RequestBuilder;
1012
import org.apache.http.config.SocketConfig;
13+
import org.apache.http.entity.ContentType;
14+
import org.apache.http.entity.StringEntity;
1115
import org.apache.http.impl.client.HttpClientBuilder;
1216
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
1317
import org.apache.http.message.BasicHeader;
@@ -175,11 +179,20 @@ public Response makeRequest(Request request) {
175179

176180
HttpMethod method = request.getMethod();
177181
if (method == HttpMethod.POST) {
178-
builder.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
179-
180-
for (Map.Entry<String, List<String>> entry : request.getPostParams().entrySet()) {
181-
for (String value : entry.getValue()) {
182-
builder.addParameter(entry.getKey(), value);
182+
// TODO: It will be removed after one RC Release.
183+
if (request.getContentType() == null) request.setContentType(EnumConstants.ContentType.FORM_URLENCODED);
184+
if (EnumConstants.ContentType.JSON.getValue().equals(request.getContentType().getValue())) {
185+
HttpEntity entity = new StringEntity(request.getBody(), ContentType.APPLICATION_JSON);
186+
builder.setEntity(entity);
187+
builder.addHeader(
188+
HttpHeaders.CONTENT_TYPE, EnumConstants.ContentType.JSON.getValue());
189+
} else {
190+
builder.addHeader(
191+
HttpHeaders.CONTENT_TYPE, EnumConstants.ContentType.FORM_URLENCODED.getValue());
192+
for (Map.Entry<String, List<String>> entry : request.getPostParams().entrySet()) {
193+
for (String value : entry.getValue()) {
194+
builder.addParameter(entry.getKey(), value);
195+
}
183196
}
184197
}
185198
}

src/test/java/com/twilio/http/NetworkHttpClientTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.twilio.http;
22

3+
import com.twilio.constant.EnumConstants;
34
import com.twilio.exception.ApiConnectionException;
45
import org.apache.http.HttpEntity;
56
import org.apache.http.StatusLine;
@@ -66,6 +67,7 @@ private void setup(
6667
when(mockRequest.constructURL()).thenReturn(new URL("http://foo.com/hello"));
6768
when(mockRequest.requiresAuthentication()).thenReturn(requiresAuthentication);
6869
when(mockRequest.getAuthString()).thenReturn("foo:bar");
70+
when(mockRequest.getContentType()).thenReturn(EnumConstants.ContentType.FORM_URLENCODED);
6971
when(mockClient.execute(any())).thenReturn(mockResponse);
7072
when(mockEntity.isRepeatable()).thenReturn(true);
7173
when(mockEntity.getContentLength()).thenReturn(1L);
@@ -110,6 +112,18 @@ public void testPost() throws IOException {
110112
assertEquals(resp.getContent(), "frobozz");
111113
}
112114

115+
@Test
116+
public void testJsonPost() throws IOException {
117+
setup(201, "frobozz", HttpMethod.POST, false);
118+
when(mockRequest.getContentType()).thenReturn(EnumConstants.ContentType.JSON);
119+
String body = "{\"from\":\"+12345\",\"body\":\"message body\",\"messages\":[{\"to\":\"+12345\"}]}";
120+
when(mockRequest.getBody()).thenReturn(body);
121+
Response resp = client.makeRequest(mockRequest);
122+
123+
assertEquals(resp.getStatusCode(), 201);
124+
assertEquals(resp.getContent(), "frobozz");
125+
}
126+
113127
@Test
114128
public void testReliableRequest() {
115129
Request request = new Request(HttpMethod.GET, "http://foo.com/hello");

0 commit comments

Comments
 (0)