Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #167

Merged
merged 14 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CHANGELOG

## v2.0.3

### Date: 3-March-2025

- Added skip limit methods for Assets
- Resolved a bug

## v2.0.2

### Date: 5-December-2024
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.contentstack.sdk</groupId>
<artifactId>java</artifactId>
<version>2.0.2</version>
<version>2.0.3</version>
<packaging>jar</packaging>
<name>contentstack-java</name>
<description>Java SDK for Contentstack Content Delivery API</description>
Expand Down
104 changes: 103 additions & 1 deletion src/main/java/com/contentstack/sdk/AssetLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,101 @@ public int getCount() {
return count;
}

/**
* Add param assetlibrary.
*
* @param paramKey the param key
* @param paramValue the param value
* @return the assetlibrary
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* AssetLibrary assetLibObject = stack.assetlibrary();
* assetLibObject.addParam();
* </pre>
*/
public AssetLibrary addParam(@NotNull String paramKey, @NotNull Object paramValue) {
urlQueries.put(paramKey, paramValue);
return this;
}

/**
* Remove param key assetlibrary.
*
* @param paramKey the param key
* @return the assetlibrary
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* AssetLibrary assetLibObject = stack.assetlibrary();
* assetLibObject.removeParam(paramKey);
* </pre>
*/
public AssetLibrary removeParam(@NotNull String paramKey){
if(urlQueries.has(paramKey)){
urlQueries.remove(paramKey);
}
return this;
}



/**
* The number of objects to skip before returning any.
*
* @param number No of objects to skip from returned objects
* @return {@link Query} object, so you can chain this call.
* <p>
* <b> Note: </b> The skip parameter can be used for pagination,
* &#34;skip&#34; specifies the number of objects to skip in the response. <br>
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack( "apiKey", "deliveryToken", "environment");
* AssetLibrary assetLibObject = stack.assetlibrary.skip(4);<br>
* </pre>
*/

public AssetLibrary skip (@NotNull int number) {
urlQueries.put("skip",number);
return this;
}

/**
* A limit on the number of objects to return.
*
* @param number No of objects to limit.
* @return {@link Query} object, so you can chain this call.
* <p>
* <b> Note:</b> The limit parameter can be used for pagination, &#34;
* limit&#34; specifies the number of objects to limit to in the response. <br>
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack( "apiKey", "deliveryToken", "environment");
* AssetLibrary assetLibObject = stack.assetlibrary.limit(4);<br>
* </pre>
*/

public AssetLibrary limit (@NotNull int number) {
urlQueries.put("limit", number);
return this;
}

