use of com.microsoft.azure.sdk.iot.provisioning.service.configs.QueryResult in project azure-iot-sdk-java by Azure.
the class Query method next.
/**
* Return the next page of result for the query.
*
* @return A {@link QueryResult} with the next page of items for the query.
* @throws NoSuchElementException if the query does no have more pages to return.
*/
@Override
public QueryResult next() {
if (!hasNext) {
throw new NoSuchElementException("There are no more pending elements");
}
Map<String, String> headerParameters = new HashMap<>();
if (pageSize != 0) {
headerParameters.put(PAGE_SIZE_KEY, Integer.toString(pageSize));
}
if (!Tools.isNullOrEmpty(this.continuationToken)) {
headerParameters.put(CONTINUATION_TOKEN_KEY, this.continuationToken);
}
HttpResponse httpResponse;
try {
httpResponse = contractApiHttp.request(HttpMethod.POST, queryPath, headerParameters, querySpecificationJson);
} catch (ProvisioningServiceClientException e) {
// Because Query implements the iterator interface, the next cannot throws ProvisioningServiceClientException.
throw new IllegalArgumentException(e);
}
byte[] body = httpResponse.getBody();
if (body == null) {
throw new IllegalArgumentException("Http response for next cannot contains a null body");
}
String bodyStr = new String(body, StandardCharsets.UTF_8);
Map<String, String> headers = httpResponse.getHeaderFields();
String type = headers.get(ITEM_TYPE_KEY);
this.continuationToken = headers.get(CONTINUATION_TOKEN_KEY);
hasNext = (this.continuationToken != null);
return new QueryResult(type, bodyStr, this.continuationToken);
}
Aggregations