Improve paging functionality
Is your feature request related to a problem? Please describe the problem.
In my opinion, the code required for getting all paged results of a collection request is not ideal. In v5 I had a generic method that handled it for all types, but I couldn't achieve the same using v6. Mainly because the classes for the original request use a RequestConfiguration object and the classes for the paginated requests use a RequestInformation object and they seem to be wholly incompatible.
This is the best I could come up with:
public List<User> getAllUsers(List<String> selectAttributes, String filter)
{
UserCollectionResponse userCollectionResponse = graphClient.users().get(
requestConfig ->
{
if (!selectAttributes.isEmpty())
{
requestConfig.queryParameters.select = selectAttributes.toArray(new String[0]);
if (selectAttributes.contains("manager"))
requestConfig.queryParameters.expand = new String[]{"manager"};
}
if (filter != null)
requestConfig.queryParameters.filter = filter;
});
UnaryOperator<RequestInformation> requestInformation =
requestInfo ->
{
if (!selectAttributes.isEmpty())
{
requestInfo.addQueryParameter("%24select", selectAttributes.toArray(new String[0]));
if (selectAttributes.contains("manager"))
requestInfo.addQueryParameter("%24expand", new String[]{"manager"});
}
if (filter != null)
requestInfo.addQueryParameter("%24filter", filter);
return requestInfo;
};
return loadPagedEntities(userCollectionResponse, UserCollectionResponse::createFromDiscriminatorValue, requestInformation);
}
private <T extends Parsable> List<T> loadPagedEntities(
BaseCollectionPaginationCountResponse baseCollectionPaginationCountResponse,
Function<ParseNode, BaseCollectionPaginationCountResponse> collectionPageFactoryFunction,
UnaryOperator<RequestInformation> requestInformation)
{
try
{
List<T> entities = new ArrayList<>();
PageIterator<T, BaseCollectionPaginationCountResponse> pageIterator =
new PageIterator.Builder<T, BaseCollectionPaginationCountResponse>()
.client(graphClient)
.collectionPage(Objects.requireNonNull(baseCollectionPaginationCountResponse))
.collectionPageFactory(collectionPageFactoryFunction::apply)
.requestConfigurator(requestInformation)
.processPageItemCallback(entities::add)
.build();
pageIterator.iterate();
return entities;
}
catch (ReflectiveOperationException e)
{
throw new RuntimeException(e);
}
}
What irks me most about this, is that I have to specify the query parameters twice and using different syntax! Especially the "%24..." is pretty ugly.
Describe the solution you'd like.
The best solution, of course, would be if the SDK handled this transparently:
List<User> allUsers = graphClient.users().getAll(requestConfig -> ...);
The next best solution would be some kind of compatibility between RequestConfiguration and RequestInformation, so the query parameters would only have to specified once.
What do you think?
Additional context?
No response
@mkomko thanks for using the Java SDK. We will be considering the proposal to improve the experience for Page iteration