|
15 | 15 | */ |
16 | 16 | public class MailjetResponse { |
17 | 17 |
|
18 | | - private JSONObject _rawResponse; |
19 | | - private int _status; |
| 18 | + private final JSONObject responseObject; |
| 19 | + private final String rawResponse; |
| 20 | + private final int status; |
20 | 21 |
|
21 | | - public MailjetResponse(int status, JSONObject obj) { |
22 | | - _rawResponse = obj; |
23 | | - _status = status; |
| 22 | + public MailjetResponse(int status, String rawResponse) { |
| 23 | + responseObject = new JSONObject(rawResponse); |
| 24 | + this.rawResponse = rawResponse; |
| 25 | + this.status = status; |
24 | 26 | } |
25 | | - |
26 | | - public MailjetResponse(JSONObject object) { |
27 | | - _rawResponse = object; |
28 | | - _status = 0; |
29 | | - } |
30 | | - |
| 27 | + /** |
| 28 | + * @return HTTP status code returned by Mailjet server |
| 29 | + */ |
31 | 30 | public int getStatus() { |
32 | | - return _status; |
| 31 | + return status; |
33 | 32 | } |
34 | | - |
| 33 | + |
| 34 | + /** |
| 35 | + * @return Raw response string sent by Mailjet server |
| 36 | + */ |
| 37 | + public String getRawResponseContent() { return rawResponse; } |
| 38 | + |
35 | 39 | public JSONArray getData() { |
36 | | - if (_rawResponse.has("Data")) { |
37 | | - return _rawResponse.getJSONArray("Data"); |
38 | | - } else if (_rawResponse.has("Sent")) { |
39 | | - return _rawResponse.getJSONArray("Sent"); |
40 | | - } else if (_rawResponse.has("Messages")) { |
41 | | - return _rawResponse.getJSONArray("Messages"); |
| 40 | + if (responseObject.has("Data")) { |
| 41 | + return responseObject.getJSONArray("Data"); |
| 42 | + } else if (responseObject.has("Sent")) { |
| 43 | + return responseObject.getJSONArray("Sent"); |
| 44 | + } else if (responseObject.has("Messages")) { |
| 45 | + return responseObject.getJSONArray("Messages"); |
42 | 46 | } else { |
43 | | - return (new JSONArray()).put(_rawResponse); |
| 47 | + return (new JSONArray()).put(responseObject); |
44 | 48 | } |
45 | 49 | } |
46 | 50 |
|
47 | 51 | public int getTotal() { |
48 | | - if (_rawResponse.has("Total")) { |
49 | | - return _rawResponse.getInt("Total"); |
| 52 | + if (responseObject.has("Total")) { |
| 53 | + return responseObject.getInt("Total"); |
50 | 54 | } else { |
51 | 55 | return 0; |
52 | 56 | } |
53 | 57 | } |
54 | 58 |
|
55 | 59 | public String getString(String key) throws MailjetException { |
56 | 60 | try { |
57 | | - return _rawResponse.getString(key); |
| 61 | + return responseObject.getString(key); |
58 | 62 | } catch (NullPointerException e) { |
59 | 63 | throw new MailjetException("No entry found for key: " + key); |
60 | 64 | } |
61 | 65 | } |
62 | 66 |
|
63 | 67 | public int getInt(String key) throws MailjetException { |
64 | 68 | try { |
65 | | - return _rawResponse.getInt(key); |
| 69 | + return responseObject.getInt(key); |
66 | 70 | } catch (NullPointerException e) { |
67 | 71 | throw new MailjetException("No entry found for key: " + key); |
68 | 72 | } |
69 | 73 | } |
70 | 74 |
|
71 | 75 | public JSONArray getJSONArray(String key) throws MailjetException { |
72 | 76 | try { |
73 | | - return _rawResponse.getJSONArray(key); |
| 77 | + return responseObject.getJSONArray(key); |
74 | 78 | } catch (NullPointerException e) { |
75 | 79 | throw new MailjetException("No entry found for key: " + key); |
76 | 80 | } |
77 | 81 | } |
78 | 82 |
|
79 | 83 | public int getCount() { |
80 | | - if (_rawResponse.has("Count")) { |
81 | | - return _rawResponse.getInt("Count"); |
| 84 | + if (responseObject.has("Count")) { |
| 85 | + return responseObject.getInt("Count"); |
82 | 86 | } else { |
83 | 87 | return 0; |
84 | 88 | } |
|
0 commit comments