Skip to content

Commit

Permalink
Add implementation of DT Apis (#14671)
Browse files Browse the repository at this point in the history
  • Loading branch information
bikamani authored Sep 1, 2020
1 parent bf7b33c commit 49d7bb5
Show file tree
Hide file tree
Showing 5 changed files with 406 additions and 58 deletions.
59 changes: 6 additions & 53 deletions sdk/digitaltwins/azure-digitaltwins-core/API design.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,44 +400,21 @@ public Mono<Response<Void>> deleteDigitalTwinWithResponse(String digitalTwinId,
*
* @param digitalTwinId The Id of the digital twin.
* @param digitalTwinUpdateOperations The application/json-patch+json operations to be performed on the specified digital twin
* @return The updated application/json digital twin.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<String> updateDigitalTwin(String digitalTwinId, List<Object> digitalTwinUpdateOperations)

/**
* Updates a digital twin.
*
* @param digitalTwinId The Id of the digital twin.
* @param digitalTwinUpdateOperations The application/json-patch+json operations to be performed on the specified digital twin
* @param classType The model class to convert the response to.
* @return The updated application/json digital twin.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public <T> Mono<T> updateDigitalTwin(String digitalTwinId, List<Object> digitalTwinUpdateOperations, Class<T> classType)
public Mono<Void> updateDigitalTwin(String digitalTwinId, List<Object> digitalTwinUpdateOperations)

/**
* Updates a digital twin.
*
* @param digitalTwinId The Id of the digital twin.
* @param digitalTwinUpdateOperations The application/json-patch+json operations to be performed on the specified digital twin
* @param options The optional settings for this request
* @return A Http response containing updated application/json digital twin.
* @return A Http response
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<String>> updateDigitalTwinWithResponse(String digitalTwinId, List<Object> digitalTwinUpdateOperations, RequestOptions options)
public Mono<Response<Void>> updateDigitalTwinWithResponse(String digitalTwinId, List<Object> digitalTwinUpdateOperations, RequestOptions options)

/**
* Updates a digital twin.
*
* @param digitalTwinId The Id of the digital twin.
* @param digitalTwinUpdateOperations The application/json-patch+json operations to be performed on the specified digital twin
* @param classType The model class to convert the response to.
* @param options The optional settings for this request
* @return A Http response containing updated application/json digital twin.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public <T> Mono<Response<T>> updateDigitalTwinWithResponse(String digitalTwinId, List<Object> digitalTwinUpdateOperations, RequestOptions options, Class<T> classType)
```

</details>
Expand Down Expand Up @@ -555,21 +532,9 @@ public Response<Void> deleteDigitalTwinWithResponse(String digitalTwinId, Reques
*
* @param digitalTwinId The Id of the digital twin.
* @param digitalTwinUpdateOperations The application/json-patch+json operations to be performed on the specified digital twin
* @return The updated application/json digital twin.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public String updateDigitalTwin(String digitalTwinId, List<Object> digitalTwinUpdateOperations)

/**
* Updates a digital twin.
*
* @param digitalTwinId The Id of the digital twin.
* @param digitalTwinUpdateOperations The application/json-patch+json operations to be performed on the specified digital twin
* @param classType The model class to convert the response to.
* @return The updated application/json digital twin.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public <T> updateDigitalTwin(String digitalTwinId, List<Object> digitalTwinUpdateOperations, Class<T> classType)
public void updateDigitalTwin(String digitalTwinId, List<Object> digitalTwinUpdateOperations)

/**
* Updates a digital twin.
Expand All @@ -578,23 +543,11 @@ public <T> updateDigitalTwin(String digitalTwinId, List<Object> digitalTwinUpdat
* @param digitalTwinUpdateOperations The application/json-patch+json operations to be performed on the specified digital twin
* @param options The optional settings for this request
* @param context Additional context that is passed through the Http pipeline during the service call.
* @return A Http response containing updated application/json digital twin.
* @return A Http response
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<String> updateDigitalTwinWithResponse(String digitalTwinId, List<Object> digitalTwinUpdateOperations, RequestOptions options, Context context)
public Response<Void> updateDigitalTwinWithResponse(String digitalTwinId, List<Object> digitalTwinUpdateOperations, RequestOptions options, Context context)

/**
* Updates a digital twin.
*
* @param digitalTwinId The Id of the digital twin.
* @param digitalTwinUpdateOperations The application/json-patch+json operations to be performed on the specified digital twin
* @param options The optional settings for this request
* @param classType The model class to convert the response to.
* @param context Additional context that is passed through the Http pipeline during the service call.
* @return A Http response containing updated application/json digital twin.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public <T> Response<T> updateDigitalTwinWithResponse(String digitalTwinId, List<Object> digitalTwinUpdateOperations, RequestOptions options, Class<T> classType, Context context)
```
</details>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.digitaltwins.core;

/**
* Optional settings that are specific to calls to {@link DigitalTwinsClient#deleteDigitalTwin(String)} and its overloads.
*/
public class DeleteDigitalTwinRequestOptions extends RequestOptions {
// This class exists to be added to later if the deleteDigitalTwin APIs get a new optional parameter in later service
// API versions and so that we don't have to expose that new optional parameter for other APIs using RequestOptions

@Override
public DeleteDigitalTwinRequestOptions setIfMatch(String ifMatch) {
super.setIfMatch(ifMatch);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,27 @@ public HttpPipeline getHttpPipeline() {
return this.protocolLayer.getHttpPipeline();
}

// TODO: This is a temporary implementation for sample purposes. This should be spruced up/replaced once this API is actually designed.
// Input is String and output is Response<String>.
/**
* Creates a digital twin.
*
* @param digitalTwinId The Id of the digital twin.
* @param digitalTwin The application/json digital twin to create.
* @return The application/json string representing the digital twin created.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<String> createDigitalTwin(String digitalTwinId, String digitalTwin)
{
return createDigitalTwinWithResponse(digitalTwinId, digitalTwin)
.map(DigitalTwinsResponse::getValue);
}

/**
* Creates a digital twin.
*
* @param digitalTwinId The Id of the digital twin.
* @param digitalTwin The application/json digital twin to create.
* @return A {@link DigitalTwinsResponse} containing the application/json string representing the digital twin created.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DigitalTwinsResponse<String>> createDigitalTwinWithResponse(String digitalTwinId, String digitalTwin) {
return withContext(context -> createDigitalTwinWithResponse(digitalTwinId, digitalTwin, context));
Expand All @@ -108,14 +127,37 @@ Mono<DigitalTwinsResponse<String>> createDigitalTwinWithResponse(String digitalT
DigitalTwinsResponseHeaders twinHeaders = mapper.convertValue(response.getDeserializedHeaders(), DigitalTwinsResponseHeaders.class);
return Mono.just(new DigitalTwinsResponse<>(response.getRequest(), response.getStatusCode(), response.getHeaders(), jsonResponse, twinHeaders));
} catch (JsonProcessingException e) {
logger.error("JsonProcessingException occurred while creating a digital twin: ", e);
logger.error("JsonProcessingException occurred while serializing json object into string ", e);
return Mono.error(e);
}
});
}

// TODO: This is a temporary implementation for sample purposes. This should be spruced up/replaced once this API is actually designed.
// Input is Object and output is Response<T>.
/**
* Creates a digital twin.
*
* @param digitalTwinId The Id of the digital twin.
* @param digitalTwin The application/json digital twin to create.
* @param clazz The model class to deserialize the response with.
* @param <T> The generic type to deserialize the digital twin with.
* @return The deserialized application/json object representing the digital twin created.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public <T> Mono<T> createDigitalTwin(String digitalTwinId, Object digitalTwin, Class<T> clazz)
{
return createDigitalTwinWithResponse(digitalTwinId, digitalTwin, clazz)
.map(DigitalTwinsResponse::getValue);
}

/**
* Creates a digital twin.
*
* @param digitalTwinId The Id of the digital twin.
* @param digitalTwin The application/json digital twin to create.
* @param clazz The model class to deserialize the response with.
* @param <T> The generic type to deserialize the digital twin with.
* @return A {@link DigitalTwinsResponse} containing the deserialized application/json object representing the digital twin created.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public <T> Mono<DigitalTwinsResponse<T>> createDigitalTwinWithResponse(String digitalTwinId, Object digitalTwin, Class<T> clazz) {
return withContext(context -> createDigitalTwinWithResponse(digitalTwinId, digitalTwin, clazz, context));
Expand All @@ -132,6 +174,158 @@ <T> Mono<DigitalTwinsResponse<T>> createDigitalTwinWithResponse(String digitalTw
});
}

/**
* Gets a digital twin.
*
* @param digitalTwinId The Id of the digital twin. The Id is unique within the service and case sensitive.
* @return The application/json string representing the digital twin.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<String> getDigitalTwin(String digitalTwinId)
{
return getDigitalTwinWithResponse(digitalTwinId)
.map(DigitalTwinsResponse::getValue);
}

/**
* Gets a digital twin.
*
* @param digitalTwinId The Id of the digital twin. The Id is unique within the service and case sensitive.
* @return A {@link DigitalTwinsResponse} containing the application/json string representing the digital twin.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DigitalTwinsResponse<String>> getDigitalTwinWithResponse(String digitalTwinId)
{
return withContext(context -> getDigitalTwinWithResponse(digitalTwinId, context));
}

Mono<DigitalTwinsResponse<String>> getDigitalTwinWithResponse(String digitalTwinId, Context context) {
return protocolLayer
.getDigitalTwins()
.getByIdWithResponseAsync(digitalTwinId, context)
.flatMap(response -> {
try {
String jsonResponse = mapper.writeValueAsString(response.getValue());
DigitalTwinsResponseHeaders twinHeaders = mapper.convertValue(response.getDeserializedHeaders(), DigitalTwinsResponseHeaders.class);
return Mono.justOrEmpty(new DigitalTwinsResponse<>(response.getRequest(), response.getStatusCode(), response.getHeaders(), jsonResponse, twinHeaders));
} catch (JsonProcessingException e) {
logger.error("JsonProcessingException occurred while serializing json object into string: ", e);
return Mono.error(e);
}
});
}

/**
* Gets a digital twin.
*
* @param digitalTwinId The Id of the digital twin. The Id is unique within the service and case sensitive.
* @param clazz The model class to deserialize the response with.
* @param <T> The generic type to deserialize the digital twin with.
* @return The deserialized application/json object representing the digital twin
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public <T> Mono<T> getDigitalTwin(String digitalTwinId, Class<T> clazz)
{
return getDigitalTwinWithResponse(digitalTwinId, clazz)
.map(DigitalTwinsResponse::getValue);
}

/**
* Gets a digital twin.
*
* @param digitalTwinId The Id of the digital twin. The Id is unique within the service and case sensitive.
* @param clazz The model class to deserialize the response with.
* @param <T> The generic type to deserialize the digital twin with.
* @return A {@link DigitalTwinsResponse} containing the deserialized application/json object representing the digital twin.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public <T> Mono<DigitalTwinsResponse<T>> getDigitalTwinWithResponse(String digitalTwinId, Class<T> clazz)
{
return withContext(context -> getDigitalTwinWithResponse(digitalTwinId, clazz, context));
}

<T> Mono<DigitalTwinsResponse<T>> getDigitalTwinWithResponse(String digitalTwinId, Class<T> clazz, Context context) {
return protocolLayer
.getDigitalTwins()
.getByIdWithResponseAsync(digitalTwinId, context)
.map(response -> {
T genericResponse = mapper.convertValue(response.getValue(), clazz);
DigitalTwinsResponseHeaders twinHeaders = mapper.convertValue(response.getDeserializedHeaders(), DigitalTwinsResponseHeaders.class);
return new DigitalTwinsResponse<>(response.getRequest(), response.getStatusCode(), response.getHeaders(), genericResponse, twinHeaders);
});
}

/**
* Updates a digital twin.
*
* @param digitalTwinId The Id of the digital twin.
* @param digitalTwinUpdateOperations The application/json-patch+json operations to be performed on the specified digital twin
* @return An empty Mono
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Void> updateDigitalTwin(String digitalTwinId, List<Object> digitalTwinUpdateOperations)
{
return updateDigitalTwinWithResponse(digitalTwinId, digitalTwinUpdateOperations, new UpdateDigitalTwinRequestOptions())
.flatMap(voidResponse -> Mono.empty());
}

/**
* Updates a digital twin.
*
* @param digitalTwinId The Id of the digital twin.
* @param digitalTwinUpdateOperations The application/json-patch+json operations to be performed on the specified digital twin
* @param options The optional settings for this request
* @return A {@link DigitalTwinsResponse}
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DigitalTwinsResponse<Void>> updateDigitalTwinWithResponse(String digitalTwinId, List<Object> digitalTwinUpdateOperations, UpdateDigitalTwinRequestOptions options)
{
return withContext(context -> updateDigitalTwinWithResponse(digitalTwinId, digitalTwinUpdateOperations, options, context));
}

Mono<DigitalTwinsResponse<Void>> updateDigitalTwinWithResponse(String digitalTwinId, List<Object> digitalTwinUpdateOperations, UpdateDigitalTwinRequestOptions options, Context context) {
String ifMatch = options != null ? options.getIfMatch() : null;
return protocolLayer
.getDigitalTwins()
.updateWithResponseAsync(digitalTwinId, digitalTwinUpdateOperations, ifMatch, context)
.map(response -> {
DigitalTwinsResponseHeaders twinHeaders = mapper.convertValue(response.getDeserializedHeaders(), DigitalTwinsResponseHeaders.class);
return new DigitalTwinsResponse<>(response.getRequest(), response.getStatusCode(), response.getHeaders(), response.getValue(), twinHeaders);
});
}

/**
* Deletes a digital twin. All relationships referencing the digital twin must already be deleted.
* @param digitalTwinId The Id of the digital twin. The Id is unique within the service and case sensitive.
* @return An empty Mono
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Void> deleteDigitalTwin(String digitalTwinId)
{
return deleteDigitalTwinWithResponse(digitalTwinId, new DeleteDigitalTwinRequestOptions())
.flatMap(voidResponse -> Mono.empty());
}

/**
* Deletes a digital twin. All relationships referencing the digital twin must already be deleted.
*
* @param digitalTwinId The Id of the digital twin. The Id is unique within the service and case sensitive.
* @param options The optional settings for this request
* @return The Http response
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Void>> deleteDigitalTwinWithResponse(String digitalTwinId, DeleteDigitalTwinRequestOptions options)
{
return withContext(context -> deleteDigitalTwinWithResponse(digitalTwinId, options, context));
}

Mono<Response<Void>> deleteDigitalTwinWithResponse(String digitalTwinId, DeleteDigitalTwinRequestOptions options, Context context) {
String ifMatch = options != null ? options.getIfMatch() : null;
return protocolLayer
.getDigitalTwins()
.deleteWithResponseAsync(digitalTwinId, ifMatch, context);
}

/**
* Creates a relationship on a digital twin.
*
Expand Down
Loading

0 comments on commit 49d7bb5

Please sign in to comment.