Skip to content

Commit d2e215a

Browse files
authored
Merge pull request #165 from contentstack/enh/DX-2072-skip-limit-method-for-assetlib
Added skip limit methods for fetching assets
2 parents 0266d0a + 4361709 commit d2e215a

File tree

4 files changed

+172
-8
lines changed

4 files changed

+172
-8
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# CHANGELOG
22

3+
## v2.0.3
4+
5+
### Date: 3-March-2025
6+
7+
- Added skip limit methods for Assets
8+
- Resolved a bug
9+
310
## v2.0.2
411

512
### Date: 5-December-2024

src/main/java/com/contentstack/sdk/AssetLibrary.java

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,101 @@ public int getCount() {
133133
return count;
134134
}
135135

136+
/**
137+
* Add param assetlibrary.
138+
*
139+
* @param paramKey the param key
140+
* @param paramValue the param value
141+
* @return the assetlibrary
142+
*
143+
* <br>
144+
* <br>
145+
* <b>Example :</b><br>
146+
*
147+
* <pre class="prettyprint">
148+
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
149+
* AssetLibrary assetLibObject = stack.assetlibrary();
150+
* assetLibObject.addParam();
151+
* </pre>
152+
*/
153+
public AssetLibrary addParam(@NotNull String paramKey, @NotNull Object paramValue) {
154+
urlQueries.put(paramKey, paramValue);
155+
return this;
156+
}
157+
158+
/**
159+
* Remove param key assetlibrary.
160+
*
161+
* @param paramKey the param key
162+
* @return the assetlibrary
163+
*
164+
* <br>
165+
* <br>
166+
* <b>Example :</b><br>
167+
*
168+
* <pre class="prettyprint">
169+
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
170+
* AssetLibrary assetLibObject = stack.assetlibrary();
171+
* assetLibObject.removeParam(paramKey);
172+
* </pre>
173+
*/
174+
public AssetLibrary removeParam(@NotNull String paramKey){
175+
if(urlQueries.has(paramKey)){
176+
urlQueries.remove(paramKey);
177+
}
178+
return this;
179+
}
180+
181+
182+
183+
/**
184+
* The number of objects to skip before returning any.
185+
*
186+
* @param number No of objects to skip from returned objects
187+
* @return {@link Query} object, so you can chain this call.
188+
* <p>
189+
* <b> Note: </b> The skip parameter can be used for pagination,
190+
* &#34;skip&#34; specifies the number of objects to skip in the response. <br>
191+
*
192+
* <br>
193+
* <br>
194+
* <b>Example :</b><br>
195+
*
196+
* <pre class="prettyprint">
197+
* Stack stack = Contentstack.stack( "apiKey", "deliveryToken", "environment");
198+
* AssetLibrary assetLibObject = stack.assetlibrary.skip(4);<br>
199+
* </pre>
200+
*/
201+
202+
public AssetLibrary skip (@NotNull int number) {
203+
urlQueries.put("skip",number);
204+
return this;
205+
}
206+
207+
/**
208+
* A limit on the number of objects to return.
209+
*
210+
* @param number No of objects to limit.
211+
* @return {@link Query} object, so you can chain this call.
212+
* <p>
213+
* <b> Note:</b> The limit parameter can be used for pagination, &#34;
214+
* limit&#34; specifies the number of objects to limit to in the response. <br>
215+
*
216+
* <br>
217+
* <br>
218+
* <b>Example :</b><br>
219+
*
220+
* <pre class="prettyprint">
221+
* Stack stack = Contentstack.stack( "apiKey", "deliveryToken", "environment");
222+
* AssetLibrary assetLibObject = stack.assetlibrary.limit(4);<br>
223+
* </pre>
224+
*/
225+
226+
public AssetLibrary limit (@NotNull int number) {
227+
urlQueries.put("limit", number);
228+
return this;
229+
}
230+
136231
/**
137232
* Fetch all.
138233
*
@@ -180,6 +275,10 @@ public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean
180275

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

278+
// if (objects == null || objects.isEmpty()) {
279+
// System.out.println("Objects list is null or empty");
280+
// }
281+
183282
if (objects != null && !objects.isEmpty()) {
184283
for (Object object : objects) {
185284
AssetModel model = (AssetModel) object;
@@ -193,7 +292,10 @@ public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean
193292
asset.setTags(model.tags);
194293
assets.add(asset);
195294
}
196-
}
295+
}
296+
// else {
297+
// System.out.println("Object is not an instance of AssetModel");
298+
// }
197299

198300
if (callback != null) {
199301
callback.onRequestFinish(ResponseType.NETWORK, assets);

src/main/java/com/contentstack/sdk/AssetsModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
56
import org.json.JSONArray;
67
import org.json.JSONObject;
78

8-
99
/**
1010
* The type Assets model.
1111
*/

