/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mailjet.client;

import com.mailjet.client.errors.MailjetException;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author Guillaume Badi
 */
public class MailjetResponse {
    
    private JSONObject _rawResponse;
    private int _status;

    public MailjetResponse(int status, JSONObject obj) {
        _rawResponse = obj;
        _status = status;
    }

    public MailjetResponse(JSONObject object) {
        _rawResponse = object;
        _status = 0;
    }
    
    public int getStatus() {
        return _status;
    }
    
    public JSONArray getData() {
        if (_rawResponse.has("Data")) {
            return _rawResponse.getJSONArray("Data");
        } else if (_rawResponse.has("Sent")) {
            return _rawResponse.getJSONArray("Sent");
        } else {
            return new JSONArray();
        }
    }
    
    public int getTotal() {
        if (_rawResponse.has("Total")) {
            return _rawResponse.getInt("Total");
        } else {
            return 0;
        }
    }
    
    public String getString(String key) throws MailjetException {
        try {
            return _rawResponse.getString(key);
        } catch (NullPointerException e) {
           throw new MailjetException("No entry found for key: " + key);
        }
    }
    
    public int getInt(String key) throws MailjetException {
        try {
            return _rawResponse.getInt(key);
        } catch (NullPointerException e) {
            throw new MailjetException("No entry found for key: " + key);
        }
    }
    
    public JSONArray getJSONArray(String key) throws MailjetException {
        try {
            return _rawResponse.getJSONArray(key);
        } catch (NullPointerException e) { 
            throw new MailjetException("No entry found for key: " + key);
        }
    }
    
    public int getCount() {
        if (_rawResponse.has("Count")) {
            return _rawResponse.getInt("Count");
        } else {
            return 0;
        }
    }
}

@@ -967,24 +967,133 @@
 ay(%22Sent%22);%0A
+        %7D else if (_rawResponse.has(%22Messages%22)) %7B%0A            return _rawResponse.getJSONArray(%22Messages%22);%0A
         %7D el

add Messages property in response