16
16
17
17
package com .mailjet .client ;
18
18
19
+ import static com .mailjet .client .MailjetResponseUtil .validateMailjetResponse ;
20
+
21
+ import java .io .IOException ;
22
+ import java .io .UnsupportedEncodingException ;
23
+ import java .net .HttpURLConnection ;
24
+ import java .net .MalformedURLException ;
25
+
26
+ import org .json .JSONObject ;
27
+
19
28
import com .mailjet .client .errors .MailjetException ;
20
29
import com .mailjet .client .errors .MailjetSocketTimeoutException ;
21
30
import com .turbomanage .httpclient .BasicHttpClient ;
22
31
import com .turbomanage .httpclient .BasicRequestHandler ;
23
32
import com .turbomanage .httpclient .ConsoleRequestLogger ;
24
33
import com .turbomanage .httpclient .HttpResponse ;
25
34
import com .turbomanage .httpclient .ParameterMap ;
26
- import com .turbomanage .httpclient .RequestLogger ;
27
35
import com .turbomanage .httpclient .RequestHandler ;
28
- import java .io .IOException ;
29
- import java .io .UnsupportedEncodingException ;
30
- import java .net .HttpURLConnection ;
31
- import java .net .MalformedURLException ;
32
- import org .json .JSONObject ;
33
- import com .mailjet .client .ClientOptions ;
36
+ import com .turbomanage .httpclient .RequestLogger ;
34
37
35
38
/**
36
39
*
@@ -237,9 +240,7 @@ public MailjetResponse get(MailjetRequest request) throws MailjetException, Mail
237
240
p .putAll (request ._filters );
238
241
HttpResponse response = _client .get (url , p );
239
242
240
- if (response == null ) {
241
- throw new MailjetSocketTimeoutException ("Socket Timeout" );
242
- }
243
+ validateMailjetResponse (response );
243
244
244
245
String json = (response .getBodyAsString () != null && !(response .getBodyAsString ().equals ("" )) ?
245
246
response .getBodyAsString () : new JSONObject ().put ("status" , response .getStatus ()).toString ());
@@ -278,9 +279,7 @@ public MailjetResponse post(MailjetRequest request) throws MailjetException, Mai
278
279
279
280
response = _client .post (url , request .getContentType (), request .getBody ().getBytes ("UTF8" ));
280
281
281
- if (response == null ) {
282
- throw new MailjetSocketTimeoutException ("Socket Timeout" );
283
- }
282
+ validateMailjetResponse (response );
284
283
285
284
json = (response .getBodyAsString () != null && !(response .getBodyAsString ().equals ("" )) ?
286
285
response .getBodyAsString () : new JSONObject ().put ("status" , response .getStatus ()).toString ());
@@ -307,11 +306,12 @@ public MailjetResponse put(MailjetRequest request) throws MailjetException, Mail
307
306
308
307
response = _client .put (url , request .getContentType (), request .getBody ().getBytes ("UTF8" ));
309
308
310
- if (response == null ) {
311
- throw new MailjetSocketTimeoutException ("Socket Timeout" );
312
- }
309
+ validateMailjetResponse (response );
310
+
311
+ String json = (response .getBodyAsString () != null && !response .getBodyAsString ().trim ().equals ("" ) ?
312
+ response .getBodyAsString () : new JSONObject ().put ("status" , response .getStatus ()).toString ());
313
313
314
- return new MailjetResponse (response .getStatus (), new JSONObject (response . getBodyAsString () ));
314
+ return new MailjetResponse (response .getStatus (), new JSONObject (json ));
315
315
} catch (MalformedURLException ex ) {
316
316
throw new MailjetException ("Internal Exception: Malformed Url" );
317
317
} catch (UnsupportedEncodingException ex ) {
@@ -336,12 +336,11 @@ public MailjetResponse delete(MailjetRequest request) throws MailjetException, M
336
336
p .putAll (request ._filters );
337
337
response = _client .delete (url , p );
338
338
339
- if (response == null ) {
340
- throw new MailjetSocketTimeoutException ("Socket Timeout" );
341
- }
342
-
339
+ validateMailjetResponse (response );
340
+
343
341
json = (response .getBodyAsString () != null && !response .getBodyAsString ().trim ().equals ("" ) ?
344
342
response .getBodyAsString () : new JSONObject ().put ("status" , response .getStatus ()).toString ());
343
+
345
344
return new MailjetResponse (response .getStatus (), new JSONObject (json ));
346
345
} catch (MalformedURLException ex ) {
347
346
throw new MailjetException ("Internal Exception: Malformed Url" );
0 commit comments