src/test/java/com/contentstack/sdk/TestAssetLibrary.java

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.List;
77
import java.util.logging.Logger;
88

9-
import static org.junit.jupiter.api.Assertions.assertEquals;
109

1110
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
1211
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@@ -24,15 +23,15 @@ void testNewAssetLibrary() {
2423
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
2524
Asset model = assets.get(0);
2625
Assertions.assertTrue(model.getAssetUid().startsWith("blt"));
27-
assertEquals("image/png", model.getFileType());
28-
assertEquals("13006", model.getFileSize());
29-
assertEquals("iot-icon.png", model.getFileName());
26+
Assertions.assertEquals("image/png", model.getFileType());
27+
Assertions.assertEquals("13006", model.getFileSize());
28+
Assertions.assertEquals("iot-icon.png", model.getFileName());
3029
Assertions.assertTrue(model.getUrl().endsWith("iot-icon.png"));
3130
Assertions.assertTrue(model.toJSON().has("created_at"));
3231
Assertions.assertTrue(model.getCreatedBy().startsWith("blt"));
33-
assertEquals("gregory", model.getUpdateAt().getCalendarType());
32+
Assertions.assertEquals("gregory", model.getUpdateAt().getCalendarType());
3433
Assertions.assertTrue(model.getUpdatedBy().startsWith("blt"));
35-
assertEquals("", model.getDeletedBy());
34+
Assertions.assertEquals("", model.getDeletedBy());
3635
logger.info("passed...");
3736
}
3837
});
@@ -107,4 +106,60 @@ public void onCompletion(ResponseType responseType, List<Asset> assets, Error er
107106
}
108107
});
109108
}
109+
110+
@Test
111+
void testFetchFirst10Assets() throws IllegalAccessException {
112+
AssetLibrary assetLibrary = stack.assetLibrary();
113+
assetLibrary.skip(0).limit(10).fetchAll(new FetchAssetsCallback() {
114+
@Override
115+
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
116+
Assertions.assertNotNull(assets, "Assets list should not be null");
117+
Assertions.assertTrue(assets.size() <= 10, "Assets fetched should not exceed the limit");
118+
}
119+
});
120+
}
121+
122+
@Test
123+
void testFetchAssetsWithSkip() throws IllegalAccessException {
124+
AssetLibrary assetLibrary = stack.assetLibrary();
125+
assetLibrary.skip(10).limit(10).fetchAll(new FetchAssetsCallback() {
126+
@Override
127+
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
128+
Assertions.assertNotNull(assets, "Assets list should not be null");
129+
Assertions.assertTrue(assets.size() <= 10, "Assets fetched should not exceed the limit");
130+
}
131+
});
132+
}
133+
134+
@Test
135+
void testFetchBeyondAvailableAssets() throws IllegalAccessException {
136+
AssetLibrary assetLibrary = stack.assetLibrary();
137+
assetLibrary.skip(5000).limit(10).fetchAll(new FetchAssetsCallback() {
138+
@Override
139+
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
140+
Assertions.assertNotNull(assets, "Assets list should not be null");
141+
Assertions.assertEquals(0, assets.size(), "No assets should be fetched when skip exceeds available assets");
142+
}
143+
});
144+
}
145+
146+
@Test
147+
void testFetchAllAssetsInBatches() throws IllegalAccessException {
148+
AssetLibrary assetLibrary = stack.assetLibrary();
149+
int limit = 50;
150+
int totalAssetsFetched[] = {0};
151+
152+
for (int skip = 0; skip < 150; skip += limit) {
153+
assetLibrary.skip(skip).limit(limit).fetchAll(new FetchAssetsCallback() {
154+
@Override
155+
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
156+
totalAssetsFetched[0] += assets.size();
157+
Assertions.assertNotNull(assets, "Assets list should not be null");
158+
Assertions.assertTrue(assets.size() <= limit, "Assets fetched should not exceed the limit");
159+
Assertions.assertEquals(6, totalAssetsFetched[0]);
160+
}
161+
});
162+
}
163+
}
164+
110165
}

0 commit comments

Comments
 (0)