Skip to content

Commit 517da45

Browse files
authored
Merge pull request #62 from TeleSign/feature/fix_application_json_support
Fix application/json support for PhoneID.
2 parents 83493e7 + f6da9df commit 517da45

File tree

6 files changed

+271
-34
lines changed

6 files changed

+271
-34
lines changed

RELEASE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [2.2.2]((http://central.maven.org/maven2/com/telesign/telesign/2.2.2/) - 2018-05-25
2+
- 2019-05-20
3+
- Fixed support for application/json content-type
4+
15
## [2.2.1](http://central.maven.org/maven2/com/telesign/telesign/2.2.1/) - 2018-03-09
26
- 2018-03-09
37
- Added support for application/json content-type

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
group 'com.telesign'
6-
version '2.2.1'
6+
version '2.2.2'
77

88
apply plugin: 'idea'
99
apply plugin: 'java'

src/main/java/com/telesign/PhoneIdClient.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
import java.io.UnsupportedEncodingException;
55
import java.net.Proxy;
66
import java.security.GeneralSecurityException;
7+
import java.util.HashMap;
78
import java.util.Map;
89

10+
import com.google.gson.JsonObject;
11+
import com.google.gson.reflect.TypeToken;
912
import okhttp3.MediaType;
1013
import okhttp3.RequestBody;
11-
import com.google.gson.Gson;;
14+
import com.google.gson.Gson;
1215

1316
/**
1417
* A set of APIs that deliver deep phone number data attributes that help optimize the end user
@@ -49,17 +52,33 @@ public PhoneIdClient(String customerId,
4952
*/
5053
public TelesignResponse phoneid(String phoneNumber, Map<String, ? extends Object> params) throws IOException, GeneralSecurityException {
5154

52-
return this.post(String.format(PHONEID_RESOURCE, phoneNumber), params);
53-
}
54-
55-
@Override
56-
public RequestBody createRequestBody(Map<String, ? extends Object> params) throws UnsupportedEncodingException {
57-
String jsonString = (new Gson()).toJson(params);
58-
return params != null ? RequestBody.create(PhoneIdClient.JSON, jsonString.getBytes()) : RequestBody.create(PhoneIdClient.JSON, "");
59-
}
60-
61-
@Override
62-
protected String getContentType() {
63-
return "application/json";
55+
return this.post(String.format(PHONEID_RESOURCE, phoneNumber), params, JSON_CONTENT_TYPE);
56+
}
57+
58+
@Override
59+
public RequestBody createRequestBody(Map<String, ? extends Object> params, String contentType) throws UnsupportedEncodingException, IOException {
60+
if (contentType.equals(JSON_CONTENT_TYPE)) {
61+
Gson gson = new Gson();
62+
JsonObject jsonObject = new JsonObject();
63+
HashMap<String, Object> paramsCopy = new HashMap<>(params);
64+
65+
if (paramsCopy.containsKey("addons")) {
66+
jsonObject.add("addons", gson.toJsonTree(
67+
paramsCopy.remove("addons"),
68+
new TypeToken<HashMap<String, HashMap<String, String>>>() {
69+
}.getType()));
70+
}
71+
72+
JsonObject paramsObject = gson.toJsonTree(params, new TypeToken<HashMap<String, String>>() {
73+
}.getType()).getAsJsonObject();
74+
for (String key : paramsCopy.keySet()) {
75+
jsonObject.add(key, paramsObject.get(key));
76+
}
77+
78+
String jsonString = jsonObject.toString();
79+
return params != null ? RequestBody.create(com.telesign.PhoneIdClient.JSON, jsonString.getBytes()) : RequestBody.create(com.telesign.PhoneIdClient.JSON, "");
80+
} else {
81+
return super.createRequestBody(params, contentType);
82+
}
6483
}
6584
}

src/main/java/com/telesign/RestClient.java

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public class RestClient {
3030
private static final String userAgent = String.format("TeleSignSDK/java-%s Java/%s %s", BuildConfig.VERSION,
3131
System.getProperty("java.version"), Version.userAgent());
3232

33+
public static final String URL_FORM_ENCODED_CONTENT_TYPE = "application/x-www-form-urlencoded";
34+
public static final String JSON_CONTENT_TYPE = "application/json";
35+
3336
private String customerId;
3437
private String apiKey;
3538
private String restEndpoint;
@@ -171,7 +174,7 @@ public TelesignResponse(Response okHttpResponse) {
171174
* be one of 'POST', 'GET', 'PUT' or 'DELETE'.
172175
* @param resource
173176
* The partial resource URI to perform the request against.
174-
* @param urlEncodedFields
177+
* @param requestParams
175178
* URL encoded HTTP body to perform the HTTP request with.
176179
* @param dateRfc2616
177180
* (optional) The date and time of the request formatted in rfc 2616.
@@ -247,14 +250,19 @@ public static Map<String, String> generateTelesignHeaders(String customerId, Str
247250

248251
return headers;
249252
}
250-
253+
251254
/**
252-
* Returns specific content type for request execution execution
253-
*
254-
* @return
255+
* Generic TeleSign REST API POST handler.
256+
*
257+
* @param resource
258+
* The partial resource URI to perform the request against.
259+
* @param params
260+
* Params to perform the POST request with.
261+
* @return The TelesignResponse for the request.
255262
*/
256-
protected String getContentType() {
257-
return "application/x-www-form-urlencoded";
263+
public TelesignResponse post(String resource, Map<String, ? extends Object> params)
264+
throws IOException, GeneralSecurityException {
265+
return this.execute("POST", resource, params, URL_FORM_ENCODED_CONTENT_TYPE);
258266
}
259267

260268
/**
@@ -266,10 +274,10 @@ protected String getContentType() {
266274
* Params to perform the POST request with.
267275
* @return The TelesignResponse for the request.
268276
*/
269-
public TelesignResponse post(String resource, Map<String, ? extends Object> params)
277+
public TelesignResponse post(String resource, Map<String, ? extends Object> params, String contentType)
270278
throws IOException, GeneralSecurityException {
271279

272-
return this.execute("POST", resource, params);
280+
return this.execute("POST", resource, params, contentType);
273281
}
274282

275283
/**
@@ -299,7 +307,7 @@ public TelesignResponse get(String resource, Map<String, String> params)
299307
public TelesignResponse put(String resource, Map<String, String> params)
300308
throws IOException, GeneralSecurityException {
301309

302-
return this.execute("PUT", resource, params);
310+
return this.execute("PUT", resource, params, URL_FORM_ENCODED_CONTENT_TYPE);
303311
}
304312

305313
/**
@@ -320,17 +328,13 @@ public TelesignResponse delete(String resource, Map<String, String> params)
320328
/**
321329
* Generic TeleSign REST API request handler.
322330
*
323-
* @param methodName
324-
* The HTTP method name, as an upper case string.
325-
* @param resource
326-
* The partial resource URI to perform the request against.
327331
* @param params
328332
* Params to perform the request with.
329-
* @return The TelesignResponse for the request.
333+
* @return The RequestBody for the request.
330334
* @throws IOException
331335
*/
332336

333-
public RequestBody createRequestBody(Map<String, ? extends Object> params) throws IOException {
337+
public RequestBody createRequestBody(Map<String, ? extends Object> params, String contentType) throws IOException {
334338
FormBody.Builder formBodyBuilder = new FormBody.Builder();
335339
for (Map.Entry<String, ? extends Object> entry : params.entrySet()) {
336340
formBodyBuilder.add(entry.getKey(), (String) entry.getValue());
@@ -340,10 +344,10 @@ public RequestBody createRequestBody(Map<String, ? extends Object> params) throw
340344
return formBody;
341345

342346
}
343-
347+
344348
/**
345349
* Generic TeleSign method for request execution,
346-
*
350+
*
347351
* @param methodName
348352
* @param resource
349353
* @param params
@@ -353,6 +357,21 @@ public RequestBody createRequestBody(Map<String, ? extends Object> params) throw
353357
*/
354358
private TelesignResponse execute(String methodName, String resource, Map<String, ? extends Object> params)
355359
throws IOException, GeneralSecurityException {
360+
return execute(methodName, resource, params, "");
361+
}
362+
363+
/**
364+
* Generic TeleSign method for request execution,
365+
*
366+
* @param methodName
367+
* @param resource
368+
* @param params
369+
* @return The TelesignResponse for the request
370+
* @throws IOException
371+
* @throws GeneralSecurityException
372+
*/
373+
private TelesignResponse execute(String methodName, String resource, Map<String, ? extends Object> params, String contentType)
374+
throws IOException, GeneralSecurityException {
356375

357376
if (params == null) {
358377
params = new HashMap<>();
@@ -363,7 +382,7 @@ private TelesignResponse execute(String methodName, String resource, Map<String,
363382
RequestBody requestBody = null;
364383
String requestParams = "";
365384
if (methodName.equals("POST") || methodName.equals("PUT")) {
366-
requestBody = this.createRequestBody(params);
385+
requestBody = this.createRequestBody(params, contentType);
367386
if (requestBody != null) {
368387
Buffer buffer = new Buffer();
369388
requestBody.writeTo(buffer);
@@ -378,7 +397,7 @@ private TelesignResponse execute(String methodName, String resource, Map<String,
378397
}
379398

380399
Map<String, String> headers = RestClient.generateTelesignHeaders(this.customerId, this.apiKey,
381-
methodName, resource, requestParams, null, null, RestClient.userAgent, this.getContentType());
400+
methodName, resource, requestParams, null, null, RestClient.userAgent, contentType);
382401

383402
Request.Builder requestBuilder = new Request.Builder().url(httpUrl).method(methodName, requestBody);
384403
for (Map.Entry<String, String> entry : headers.entrySet()) {
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.telesign;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonObject;
7+
import com.google.gson.reflect.TypeToken;
8+
import junit.framework.TestCase;
9+
import okhttp3.RequestBody;
10+
import okio.Buffer;
11+
12+
import java.io.IOException;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
16+
public class JsonCreateRequestBodyTest extends TestCase {
17+
18+
private void makeRequestBodyAndAssert(HashMap<String, ? extends Object> params) {
19+
PhoneIdClient phoneIdClient = new PhoneIdClient("", "");
20+
Buffer actualJsonBuffer = new Buffer();
21+
Gson gson = new Gson();
22+
23+
try {
24+
RequestBody rb = phoneIdClient.createRequestBody(params, RestClient.JSON_CONTENT_TYPE);
25+
rb.writeTo(actualJsonBuffer);
26+
} catch (IOException exc) {
27+
fail(exc.getMessage());
28+
}
29+
HashMap<String, Object> unserializedJson = gson.fromJson(actualJsonBuffer.readUtf8(),
30+
new TypeToken<HashMap<String, Object>>() {}.getType());
31+
assertEquals(params, unserializedJson);
32+
}
33+
34+
public void testEmptyParams() {
35+
makeRequestBodyAndAssert(new HashMap<String, Object>());
36+
}
37+
38+
public void testNoAddonsParams() {
39+
HashMap<String, String> params = new HashMap<String, String>() {{
40+
put("originating_ip", "127.0.0.1");
41+
put("bogus", "param");
42+
}};
43+
makeRequestBodyAndAssert(params);
44+
}
45+
46+
public void testAddonParamOnly() {
47+
HashMap<String, Object> params = new HashMap<String, Object>() {{
48+
put("addons", new HashMap<String, Object>() {{
49+
put("contact", new HashMap<>());
50+
put("contact_plus", new HashMap<String, String>(){{
51+
put("billing_postal_code", "90210");
52+
}});
53+
put("contact_match", new HashMap<String, String>() {{
54+
put("first_name", "Bob");
55+
put("last_name", "Smith");
56+
put("address", "12345 Some St");
57+
put("city", "Los Angeles");
58+
put("postal_code", "90210");
59+
put("state", "CA");
60+
put("country", "USA");
61+
}});
62+
put("current_location", new HashMap<>());
63+
put("current_location_plus", new HashMap<>());
64+
put("device_info", new HashMap<>());
65+
put("number_deactivation", new HashMap<>());
66+
put("subscriber_status", new HashMap<>());
67+
}});
68+
}};
69+
makeRequestBodyAndAssert(params);
70+
}
71+
public void testAddonAndRegularParams() {
72+
HashMap<String, Object> params = new HashMap<String, Object>() {{
73+
put("originating_ip", "127.0.0.1");
74+
put("account_lifecycle_event", "create");
75+
put("addons", new HashMap<String, Object>() {{
76+
put("contact", new HashMap<>());
77+
put("contact_plus", new HashMap<String, String>(){{
78+
put("billing_postal_code", "90210");
79+
}});
80+
put("contact_match", new HashMap<String, String>() {{
81+
put("first_name", "Bob");
82+
put("last_name", "Smith");
83+
put("address", "12345 Some St");
84+
put("city", "Los Angeles");
85+
put("postal_code", "90210");
86+
put("state", "CA");
87+
put("country", "USA");
88+
}});
89+
put("current_location", new HashMap<>());
90+
put("current_location_plus", new HashMap<>());
91+
put("device_info", new HashMap<>());
92+
put("number_deactivation", new HashMap<>());
93+
put("subscriber_status", new HashMap<>());
94+
}});
95+
}};
96+
makeRequestBodyAndAssert(params);
97+
}
98+
}

0 commit comments

Comments
 (0)