Cumulocity Microservice SDK: get all applications using ApplicationApi

What product/components do you use and which version/fix level are you on?

C8Y MS SDK 1015.334.0

What are you trying to achieve? Please describe it in detail.

I try to get all applications/microservices deployed to the current tenant. I’m using

		subscriptionsService.runForTenant(tenantId, () -> {
			ApplicationCollectionRepresentation list = applicationApi.list();
			List<ApplicationRepresentation> applications = list.getApplications();
			// this list only contains 5 elements
		});

The problem is, that only 5 elements are returned by “getApplications()” (probably b/c the default pagesize is 5) and I dont see a way to either increase the pagesize for this call or use a pagination feature similar to the InventoryApi or MeasurementApi.

Any ideas?
Thanks a lot!
Michael

Hi,
I can agree, this seems to be a limitation in the SDK. Might be worth creating a small feature request for this.

Alternatively you could use the RestConnector to do the API call yourself using something similar to this (not tested):

restConnector.get("/application/applications?pageSize=2000", CumulocityMediaType.APPLICATION_JSON, ApplicationCollectionRepresentation.class);

Regards Kai

Since ApplicationCollectionRepresentation doesn’t support paging like some of the other collection representation there is no way to iterate over multiple pages out of the box with this. Instead we can use RestConnector to specify parameters to our request directly. Here is an example for the application API:

@Component
public class ApplicationRepository {

    private final RestConnector restConnector;

    @Autowired
    public ApplicationRepository(RestConnector restConnector) {
        this.restConnector = restConnector;
    }

    public List<ApplicationRepresentation> getAllApplications() {
        int pageNumber = 1;
        List<ApplicationRepresentation> page = getApplicationsPage(pageNumber).getApplications();
        List<ApplicationRepresentation> allPages = new ArrayList<>();
        while (page.size() > 0) {
            allPages.addAll(page);
            pageNumber++;
            page = getApplicationsPage(pageNumber).getApplications();
        }
        return allPages;
    }

    private ApplicationCollectionRepresentation getApplicationsPage(int page) {
        return restConnector.get("/application/applications?pageSize=2000&currentPage=" + page,
                    ApplicationMediaType.APPLICATION_COLLECTION,
                    ApplicationCollectionRepresentation.class);
    }
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.