Hi,
we’re using the Java Microservice SDK 1020.155.0 in a Cumulocity Microservice. The Microservice offers a REST API to its users where they can query some specific objects in the Inventory.
Now we have a question rgd the usage of pagination queries using the InventoryApi.
As long as the Micropservice exists, this logic (probably inherited and/or copied from some tutorial/example) is being used:
// the filter contains something like "owner" or "type", but it does *not* contain the requested currentPage or pageSize
final ManagedObjectCollection managedObjectsByFilter = inventoryApi.getManagedObjectsByFilter(filter);
// here we use the pageSize
PagedManagedObjectCollectionRepresentation collectionRepresentation = managedObjectsByFilter.get(pageSize);
// here we use currentPage & pageSize
PagedManagedObjectCollectionRepresentation pagedMOCollection = managedObjectsByFilter.getPage(collectionRepresentation, currentPage, pageSize);
When debugging this code section and recording the actual REST requests to Inventory made under the hood (to find an unrelated bug), we surprisingly found out that actually two requests were made:
1. managedObjectsByFilter.get(pageSize): executes a REST request with the filter criteria and pageSize; since there is no way to define the currentPage, the platform defaults will be used, i.e. the first page will be loaded
2. managedObjectsByFilter.getPage(collectionRepresentation, currentPage, pageSize): executes another REST request based on the previously defined filter plus the given pageSize & currentPage
While the first request *always* queries the first page, only the second one queries for the data which the user actually requested. At the end, the data returned is correct, but obviously we questioned if this is the right way to go.
So now we came up with a slightly different approach:
// the filter now contains the query but also the requested *currentPage*, but not the pageSize !
final ManagedObjectCollection managedObjectsByFilter = getInventoryApi().getManagedObjectsByFilter(filter);
// now this is the same call as above using the pageSize
PagedManagedObjectCollectionRepresentation collectionRepresentation = managedObjectsByFilter.get(pageSize);
Since the com.cumulocity.sdk.client.inventory.InventoryFilter class does not provide the “currentPage” field, we extended it like this:
public class CustomInventoryFilter extends InventoryFilter {
@Getter
@ParamSource
private String currentPage;
public CustomInventoryFilter withCurrentPage(int currentPage) {
this.currentPage = String.valueOf(currentPage);
return this;
}
}
While all our Unit/Cypress tests are still green
we’d still get your feedback on this topic. Are there any disadvantages of using the new approach? Any use cases of the initial implementation that are not covered with the new one? Did we miss an example or hint in the doc? Here I can only see usages like this, but this does not fit our use case:
inventory.getManagedObjectsByFilter(inventoryFilter).get().allPages();
Is the latter approach even considered best practice?
Thanks a lot!
Michael