Skip to content

Commit

Permalink
Implement Models API Part #1 (#14533)
Browse files Browse the repository at this point in the history
  • Loading branch information
azabbasi committed Aug 31, 2020
1 parent db97b97 commit 1317e07
Show file tree
Hide file tree
Showing 4 changed files with 324 additions and 114 deletions.
114 changes: 2 additions & 112 deletions sdk/digitaltwins/azure-digitaltwins-core/API design.md
Original file line number Diff line number Diff line change
Expand Up @@ -1175,63 +1175,7 @@ When updating a model, the payload for a multi-operation json patch follows the
Async APIs

```java
/**
* Creates one or many models.
* @param models The list of models to create. Each string corresponds to exactly one model.
* @return The list of created models
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public Mono<List<ModelData>> createModels(List<String> models) { }

/**
* Creates one or many models.
* @param models The list of models to create. Each string corresponds to exactly one model.
* @return A REST response containing the list of created models.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public Mono<Response<List<ModelData>>> createModelsWithResponse(List<String> models) { }

/**
* Gets a model, including the model metadata and the model definition.
* @param modelId The Id of the model.
* @return The application/json model
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<ModelData> getModel(String modelId) { }

/**
* Gets a model, including the model metadata and the model definition asynchronously.
* @param modelId The Id of the model.
* @return A REST response containing the model.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<ModelData>> getModelWithResponse(String modelId) { }

/**
* Gets the list of models by iterating through a collection.
* @param dependenciesFor The model Ids to have dependencies retrieved.
* @param includeModelDefinition Whether to include the model definition in the result. If false, only the model metadata will be returned.
* @param options The options to follow when listing the models. For example, the page size hint can be specified.
* @return A {@link PagedFlux} of ModelData.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedFlux<ModelData> listModels(List<String> dependenciesFor, boolean includeModelDefinition, DigitalTwinModelsListOptions options) { }

/**
* Deletes a model.
* @param modelId The id for the model. The id is globally unique and case sensitive.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Void> deleteModel(String modelId) { }

/**
* Deletes a model.
* @param modelId The id for the model. The id is globally unique and case sensitive.
* @return The http response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response> deleteModelWithResponse(String modelId) { }


/**
* Decommissions a model.
* @param modelId The Id of the model to decommission.
Expand All @@ -1252,61 +1196,6 @@ Async APIs
Sync APIs
```java
/**
* Creates one or many models.
* @param models The list of models to create. Each string corresponds to exactly one model.
* @return The list of created models
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public List<ModelData> createModels(List<String> models) { }

/**
* Creates one or many models.
* @param models The list of models to create. Each string corresponds to exactly one model.
* @return A REST response containing the list of created models.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public Response<List<ModelData>> createModelsWithResponse(List<String> models, Context context) { }

/**
* Gets a model, including the model metadata and the model definition.
* @param modelId The Id of the model.
* @return The application/json model
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public ModelData getModel(String modelId) { }

/**
* Gets a model, including the model metadata and the model definition.
* @param modelId The Id of the model.
* @return A REST response containing the model.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<ModelData> getModelWithResponse(String modelId, Context context) { }

/**
* Gets the list of models by iterating through a collection.
* @param dependenciesFor The model Ids to have dependencies retrieved.
* @param includeModelDefinition Whether to include the model definition in the result. If false, only the model metadata will be returned.
* @param options The options to follow when listing the models. For example, the page size hint can be specified.
* @return A {@link PagedIterable} of ModelData.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<ModelData> listModels(List<String> dependenciesFor, boolean includeModelDefinition, DigitalTwinModelsListOptions options, Context context) { }

/**
* Deletes a model.
* @param modelId The id for the model. The id is globally unique and case sensitive.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Void> deleteModel(String modelId) { }

/**
* Deletes a model.
* @param modelId The id for the model. The id is globally unique and case sensitive.
* @return The http response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response deleteModelWithResponse(String modelId, Context context) { }
/**
* Decommissions a model.
Expand All @@ -1318,6 +1207,7 @@ Sync APIs
/**
* Decommissions a model.
* @param modelId The Id of the model to decommission.
* @param context Additional context that is passed through the Http pipeline during the service call.
* @return The http response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.digitaltwins.core.implementation.AzureDigitalTwinsAPIImpl;
import com.azure.digitaltwins.core.implementation.AzureDigitalTwinsAPIImplBuilder;
import com.azure.digitaltwins.core.implementation.models.DigitalTwinModelsListOptions;
import com.azure.digitaltwins.core.models.ModelData;
import com.azure.digitaltwins.core.util.DigitalTwinsResponse;
import com.azure.digitaltwins.core.util.DigitalTwinsResponseHeaders;
import com.azure.digitaltwins.core.implementation.serializer.DigitalTwinsStringSerializer;
import com.azure.digitaltwins.core.util.ListModelOptions;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
Expand Down Expand Up @@ -48,6 +53,7 @@ public final class DigitalTwinsAsyncClient {
private static final ObjectMapper mapper = new ObjectMapper();
private final DigitalTwinsServiceVersion serviceVersion;
private final AzureDigitalTwinsAPIImpl protocolLayer;
private static final Boolean includeModelDefinition = true;

DigitalTwinsAsyncClient(HttpPipeline pipeline, DigitalTwinsServiceVersion serviceVersion, String host) {
final SimpleModule stringModule = new SimpleModule("String Serializer");
Expand Down Expand Up @@ -228,4 +234,149 @@ Mono<PagedResponse<String>> listRelationshipsNextSinglePageAsync(String nextLink
});
}


//==================================================================================================================================================
// Models APIs
//==================================================================================================================================================

/**
* Creates one or many models.
* @param models The list of models to create. Each string corresponds to exactly one model.
* @return A {@link PagedFlux} of created models and the http response.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedFlux<ModelData> createModels(List<String> models) {
return new PagedFlux<>(
() -> withContext(context -> createModelsSinglePageAsync(models, context)),
nextLink -> withContext(context -> Mono.empty()));
}

PagedFlux<ModelData> createModels(List<String> models, Context context){
return new PagedFlux<>(
() -> createModelsSinglePageAsync(models, context),
nextLink -> Mono.empty());
}

Mono<PagedResponse<ModelData>> createModelsSinglePageAsync(List<String> models, Context context)
{
List<Object> modelsPayload = new ArrayList<>();
for (String model: models) {
try {
modelsPayload.add(mapper.readValue(model, Object.class));
}
catch (JsonProcessingException e) {
logger.error("Could not parse the model payload [%s]: %s", model, e);
return Mono.error(e);
}
}

return protocolLayer.getDigitalTwinModels().addWithResponseAsync(modelsPayload, context)
.map(
listResponse -> new PagedResponseBase<>(
listResponse.getRequest(),
listResponse.getStatusCode(),
listResponse.getHeaders(),
listResponse.getValue(),
null,
((ResponseBase)listResponse).getDeserializedHeaders()));
}

/**
* Gets a model, including the model metadata and the model definition.
* @param modelId The Id of the model.
* @return The ModelData
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<ModelData> getModel(String modelId) {
return withContext(context -> getModelWithResponse(modelId, context))
.flatMap(response -> Mono.just(response.getValue()));
}

/**
* Gets a model, including the model metadata and the model definition.
* @param modelId The Id of the model.
* @return The ModelData and the http response
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<ModelData>> getModelWithResponse(String modelId) {
return withContext(context -> getModelWithResponse(modelId, context));
}

Mono<Response<ModelData>> getModelWithResponse(String modelId, Context context){
return protocolLayer
.getDigitalTwinModels()
.getByIdWithResponseAsync(modelId, includeModelDefinition, context);
}

/**
* Gets the list of models by iterating through a collection.
* @param listModelOptions The options to follow when listing the models. For example, the page size hint can be specified.
* @return A {@link PagedFlux} of ModelData and the http response.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedFlux<ModelData> listModels(ListModelOptions listModelOptions) {
return new PagedFlux<>(
() -> withContext(context -> listModelsSinglePageAsync(listModelOptions, context)),
nextLink -> withContext(context -> listModelsNextSinglePageAsync(nextLink, context)));
}

/**
* Gets the list of models by iterating through a collection.
* @return A {@link PagedFlux} of ModelData and the http response.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedFlux<ModelData> listModels() {
return listModels(new ListModelOptions());
}

PagedFlux<ModelData> listModels(Context context){
return new PagedFlux<>(
() -> listModelsSinglePageAsync(new ListModelOptions(), context),
nextLink -> listModelsNextSinglePageAsync(nextLink, context));
}

PagedFlux<ModelData> listModels(ListModelOptions listModelOptions, Context context){
return new PagedFlux<>(
() -> listModelsSinglePageAsync(listModelOptions, context),
nextLink -> listModelsNextSinglePageAsync(nextLink, context));
}

Mono<PagedResponse<ModelData>> listModelsSinglePageAsync(ListModelOptions listModelOptions, Context context){
return protocolLayer.getDigitalTwinModels().listSinglePageAsync(
listModelOptions.getDependenciesFor(),
listModelOptions.getIncludeModelDefinition(),
new DigitalTwinModelsListOptions().setMaxItemCount(listModelOptions.getMaxItemCount()),
context);
}

Mono<PagedResponse<ModelData>> listModelsNextSinglePageAsync(String nextLink, Context context){
return protocolLayer.getDigitalTwinModels().listNextSinglePageAsync(nextLink, context);
}

/**
* Deletes a model.
* @param modelId The Id for the model. The Id is globally unique and case sensitive.
* @return An empty Mono
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Void> deleteModel(String modelId) {
return withContext(context -> deleteModelWithResponse(modelId, context))
.flatMap(response -> Mono.just(response.getValue()));
}

/**
* Deletes a model.
* @param modelId The Id for the model. The Id is globally unique and case sensitive.
* @return The http response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Void>> deleteModelWithResponse(String modelId) {
return withContext(context -> deleteModelWithResponse(modelId, context));
}

Mono<Response<Void>> deleteModelWithResponse(String modelId, Context context){
return protocolLayer.getDigitalTwinModels().deleteWithResponseAsync(modelId, context);
}

//TODO: Decommission Model APIs (waiting for Abhipsa's change to come in)
}
Loading

0 comments on commit 1317e07

Please sign in to comment.