All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.contentstack.sdk.AssetsModel Maven / Gradle / Ivy

There is a newer version: 2.1.3
Show newest version
package com.contentstack.sdk;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * The type Assets model.
 */
class AssetsModel {

    List objects = new ArrayList<>();

    /**
     * Instantiates a new Assets model.
     *
     * @param response the response
     */
    public AssetsModel(JSONObject response) {
        JSONArray listResponse = null;
        Object rawAssets = response.opt("assets"); // Get assets
        if (rawAssets instanceof List) {  // Check if it's an ArrayList
            List assetsList = (List) rawAssets;
            listResponse = new JSONArray(assetsList); // Convert to JSONArray
        } else if (rawAssets != null) {
            throw new IllegalArgumentException("Invalid type for 'assets' key: " + rawAssets.getClass().getName());
        }
        if (listResponse != null) {
            listResponse.forEach(model -> {
                JSONObject modelObj = (JSONObject) model;
                AssetModel newModel = new AssetModel(modelObj, true);
                objects.add(newModel);
            });
        }
    }
}