-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Implement Models API Part #1 #14533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
azabbasi
merged 15 commits into
master
from
feature/adt/azabbasi/secondIterationModelsAPI
Aug 31, 2020
Merged
Implement Models API Part #1 #14533
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
465543c
Implement ListModels API
azabbasi cd664bc
Update API design.md
azabbasi 73ff14c
Update DigitalTwinsAsyncClient.java
azabbasi 5468f61
Add createModels API
azabbasi 7835323
Minor fix to the next link
azabbasi 68a49c1
Implement Async getModel apis
azabbasi a5abc00
Rebase master, implement context
azabbasi cf7d079
Update DigitalTwinsAsyncClient.java
azabbasi 6954b7b
Address comments.
azabbasi 9f0dd67
Update comments.
azabbasi 6efdd3a
Implement deleteModels
azabbasi f8134cd
fix comments.
azabbasi c073688
Implement Sync APIs
azabbasi 36b8d0b
Implement contexts for sync client.
azabbasi acb56b2
Update comments. Remove API design doc for methods that have been imp…
azabbasi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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"); | ||
|
|
@@ -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) { | ||
|
azabbasi marked this conversation as resolved.
|
||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we even need to mention this return?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we will get doc verification errors. |
||
| */ | ||
| @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) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.