@@ -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 ()) {
0 commit comments