/**
* Fetch all.
*
Expand Down Expand Up @@ -180,6 +275,10 @@ public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean

List<Asset> assets = new ArrayList<>();

// if (objects == null || objects.isEmpty()) {
// System.out.println("Objects list is null or empty");
// }

if (objects != null && !objects.isEmpty()) {
for (Object object : objects) {
AssetModel model = (AssetModel) object;
Expand All @@ -193,7 +292,10 @@ public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean
asset.setTags(model.tags);
assets.add(asset);
}
}
}
// else {
// System.out.println("Object is not an instance of AssetModel");
// }

if (callback != null) {
callback.onRequestFinish(ResponseType.NETWORK, assets);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/contentstack/sdk/AssetModel.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.contentstack.sdk;

import java.util.LinkedHashMap;
import org.json.JSONArray;
import org.json.JSONObject;


/**
* The type Asset model.
*/
Expand All @@ -25,11 +27,10 @@ class AssetModel {
* @param isArray the is array
*/
public AssetModel(JSONObject response, boolean isArray) {

if (isArray) {
json = response;
} else {
json = response.optJSONObject("asset");
json = new JSONObject((LinkedHashMap<?, ?>) response.get("asset"));
}

if (json != null) {
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/contentstack/sdk/AssetsModel.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.contentstack.sdk;

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

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

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

/**
* The type Assets model.
*/
Expand All @@ -19,7 +19,12 @@ class AssetsModel {
* @param response the response
*/
public AssetsModel(JSONObject response) {
JSONArray listResponse = response != null && response.has("assets") ? response.optJSONArray("assets") : null;
JSONArray listResponse = null;
Object rawAssets = response.get("assets"); // Get assets
if (rawAssets instanceof List) { // Check if it's an ArrayList
List<?> assetsList = (List<?>) rawAssets;
listResponse = new JSONArray(assetsList); // Convert to JSONArray
}
if (listResponse != null) {
listResponse.forEach(model -> {
JSONObject modelObj = (JSONObject) model;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/contentstack/sdk/CSConnectionRequest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.contentstack.sdk;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import org.json.JSONObject;


import static com.contentstack.sdk.Constants.*;

Expand Down Expand Up @@ -128,7 +128,8 @@ public void onRequestFinished(CSHttpConnection request) {
EntriesModel model = new EntriesModel(jsonResponse);
notifyClass.getResultObject(model.objectList, jsonResponse, true);
} else if (request.getController().equalsIgnoreCase(Constants.FETCHENTRY)) {
EntryModel model = new EntryModel(jsonResponse);
JSONObject jsonModel = new JSONObject((LinkedHashMap<?, ?>) jsonResponse.get("entry"));
EntryModel model = new EntryModel(jsonModel);
entryInstance.resultJson = model.jsonObject;
entryInstance.title = model.title;
entryInstance.url = model.url;
Expand Down
31 changes: 15 additions & 16 deletions src/main/java/com/contentstack/sdk/CSHttpConnection.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package com.contentstack.sdk;

import okhttp3.Request;
import okhttp3.ResponseBody;

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

import retrofit2.Call;
import retrofit2.Response;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.type.MapType;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.SocketTimeoutException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -22,10 +15,16 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.IntStream;
import com.fasterxml.jackson.databind.ObjectMapper; // Jackson for JSON parsing
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.type.MapType;
import okhttp3.Request;
import okhttp3.ResponseBody;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import retrofit2.Call;
import retrofit2.Response;




import static com.contentstack.sdk.Constants.*;

Expand Down Expand Up @@ -230,7 +229,7 @@ private void getService(String requestUrl) throws IOException {
MapType type = mapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class,
Object.class);
Map<String, Object> responseMap = mapper.readValue(response.body().string(), type);

// Use the custom method to create an ordered JSONObject
responseJSON = createOrderedJSONObject(responseMap);
if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) {
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/com/contentstack/sdk/ContentTypesModel.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.contentstack.sdk;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;



/**
* The ContentTypesModel that contains content type response
*/
Expand All @@ -12,16 +17,26 @@ public class ContentTypesModel {
private JSONArray responseJSONArray = new JSONArray();

public void setJSON(JSONObject responseJSON) {

if (responseJSON != null) {
String ctKey = "content_type";
if (responseJSON.has(ctKey) && responseJSON.opt(ctKey) instanceof JSONObject) {
this.response = responseJSON.optJSONObject(ctKey);
if (responseJSON.has(ctKey) && responseJSON.opt(ctKey) instanceof LinkedHashMap) {
this.response = new JSONObject((LinkedHashMap<?, ?>) responseJSON.get(ctKey));
}
String ctListKey = "content_types";
if (responseJSON.has(ctListKey) && responseJSON.opt(ctListKey) instanceof JSONArray) {
this.response = responseJSON.optJSONArray(ctListKey);
this.responseJSONArray = (JSONArray) this.response;
if (responseJSON.has(ctListKey) && responseJSON.opt(ctListKey) instanceof ArrayList) {
ArrayList<LinkedHashMap<?, ?>> contentTypes = (ArrayList) responseJSON.get(ctListKey);
List<Object> objectList = new ArrayList<>();
if (!contentTypes.isEmpty()) {
contentTypes.forEach(model -> {
if (model instanceof LinkedHashMap) {
// Convert LinkedHashMap to JSONObject
JSONObject jsonModel = new JSONObject((LinkedHashMap<?, ?>) model);
objectList.add(jsonModel);
}
});
}
this.response = new JSONArray(objectList);
this.responseJSONArray = new JSONArray(objectList);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/contentstack/sdk/EntryModel.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.contentstack.sdk;

import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

class EntryModel {

Expand Down Expand Up @@ -40,6 +40,7 @@ class EntryModel {

public EntryModel(JSONObject response) {
this.jsonObject = response;

if (this.jsonObject.has(ENTRY_KEY)) {
this.jsonObject = jsonObject.optJSONObject(ENTRY_KEY);
}
Expand All @@ -59,7 +60,6 @@ public EntryModel(JSONObject response) {
if (this.jsonObject.has("description")) {
this.description = this.jsonObject.opt("description");
}

this.images = (JSONArray) this.jsonObject.opt("images");
this.isDirectory = (Boolean) this.jsonObject.opt("is_dir");
this.updatedAt = (String) this.jsonObject.opt("updated_at");
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/contentstack/sdk/Query.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.contentstack.sdk;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.contentstack.sdk.Constants.*;

Expand Down Expand Up @@ -1226,7 +1226,6 @@ public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean
entry.setTags(((EntryModel) object).tags);
objectList.add(entry);
}

if (isSingleEntry) {
Entry entry = contentTypeInstance.entry();
if (!objectList.isEmpty()) {
Expand Down
Loading
Loading