diff --git a/sdk/digitaltwins/azure-digitaltwins-core/API design.md b/sdk/digitaltwins/azure-digitaltwins-core/API design.md index 9750ccf0b3d92..36ae41ea90237 100644 --- a/sdk/digitaltwins/azure-digitaltwins-core/API design.md +++ b/sdk/digitaltwins/azure-digitaltwins-core/API design.md @@ -770,59 +770,6 @@ public DigitalTwinsResponse updateComponentWithResponse(String digitalTwin ``` -## Query -
Async APIs - - ```java - /** - * Query digital twins. - * - * @param query The query string, in SQL-like syntax. - * @return A {@link PagedFlux} of application/json for the query result items. - */ - @ServiceMethod(returns = ReturnType.COLLECTION) - public PagedFlux query(String query) - - /** - * Query digital twins. - * - * @param query The query string, in SQL-like syntax. - * @param clazz The model class to convert the query response to. - * @param The generic type to convert the query response to. - * @return A {@link PagedFlux} of application/json for the query result items. - */ - @ServiceMethod(returns = ReturnType.COLLECTION) - public PagedFlux query(String query, Class clazz) - ``` -
- -
Sync APIs - -```java -/** -* Query digital twins. -* -* @param query The query string, in SQL-like syntax. -* @param context Additional context that is passed through the Http pipeline during the service call. -* @return A {@link PagedIterable} of application/json for the query result items. -*/ -@ServiceMethod(returns = ReturnType.COLLECTION) -public PagedIterable query(String query, Context context) - -/** -* Query digital twins. -* -* @param query The query string, in SQL-like syntax. -* @param context Additional context that is passed through the Http pipeline during the service call. -* @param clazz The model class to convert the query response to. -* @param The generic type to convert the query response to. -* @return A {@link PagedIterable} of application/json for the query result items. -*/ -@ServiceMethod(returns = ReturnType.COLLECTION) -public PagedIterable query(String query, Class clazz, Context context) -``` -
- ## Models
Examples A model defines the properties, components, and relationships of a given digital twin. A sample model can be seen below diff --git a/sdk/digitaltwins/azure-digitaltwins-core/src/main/java/com/azure/digitaltwins/core/DigitalTwinsAsyncClient.java b/sdk/digitaltwins/azure-digitaltwins-core/src/main/java/com/azure/digitaltwins/core/DigitalTwinsAsyncClient.java index 5ef8f999fa02d..cd0d9f4a21d4c 100644 --- a/sdk/digitaltwins/azure-digitaltwins-core/src/main/java/com/azure/digitaltwins/core/DigitalTwinsAsyncClient.java +++ b/sdk/digitaltwins/azure-digitaltwins-core/src/main/java/com/azure/digitaltwins/core/DigitalTwinsAsyncClient.java @@ -15,6 +15,7 @@ import com.azure.digitaltwins.core.implementation.AzureDigitalTwinsAPIImplBuilder; import com.azure.digitaltwins.core.implementation.converters.ModelDataConverter; import com.azure.digitaltwins.core.implementation.models.DigitalTwinModelsListOptions; +import com.azure.digitaltwins.core.implementation.models.QuerySpecification; import com.azure.digitaltwins.core.implementation.serializer.DigitalTwinsStringSerializer; import com.azure.digitaltwins.core.models.IncomingRelationship; import com.azure.digitaltwins.core.models.ModelData; @@ -1067,4 +1068,128 @@ Mono> updateComponentWithResponse(String digitalTwinI return Mono.just(new DigitalTwinsResponse<>(response.getRequest(), response.getStatusCode(), response.getHeaders(), null, twinHeaders)); }); } + + /** + * Query digital twins. + * @param query The query string, in SQL-like syntax. + * @return A {@link PagedFlux} of application/json query result items. + */ + @ServiceMethod(returns = ReturnType.COLLECTION) + public PagedFlux query(String query) { + return new PagedFlux<>( + () -> withContext(context -> queryFirstPage(query, context)), + nextLink -> withContext(context -> queryNextPage(query, nextLink, context))); + } + + PagedFlux query(String query, Context context) { + return new PagedFlux<>( + () -> queryFirstPage(query, context), + nextLink -> queryNextPage(query, nextLink, context)); + } + + /** + * Query digital twins. + * @param query The query string, in SQL-like syntax. + * @param clazz The model class to convert the query response to. + * @param The generic type to convert the query response to. + * @return A {@link PagedFlux} of application/json of the specified type. + */ + @ServiceMethod(returns = ReturnType.COLLECTION) + public PagedFlux query(String query, Class clazz) { + return new PagedFlux( + () -> withContext(context -> queryFirstPage(query, clazz, context)), + nextLink -> withContext(context -> queryNextPage(nextLink, clazz, context))); + } + + PagedFlux query(String query, Class clazz, Context context) { + return new PagedFlux<>( + () -> queryFirstPage(query, clazz, context), + nextLink -> queryNextPage(nextLink, clazz, context)); + } + + Mono> queryFirstPage(String query, Context context) { + QuerySpecification querySpecification = new QuerySpecification().setQuery(query); + + return protocolLayer + .getQueries() + .queryTwinsWithResponseAsync(querySpecification, context) + .map(objectPagedResponse -> new PagedResponseBase<>( + objectPagedResponse.getRequest(), + objectPagedResponse.getStatusCode(), + objectPagedResponse.getHeaders(), + objectPagedResponse.getValue().getItems().stream() + .map(object -> { + try { + return mapper.writeValueAsString(object); + } catch (JsonProcessingException e) { + logger.error("JsonProcessingException occurred while retrieving query result items: ", e); + throw new RuntimeException("JsonProcessingException occurred while retrieving query result items", e); + } + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()), + objectPagedResponse.getValue().getContinuationToken(), + objectPagedResponse.getDeserializedHeaders())); + } + + Mono> queryFirstPage(String query, Class clazz, Context context) { + QuerySpecification querySpecification = new QuerySpecification().setQuery(query); + + return protocolLayer + .getQueries() + .queryTwinsWithResponseAsync(querySpecification, context) + .map(objectPagedResponse -> new PagedResponseBase<>( + objectPagedResponse.getRequest(), + objectPagedResponse.getStatusCode(), + objectPagedResponse.getHeaders(), + objectPagedResponse.getValue().getItems().stream() + .map(object -> mapper.convertValue(object, clazz)) + .filter(Objects::nonNull) + .collect(Collectors.toList()), + objectPagedResponse.getValue().getContinuationToken(), + objectPagedResponse.getDeserializedHeaders())); + } + + Mono> queryNextPage(String nextLink, Context context) { + QuerySpecification querySpecification = new QuerySpecification().setContinuationToken(nextLink); + + return protocolLayer + .getQueries() + .queryTwinsWithResponseAsync(querySpecification, context) + .map(objectPagedResponse -> new PagedResponseBase<>( + objectPagedResponse.getRequest(), + objectPagedResponse.getStatusCode(), + objectPagedResponse.getHeaders(), + objectPagedResponse.getValue().getItems().stream() + .map(object -> { + try { + return mapper.writeValueAsString(object); + } catch (JsonProcessingException e) { + logger.error("JsonProcessingException occurred while retrieving query result items: ", e); + throw new RuntimeException("JsonProcessingException occurred while retrieving query result items", e); + } + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()), + objectPagedResponse.getValue().getContinuationToken(), + objectPagedResponse.getDeserializedHeaders())); + } + + Mono> queryNextPage(String nextLink, Class clazz, Context context) { + QuerySpecification querySpecification = new QuerySpecification().setContinuationToken(nextLink); + + return protocolLayer + .getQueries() + .queryTwinsWithResponseAsync(querySpecification, context) + .map(objectPagedResponse -> new PagedResponseBase<>( + objectPagedResponse.getRequest(), + objectPagedResponse.getStatusCode(), + objectPagedResponse.getHeaders(), + objectPagedResponse.getValue().getItems().stream() + .map(object -> mapper.convertValue(object, clazz)) + .filter(Objects::nonNull) + .collect(Collectors.toList()), + objectPagedResponse.getValue().getContinuationToken(), + objectPagedResponse.getDeserializedHeaders())); + } } diff --git a/sdk/digitaltwins/azure-digitaltwins-core/src/main/java/com/azure/digitaltwins/core/DigitalTwinsClient.java b/sdk/digitaltwins/azure-digitaltwins-core/src/main/java/com/azure/digitaltwins/core/DigitalTwinsClient.java index 75aa1ec6770a3..4a99b926c02a2 100644 --- a/sdk/digitaltwins/azure-digitaltwins-core/src/main/java/com/azure/digitaltwins/core/DigitalTwinsClient.java +++ b/sdk/digitaltwins/azure-digitaltwins-core/src/main/java/com/azure/digitaltwins/core/DigitalTwinsClient.java @@ -636,4 +636,54 @@ public void updateComponent(String digitalTwinId, String componentPath, List updateComponentWithResponse(String digitalTwinId, String componentPath, List componentUpdateOperations, UpdateComponentRequestOptions options, Context context) { return digitalTwinsAsyncClient.updateComponentWithResponse(digitalTwinId, componentPath, componentUpdateOperations, options, context).block(); } + + /** + * Query digital twins. + * + * @param query The query string, in SQL-like syntax. + * @return A {@link PagedIterable} of application/json query result items. + */ + @ServiceMethod(returns = ReturnType.COLLECTION) + public PagedIterable query(String query) { + return new PagedIterable<>(digitalTwinsAsyncClient.query(query, Context.NONE)); + } + + /** + * Query digital twins. + * + * @param query The query string, in SQL-like syntax. + * @param context Additional context that is passed through the Http pipeline during the service call. + * @return A {@link PagedIterable} of application/json query result items. + */ + @ServiceMethod(returns = ReturnType.COLLECTION) + public PagedIterable query(String query, Context context) { + return new PagedIterable<>(digitalTwinsAsyncClient.query(query, context)); + } + + /** + * Query digital twins. + * + * @param query The query string, in SQL-like syntax. + * @param clazz The model class to convert the query response to. + * @param The generic type to convert the query response to. + * @return A {@link PagedIterable} of application/json query result items. + */ + @ServiceMethod(returns = ReturnType.COLLECTION) + public PagedIterable query(String query, Class clazz) { + return new PagedIterable<>(digitalTwinsAsyncClient.query(query, clazz, Context.NONE)); + } + + /** + * Query digital twins. + * + * @param query The query string, in SQL-like syntax. + * @param context Additional context that is passed through the Http pipeline during the service call. + * @param clazz The model class to convert the query response to. + * @param The generic type to convert the query response to. + * @return A {@link PagedIterable} of application/json query result items. + */ + @ServiceMethod(returns = ReturnType.COLLECTION) + public PagedIterable query(String query, Class clazz, Context context) { + return new PagedIterable<>(digitalTwinsAsyncClient.query(query, clazz, context)); + } }