Skip to content

Commit 8da4b10

Browse files
authored
Update sample snippets to match the latest API updates. (Azure#17381)
1 parent 2abad16 commit 8da4b10

File tree

1 file changed

+31
-38
lines changed
  • sdk/digitaltwins/azure-digitaltwins-core/src/samples

1 file changed

+31
-38
lines changed

sdk/digitaltwins/azure-digitaltwins-core/src/samples/README.md

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Check out sample models [here](https://github.com/Azure/azure-sdk-for-java/tree/
127127
Example of using sync client to create models.
128128

129129
```java
130-
List<String> modelsList = new ArrayList<>(Arrays.asList(newComponentModelPayload, newModelPayload));
130+
List<String> modelsList = Arrays.asList(newComponentModelPayload, newModelPayload);
131131
List<DigitalTwinsModelData> modelList = syncClient.createModels(modelsList);
132132

133133
for (DigitalTwinsModelData model : modelList) {
@@ -163,10 +163,10 @@ asynClient.listModels()
163163
Use `getModel` with model's unique identifier to get a specific model.
164164

165165
```java
166-
asyncClient.createOrReplaceDigitalTwin(twinId, twinContent)
166+
asyncClient.getModel(modelId)
167167
.subscribe(
168-
twin -> System.out.println("Created digital twin: " + twinId + "\n\t Body: " + twin),
169-
throwable -> System.out.println("Could not create digital twin " + twinId + " due to " + throwable)
168+
model -> System.out.println("Retrieved model with Id: " + model.getModelId()),
169+
throwable -> System.out.println("Could not get model " + modelId + " due to " + throwable)
170170
)
171171
```
172172

@@ -197,22 +197,20 @@ One option is to use the provided class BasicDigitalTwin for serialization and d
197197
```java
198198
// Create digital twin with component payload using the BasicDigitalTwin serialization helper
199199

200-
BasicDigitalTwin basicTwin = new BasicDigitalTwin()
201-
.setId(basicDigitalTwinId)
200+
BasicDigitalTwin basicTwin = new BasicDigitalTwin(basicDigitalTwinId)
202201
.setMetadata(
203-
new DigitalTwinMetadata()
202+
new BasicDigitalTwinMetadata()
204203
.setModelId(modelId)
205204
)
206205
.addToContents("Prop1", "Value1")
207206
.addToContents("Prop2", 987)
208207
.addToContents(
209208
"Component1",
210209
new BasicDigitalTwinComponent()
211-
.addroperty("ComponentProp1", "Component value 1")
212-
.addroperty("ComponentProp2", 123)
213-
);
210+
.addToContents("ComponentProp1", "Component value 1")
211+
.addToContents("ComponentProp2", 123));
214212

215-
BasicDigitalTwin basicTwinResponse = syncClient.createOrReplaceDigitalTwin(basicDtId, basicTwin, BasicDigitalTwin.class);
213+
BasicDigitalTwin basicTwinResponse = syncClient.createOrReplaceDigitalTwin(basicDigitalTwinId, basicTwin, BasicDigitalTwin.class);
216214
```
217215

218216
Alternatively, you can create your own custom data types to serialize and deserialize your digital twins.
@@ -281,12 +279,11 @@ To update a component or in other words to replace, remove and/or add a componen
281279

282280
```C# Snippet:DigitalTwinsSampleUpdateComponent
283281
// Update Component1 by replacing the property ComponentProp1 value,
284-
// using the UpdateOperationUtility to build the payload.
285-
UpdateOperationUtility jsonPatchDocument = new UpdateOperationUtility();
282+
// using the JsonPatchDocument to build the update operation patch document.
283+
JsonPatchDocument updateOp = new JsonPatchDocument()
284+
.appendReplace("/ComponentProp1", "Some new Value");
286285

287-
jsonPatchDocument.appendReplaceOperation("/ComponentProp1", "Some new Value");
288-
289-
client.updateComponent(basicDigitalTwinId, "Component1", jsonPatchDocument.getUpdateOperations());
286+
client.updateComponent(basicDigitalTwinId, "Component1", updateOp);
290287
```
291288

292289
### Get digital twin components
@@ -306,15 +303,20 @@ String getComponentResponse = client.getComponent(digitalTwinId, "Component1", S
306303
One option is to use the provided class BasicRelationship for serialization and deserialization.
307304

308305
```java
309-
BasicRelationship buildingFloorRelationshipPayload = new BasicRelationship()
310-
.setId(buildingFloorRelationshipId)
311-
.setSourceId(buildingTwinId)
312-
.setTargetId(floorTwinId)
313-
.setName("contains")
306+
BasicRelationship buildingToFloorBasicRelationship =
307+
new BasicRelationship(
308+
"myRelationshipId",
309+
"mySourceDigitalTwinId",
310+
"myTargetDigitalTwinId",
311+
"contains")
314312
.addProperty("Prop1", "Prop1 value")
315313
.addProperty("Prop2", 6);
316314

317-
client.createOrReplaceRelationship(buildingTwinId, buildingFloorRelationshipId, buildingFloorRelationshipPayload, BasicRelationship.class);
315+
BasicRelationship createdRelationship = client.createOrReplaceRelationship(
316+
"mySourceDigitalTwinId",
317+
"myRelationshipId",
318+
buildingToFloorBasicRelationship,
319+
BasicRelationship.class);
318320
```
319321

320322
### Get and deserialize a digital twin relationship
@@ -383,12 +385,10 @@ client.deleteRelationship(buildingTwinId, buildingFloorRelationshipId);
383385
To create an event route, provide an Id of an event route such as "sampleEventRoute" and event route data containing the endpoint and optional filter like the example shown below.
384386

385387
```java
386-
String filter = "$eventType = 'DigitalTwinTelemetryMessages' or $eventType = 'DigitalTwinLifecycleNotification'";
387-
DigitalTwinsEventRoute eventRoute = new EventRoute();
388-
eventRoute.setEndpointName(eventRouteEndpointName);
389-
eventRoute.setFilter(filter);
388+
String filter ="$eventType = 'DigitalTwinTelemetryMessages' or $eventType = 'DigitalTwinLifecycleNotification'";
390389

391-
client.createOrReplaceEventRoute(eventRouteId, eventRoute);
390+
DigitalTwinsEventRoute eventRoute = new DigitalTwinsEventRoute("myEndpointName").setFilter(filter);
391+
client.createOrReplaceEventRoute("myEventRouteId", eventRoute);
392392
```
393393

394394
For more information on the event route filter language, see the "how to manage routes" [filter events documentation](https://docs.microsoft.com/azure/digital-twins/how-to-manage-routes-apis-cli#filter-events).
@@ -398,17 +398,10 @@ For more information on the event route filter language, see the "how to manage
398398
List a specific event route given event route Id or all event routes setting options with `GetEventRouteAsync` and `GetEventRoutesAsync`.
399399

400400
```java
401-
PagedIterable<DigitalTwinsEventRoute> eventRoutes = client.listEventRoutes();
402-
403-
for (DigitalTwinsEventRoute eventRoute : eventRoutes) {
404-
existingEventRouteId = eventRoute.getId();
405-
System.out.println(String.format("\tEventRouteId: %s", eventRoute.getId()));
406-
System.out.println(String.format("\tEventRouteEndpointName: %s", eventRoute.getEndpointName()));
407-
if (eventRoute.getFilter() != null)
408-
{
409-
System.out.println(String.format("\tFilter: %s", eventRoute.getFilter()));
410-
}
411-
}
401+
PagedIterable<DigitalTwinsEventRoute> listResponse = client.listEventRoutes();
402+
403+
listResponse.forEach(
404+
eventRoute -> System.out.println("Retrieved event route with Id: " + eventRoute.getEventRouteId()));
412405
```
413406

414407
### Delete event routes

0 commit comments

Comments
 (0)