| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Text.Json; |
| | 6 | |
|
| | 7 | | namespace Azure.DigitalTwins.Core.Serialization |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// A utility to create the application/json-patch+json operations payload required for update operations. |
| | 11 | | /// </summary> |
| | 12 | | /// <remarks> |
| | 13 | | /// For more samples, see <see href="https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/digitaltwins/Azure.D |
| | 14 | | /// </remarks> |
| | 15 | | /// <example> |
| | 16 | | /// <code snippet="Snippet:DigitalTwinsSampleUpdateComponent"> |
| | 17 | | /// // Update Component1 by replacing the property ComponentProp1 value, |
| | 18 | | /// // using an optional utility to build the payload. |
| | 19 | | /// var componentUpdateUtility = new UpdateOperationsUtility(); |
| | 20 | | /// componentUpdateUtility.AppendReplaceOp("/ComponentProp1", "Some new value"); |
| | 21 | | /// string updatePayload = componentUpdateUtility.Serialize(); |
| | 22 | | /// await client.UpdateComponentAsync(basicDtId, "Component1", updatePayload); |
| | 23 | | /// Console.WriteLine($"Updated component for digital twin '{basicDtId}'."); |
| | 24 | | /// </code> |
| | 25 | | /// </example> |
| | 26 | | public class UpdateOperationsUtility |
| | 27 | | { |
| | 28 | | private const string Op = "op"; |
| | 29 | | private const string Add = "add"; |
| | 30 | | private const string Replace = "replace"; |
| | 31 | | private const string Remove = "remove"; |
| | 32 | | private const string Path = "path"; |
| | 33 | | private const string Value = "value"; |
| | 34 | |
|
| 14 | 35 | | private readonly List<Dictionary<string, object>> _ops = new List<Dictionary<string, object>>(); |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Include an add operation. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="path">The path to the property to be added.</param> |
| | 41 | | /// <param name="value">The value to update to.</param> |
| | 42 | | public void AppendAddOp(string path, object value) |
| | 43 | | { |
| 6 | 44 | | var op = new Dictionary<string, object> |
| 6 | 45 | | { |
| 6 | 46 | | { Op, Add }, |
| 6 | 47 | | { Path, path }, |
| 6 | 48 | | { Value, value }, |
| 6 | 49 | | }; |
| 6 | 50 | | _ops.Add(op); |
| 6 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Include a replace operation. |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="path">The path to the property to be updated.</param> |
| | 57 | | /// <param name="value">The value to update to.</param> |
| | 58 | | public void AppendReplaceOp(string path, object value) |
| | 59 | | { |
| 14 | 60 | | var op = new Dictionary<string, object> |
| 14 | 61 | | { |
| 14 | 62 | | { Op, Replace }, |
| 14 | 63 | | { Path, path }, |
| 14 | 64 | | { Value, value }, |
| 14 | 65 | | }; |
| 14 | 66 | | _ops.Add(op); |
| 14 | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Include a remove operation. |
| | 71 | | /// </summary> |
| | 72 | | /// <param name="path">The path to the property to be added.</param> |
| | 73 | | public void AppendRemoveOp(string path) |
| | 74 | | { |
| 6 | 75 | | var op = new Dictionary<string, object> |
| 6 | 76 | | { |
| 6 | 77 | | { Op, Remove }, |
| 6 | 78 | | { Path, path }, |
| 6 | 79 | | }; |
| 6 | 80 | | _ops.Add(op); |
| 6 | 81 | | } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Serialize the constructed payload as json. |
| | 85 | | /// </summary> |
| | 86 | | /// <returns>A string of the json payload.</returns> |
| | 87 | | public string Serialize() |
| | 88 | | { |
| 14 | 89 | | return JsonSerializer.Serialize(_ops); |
| | 90 | | } |
| | 91 | | } |
| | 92 | | } |