| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | // <auto-generated/> |
| | 5 | |
|
| | 6 | | #nullable disable |
| | 7 | |
|
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Text.Json; |
| | 11 | | using System.Threading; |
| | 12 | | using System.Threading.Tasks; |
| | 13 | | using Azure; |
| | 14 | | using Azure.Core; |
| | 15 | | using Azure.Core.Pipeline; |
| | 16 | | using Azure.Iot.Hub.Service.Models; |
| | 17 | |
|
| | 18 | | namespace Azure.Iot.Hub.Service |
| | 19 | | { |
| | 20 | | internal partial class ModulesRestClient |
| | 21 | | { |
| | 22 | | private Uri endpoint; |
| | 23 | | private string apiVersion; |
| | 24 | | private ClientDiagnostics _clientDiagnostics; |
| | 25 | | private HttpPipeline _pipeline; |
| | 26 | |
|
| | 27 | | /// <summary> Initializes a new instance of ModulesRestClient. </summary> |
| | 28 | | /// <param name="clientDiagnostics"> The handler for diagnostic messaging in the client. </param> |
| | 29 | | /// <param name="pipeline"> The HTTP pipeline for sending and receiving REST requests and responses. </param> |
| | 30 | | /// <param name="endpoint"> server parameter. </param> |
| | 31 | | /// <param name="apiVersion"> Api Version. </param> |
| | 32 | | /// <exception cref="ArgumentNullException"> <paramref name="apiVersion"/> is null. </exception> |
| 76 | 33 | | public ModulesRestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri endpoint = null, string |
| | 34 | | { |
| 76 | 35 | | endpoint ??= new Uri("https://fully-qualified-iothubname.azure-devices.net"); |
| 76 | 36 | | if (apiVersion == null) |
| | 37 | | { |
| 0 | 38 | | throw new ArgumentNullException(nameof(apiVersion)); |
| | 39 | | } |
| | 40 | |
|
| 76 | 41 | | this.endpoint = endpoint; |
| 76 | 42 | | this.apiVersion = apiVersion; |
| 76 | 43 | | _clientDiagnostics = clientDiagnostics; |
| 76 | 44 | | _pipeline = pipeline; |
| 76 | 45 | | } |
| | 46 | |
|
| | 47 | | internal HttpMessage CreateGetTwinRequest(string id, string mid) |
| | 48 | | { |
| 12 | 49 | | var message = _pipeline.CreateMessage(); |
| 12 | 50 | | var request = message.Request; |
| 12 | 51 | | request.Method = RequestMethod.Get; |
| 12 | 52 | | var uri = new RawRequestUriBuilder(); |
| 12 | 53 | | uri.Reset(endpoint); |
| 12 | 54 | | uri.AppendPath("/twins/", false); |
| 12 | 55 | | uri.AppendPath(id, true); |
| 12 | 56 | | uri.AppendPath("/modules/", false); |
| 12 | 57 | | uri.AppendPath(mid, true); |
| 12 | 58 | | uri.AppendQuery("api-version", apiVersion, true); |
| 12 | 59 | | request.Uri = uri; |
| 12 | 60 | | return message; |
| | 61 | | } |
| | 62 | |
|
| | 63 | | /// <summary> Gets the module twin. See https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-device-twins f |
| | 64 | | /// <param name="id"> The unique identifier of the device. </param> |
| | 65 | | /// <param name="mid"> The unique identifier of the module. </param> |
| | 66 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 67 | | /// <exception cref="ArgumentNullException"> <paramref name="id"/> or <paramref name="mid"/> is null. </exceptio |
| | 68 | | public async Task<Response<TwinData>> GetTwinAsync(string id, string mid, CancellationToken cancellationToken = |
| | 69 | | { |
| 6 | 70 | | if (id == null) |
| | 71 | | { |
| 0 | 72 | | throw new ArgumentNullException(nameof(id)); |
| | 73 | | } |
| 6 | 74 | | if (mid == null) |
| | 75 | | { |
| 0 | 76 | | throw new ArgumentNullException(nameof(mid)); |
| | 77 | | } |
| | 78 | |
|
| 6 | 79 | | using var message = CreateGetTwinRequest(id, mid); |
| 6 | 80 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 6 | 81 | | switch (message.Response.Status) |
| | 82 | | { |
| | 83 | | case 200: |
| | 84 | | { |
| | 85 | | TwinData value = default; |
| 6 | 86 | | using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc |
| 6 | 87 | | value = TwinData.DeserializeTwinData(document.RootElement); |
| 6 | 88 | | return Response.FromValue(value, message.Response); |
| | 89 | | } |
| | 90 | | default: |
| 0 | 91 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 92 | | } |
| 6 | 93 | | } |
| | 94 | |
|
| | 95 | | /// <summary> Gets the module twin. See https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-device-twins f |
| | 96 | | /// <param name="id"> The unique identifier of the device. </param> |
| | 97 | | /// <param name="mid"> The unique identifier of the module. </param> |
| | 98 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 99 | | /// <exception cref="ArgumentNullException"> <paramref name="id"/> or <paramref name="mid"/> is null. </exceptio |
| | 100 | | public Response<TwinData> GetTwin(string id, string mid, CancellationToken cancellationToken = default) |
| | 101 | | { |
| 6 | 102 | | if (id == null) |
| | 103 | | { |
| 0 | 104 | | throw new ArgumentNullException(nameof(id)); |
| | 105 | | } |
| 6 | 106 | | if (mid == null) |
| | 107 | | { |
| 0 | 108 | | throw new ArgumentNullException(nameof(mid)); |
| | 109 | | } |
| | 110 | |
|
| 6 | 111 | | using var message = CreateGetTwinRequest(id, mid); |
| 6 | 112 | | _pipeline.Send(message, cancellationToken); |
| 6 | 113 | | switch (message.Response.Status) |
| | 114 | | { |
| | 115 | | case 200: |
| | 116 | | { |
| | 117 | | TwinData value = default; |
| 6 | 118 | | using var document = JsonDocument.Parse(message.Response.ContentStream); |
| 6 | 119 | | value = TwinData.DeserializeTwinData(document.RootElement); |
| 6 | 120 | | return Response.FromValue(value, message.Response); |
| | 121 | | } |
| | 122 | | default: |
| 0 | 123 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 124 | | } |
| 6 | 125 | | } |
| | 126 | |
|
| | 127 | | internal HttpMessage CreateReplaceTwinRequest(string id, string mid, TwinData deviceTwinInfo, string ifMatch) |
| | 128 | | { |
| 0 | 129 | | var message = _pipeline.CreateMessage(); |
| 0 | 130 | | var request = message.Request; |
| 0 | 131 | | request.Method = RequestMethod.Put; |
| 0 | 132 | | var uri = new RawRequestUriBuilder(); |
| 0 | 133 | | uri.Reset(endpoint); |
| 0 | 134 | | uri.AppendPath("/twins/", false); |
| 0 | 135 | | uri.AppendPath(id, true); |
| 0 | 136 | | uri.AppendPath("/modules/", false); |
| 0 | 137 | | uri.AppendPath(mid, true); |
| 0 | 138 | | uri.AppendQuery("api-version", apiVersion, true); |
| 0 | 139 | | request.Uri = uri; |
| 0 | 140 | | if (ifMatch != null) |
| | 141 | | { |
| 0 | 142 | | request.Headers.Add("If-Match", ifMatch); |
| | 143 | | } |
| 0 | 144 | | request.Headers.Add("Content-Type", "application/json"); |
| 0 | 145 | | var content = new Utf8JsonRequestContent(); |
| 0 | 146 | | content.JsonWriter.WriteObjectValue(deviceTwinInfo); |
| 0 | 147 | | request.Content = content; |
| 0 | 148 | | return message; |
| | 149 | | } |
| | 150 | |
|
| | 151 | | /// <summary> Replaces the tags and desired properties of a module twin. See https://docs.microsoft.com/azure/io |
| | 152 | | /// <param name="id"> The unique identifier of the device. </param> |
| | 153 | | /// <param name="mid"> The unique identifier of the module. </param> |
| | 154 | | /// <param name="deviceTwinInfo"> The module twin info that will replace the existing info. </param> |
| | 155 | | /// <param name="ifMatch"> The string representing a weak ETag for the device twin, as per RFC7232. It determine |
| | 156 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 157 | | /// <exception cref="ArgumentNullException"> <paramref name="id"/>, <paramref name="mid"/>, or <paramref name="d |
| | 158 | | public async Task<Response<TwinData>> ReplaceTwinAsync(string id, string mid, TwinData deviceTwinInfo, string if |
| | 159 | | { |
| 0 | 160 | | if (id == null) |
| | 161 | | { |
| 0 | 162 | | throw new ArgumentNullException(nameof(id)); |
| | 163 | | } |
| 0 | 164 | | if (mid == null) |
| | 165 | | { |
| 0 | 166 | | throw new ArgumentNullException(nameof(mid)); |
| | 167 | | } |
| 0 | 168 | | if (deviceTwinInfo == null) |
| | 169 | | { |
| 0 | 170 | | throw new ArgumentNullException(nameof(deviceTwinInfo)); |
| | 171 | | } |
| | 172 | |
|
| 0 | 173 | | using var message = CreateReplaceTwinRequest(id, mid, deviceTwinInfo, ifMatch); |
| 0 | 174 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 0 | 175 | | switch (message.Response.Status) |
| | 176 | | { |
| | 177 | | case 200: |
| | 178 | | { |
| | 179 | | TwinData value = default; |
| 0 | 180 | | using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc |
| 0 | 181 | | value = TwinData.DeserializeTwinData(document.RootElement); |
| 0 | 182 | | return Response.FromValue(value, message.Response); |
| | 183 | | } |
| | 184 | | default: |
| 0 | 185 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 186 | | } |
| 0 | 187 | | } |
| | 188 | |
|
| | 189 | | /// <summary> Replaces the tags and desired properties of a module twin. See https://docs.microsoft.com/azure/io |
| | 190 | | /// <param name="id"> The unique identifier of the device. </param> |
| | 191 | | /// <param name="mid"> The unique identifier of the module. </param> |
| | 192 | | /// <param name="deviceTwinInfo"> The module twin info that will replace the existing info. </param> |
| | 193 | | /// <param name="ifMatch"> The string representing a weak ETag for the device twin, as per RFC7232. It determine |
| | 194 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 195 | | /// <exception cref="ArgumentNullException"> <paramref name="id"/>, <paramref name="mid"/>, or <paramref name="d |
| | 196 | | public Response<TwinData> ReplaceTwin(string id, string mid, TwinData deviceTwinInfo, string ifMatch = null, Can |
| | 197 | | { |
| 0 | 198 | | if (id == null) |
| | 199 | | { |
| 0 | 200 | | throw new ArgumentNullException(nameof(id)); |
| | 201 | | } |
| 0 | 202 | | if (mid == null) |
| | 203 | | { |
| 0 | 204 | | throw new ArgumentNullException(nameof(mid)); |
| | 205 | | } |
| 0 | 206 | | if (deviceTwinInfo == null) |
| | 207 | | { |
| 0 | 208 | | throw new ArgumentNullException(nameof(deviceTwinInfo)); |
| | 209 | | } |
| | 210 | |
|
| 0 | 211 | | using var message = CreateReplaceTwinRequest(id, mid, deviceTwinInfo, ifMatch); |
| 0 | 212 | | _pipeline.Send(message, cancellationToken); |
| 0 | 213 | | switch (message.Response.Status) |
| | 214 | | { |
| | 215 | | case 200: |
| | 216 | | { |
| | 217 | | TwinData value = default; |
| 0 | 218 | | using var document = JsonDocument.Parse(message.Response.ContentStream); |
| 0 | 219 | | value = TwinData.DeserializeTwinData(document.RootElement); |
| 0 | 220 | | return Response.FromValue(value, message.Response); |
| | 221 | | } |
| | 222 | | default: |
| 0 | 223 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 224 | | } |
| 0 | 225 | | } |
| | 226 | |
|
| | 227 | | internal HttpMessage CreateUpdateTwinRequest(string id, string mid, TwinData deviceTwinInfo, string ifMatch) |
| | 228 | | { |
| 12 | 229 | | var message = _pipeline.CreateMessage(); |
| 12 | 230 | | var request = message.Request; |
| 12 | 231 | | request.Method = RequestMethod.Patch; |
| 12 | 232 | | var uri = new RawRequestUriBuilder(); |
| 12 | 233 | | uri.Reset(endpoint); |
| 12 | 234 | | uri.AppendPath("/twins/", false); |
| 12 | 235 | | uri.AppendPath(id, true); |
| 12 | 236 | | uri.AppendPath("/modules/", false); |
| 12 | 237 | | uri.AppendPath(mid, true); |
| 12 | 238 | | uri.AppendQuery("api-version", apiVersion, true); |
| 12 | 239 | | request.Uri = uri; |
| 12 | 240 | | if (ifMatch != null) |
| | 241 | | { |
| 12 | 242 | | request.Headers.Add("If-Match", ifMatch); |
| | 243 | | } |
| 12 | 244 | | request.Headers.Add("Content-Type", "application/json"); |
| 12 | 245 | | var content = new Utf8JsonRequestContent(); |
| 12 | 246 | | content.JsonWriter.WriteObjectValue(deviceTwinInfo); |
| 12 | 247 | | request.Content = content; |
| 12 | 248 | | return message; |
| | 249 | | } |
| | 250 | |
|
| | 251 | | /// <summary> Updates the tags and desired properties of a module twin. See https://docs.microsoft.com/azure/iot |
| | 252 | | /// <param name="id"> The unique identifier of the device. </param> |
| | 253 | | /// <param name="mid"> The unique identifier of the module. </param> |
| | 254 | | /// <param name="deviceTwinInfo"> The module twin info containing the tags and desired properties to be updated. |
| | 255 | | /// <param name="ifMatch"> The string representing a weak ETag for the device twin, as per RFC7232. It determine |
| | 256 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 257 | | /// <exception cref="ArgumentNullException"> <paramref name="id"/>, <paramref name="mid"/>, or <paramref name="d |
| | 258 | | public async Task<Response<TwinData>> UpdateTwinAsync(string id, string mid, TwinData deviceTwinInfo, string ifM |
| | 259 | | { |
| 6 | 260 | | if (id == null) |
| | 261 | | { |
| 0 | 262 | | throw new ArgumentNullException(nameof(id)); |
| | 263 | | } |
| 6 | 264 | | if (mid == null) |
| | 265 | | { |
| 0 | 266 | | throw new ArgumentNullException(nameof(mid)); |
| | 267 | | } |
| 6 | 268 | | if (deviceTwinInfo == null) |
| | 269 | | { |
| 0 | 270 | | throw new ArgumentNullException(nameof(deviceTwinInfo)); |
| | 271 | | } |
| | 272 | |
|
| 6 | 273 | | using var message = CreateUpdateTwinRequest(id, mid, deviceTwinInfo, ifMatch); |
| 6 | 274 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 6 | 275 | | switch (message.Response.Status) |
| | 276 | | { |
| | 277 | | case 200: |
| | 278 | | { |
| | 279 | | TwinData value = default; |
| 4 | 280 | | using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc |
| 4 | 281 | | value = TwinData.DeserializeTwinData(document.RootElement); |
| 4 | 282 | | return Response.FromValue(value, message.Response); |
| | 283 | | } |
| | 284 | | default: |
| 2 | 285 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 286 | | } |
| 4 | 287 | | } |
| | 288 | |
|
| | 289 | | /// <summary> Updates the tags and desired properties of a module twin. See https://docs.microsoft.com/azure/iot |
| | 290 | | /// <param name="id"> The unique identifier of the device. </param> |
| | 291 | | /// <param name="mid"> The unique identifier of the module. </param> |
| | 292 | | /// <param name="deviceTwinInfo"> The module twin info containing the tags and desired properties to be updated. |
| | 293 | | /// <param name="ifMatch"> The string representing a weak ETag for the device twin, as per RFC7232. It determine |
| | 294 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 295 | | /// <exception cref="ArgumentNullException"> <paramref name="id"/>, <paramref name="mid"/>, or <paramref name="d |
| | 296 | | public Response<TwinData> UpdateTwin(string id, string mid, TwinData deviceTwinInfo, string ifMatch = null, Canc |
| | 297 | | { |
| 6 | 298 | | if (id == null) |
| | 299 | | { |
| 0 | 300 | | throw new ArgumentNullException(nameof(id)); |
| | 301 | | } |
| 6 | 302 | | if (mid == null) |
| | 303 | | { |
| 0 | 304 | | throw new ArgumentNullException(nameof(mid)); |
| | 305 | | } |
| 6 | 306 | | if (deviceTwinInfo == null) |
| | 307 | | { |
| 0 | 308 | | throw new ArgumentNullException(nameof(deviceTwinInfo)); |
| | 309 | | } |
| | 310 | |
|
| 6 | 311 | | using var message = CreateUpdateTwinRequest(id, mid, deviceTwinInfo, ifMatch); |
| 6 | 312 | | _pipeline.Send(message, cancellationToken); |
| 6 | 313 | | switch (message.Response.Status) |
| | 314 | | { |
| | 315 | | case 200: |
| | 316 | | { |
| | 317 | | TwinData value = default; |
| 4 | 318 | | using var document = JsonDocument.Parse(message.Response.ContentStream); |
| 4 | 319 | | value = TwinData.DeserializeTwinData(document.RootElement); |
| 4 | 320 | | return Response.FromValue(value, message.Response); |
| | 321 | | } |
| | 322 | | default: |
| 2 | 323 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 324 | | } |
| 4 | 325 | | } |
| | 326 | |
|
| | 327 | | internal HttpMessage CreateGetModulesOnDeviceRequest(string id) |
| | 328 | | { |
| 4 | 329 | | var message = _pipeline.CreateMessage(); |
| 4 | 330 | | var request = message.Request; |
| 4 | 331 | | request.Method = RequestMethod.Get; |
| 4 | 332 | | var uri = new RawRequestUriBuilder(); |
| 4 | 333 | | uri.Reset(endpoint); |
| 4 | 334 | | uri.AppendPath("/devices/", false); |
| 4 | 335 | | uri.AppendPath(id, true); |
| 4 | 336 | | uri.AppendPath("/modules", false); |
| 4 | 337 | | uri.AppendQuery("api-version", apiVersion, true); |
| 4 | 338 | | request.Uri = uri; |
| 4 | 339 | | return message; |
| | 340 | | } |
| | 341 | |
|
| | 342 | | /// <summary> Gets all the module identities on the device. </summary> |
| | 343 | | /// <param name="id"> The unique identifier of the device. </param> |
| | 344 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 345 | | /// <exception cref="ArgumentNullException"> <paramref name="id"/> is null. </exception> |
| | 346 | | public async Task<Response<IReadOnlyList<ModuleIdentity>>> GetModulesOnDeviceAsync(string id, CancellationToken |
| | 347 | | { |
| 2 | 348 | | if (id == null) |
| | 349 | | { |
| 0 | 350 | | throw new ArgumentNullException(nameof(id)); |
| | 351 | | } |
| | 352 | |
|
| 2 | 353 | | using var message = CreateGetModulesOnDeviceRequest(id); |
| 2 | 354 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 2 | 355 | | switch (message.Response.Status) |
| | 356 | | { |
| | 357 | | case 200: |
| | 358 | | { |
| | 359 | | IReadOnlyList<ModuleIdentity> value = default; |
| 2 | 360 | | using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc |
| 2 | 361 | | List<ModuleIdentity> array = new List<ModuleIdentity>(); |
| 24 | 362 | | foreach (var item in document.RootElement.EnumerateArray()) |
| | 363 | | { |
| 10 | 364 | | array.Add(ModuleIdentity.DeserializeModuleIdentity(item)); |
| | 365 | | } |
| 2 | 366 | | value = array; |
| 2 | 367 | | return Response.FromValue(value, message.Response); |
| | 368 | | } |
| | 369 | | default: |
| 0 | 370 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 371 | | } |
| 2 | 372 | | } |
| | 373 | |
|
| | 374 | | /// <summary> Gets all the module identities on the device. </summary> |
| | 375 | | /// <param name="id"> The unique identifier of the device. </param> |
| | 376 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 377 | | /// <exception cref="ArgumentNullException"> <paramref name="id"/> is null. </exception> |
| | 378 | | public Response<IReadOnlyList<ModuleIdentity>> GetModulesOnDevice(string id, CancellationToken cancellationToken |
| | 379 | | { |
| 2 | 380 | | if (id == null) |
| | 381 | | { |
| 0 | 382 | | throw new ArgumentNullException(nameof(id)); |
| | 383 | | } |
| | 384 | |
|
| 2 | 385 | | using var message = CreateGetModulesOnDeviceRequest(id); |
| 2 | 386 | | _pipeline.Send(message, cancellationToken); |
| 2 | 387 | | switch (message.Response.Status) |
| | 388 | | { |
| | 389 | | case 200: |
| | 390 | | { |
| | 391 | | IReadOnlyList<ModuleIdentity> value = default; |
| 2 | 392 | | using var document = JsonDocument.Parse(message.Response.ContentStream); |
| 2 | 393 | | List<ModuleIdentity> array = new List<ModuleIdentity>(); |
| 24 | 394 | | foreach (var item in document.RootElement.EnumerateArray()) |
| | 395 | | { |
| 10 | 396 | | array.Add(ModuleIdentity.DeserializeModuleIdentity(item)); |
| | 397 | | } |
| 2 | 398 | | value = array; |
| 2 | 399 | | return Response.FromValue(value, message.Response); |
| | 400 | | } |
| | 401 | | default: |
| 0 | 402 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 403 | | } |
| 2 | 404 | | } |
| | 405 | |
|
| | 406 | | internal HttpMessage CreateGetIdentityRequest(string id, string mid) |
| | 407 | | { |
| 44 | 408 | | var message = _pipeline.CreateMessage(); |
| 44 | 409 | | var request = message.Request; |
| 44 | 410 | | request.Method = RequestMethod.Get; |
| 44 | 411 | | var uri = new RawRequestUriBuilder(); |
| 44 | 412 | | uri.Reset(endpoint); |
| 44 | 413 | | uri.AppendPath("/devices/", false); |
| 44 | 414 | | uri.AppendPath(id, true); |
| 44 | 415 | | uri.AppendPath("/modules/", false); |
| 44 | 416 | | uri.AppendPath(mid, true); |
| 44 | 417 | | uri.AppendQuery("api-version", apiVersion, true); |
| 44 | 418 | | request.Uri = uri; |
| 44 | 419 | | return message; |
| | 420 | | } |
| | 421 | |
|
| | 422 | | /// <summary> Gets a module identity on the device. </summary> |
| | 423 | | /// <param name="id"> The unique identifier of the device. </param> |
| | 424 | | /// <param name="mid"> The unique identifier of the module. </param> |
| | 425 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 426 | | /// <exception cref="ArgumentNullException"> <paramref name="id"/> or <paramref name="mid"/> is null. </exceptio |
| | 427 | | public async Task<Response<ModuleIdentity>> GetIdentityAsync(string id, string mid, CancellationToken cancellati |
| | 428 | | { |
| 22 | 429 | | if (id == null) |
| | 430 | | { |
| 0 | 431 | | throw new ArgumentNullException(nameof(id)); |
| | 432 | | } |
| 22 | 433 | | if (mid == null) |
| | 434 | | { |
| 0 | 435 | | throw new ArgumentNullException(nameof(mid)); |
| | 436 | | } |
| | 437 | |
|
| 22 | 438 | | using var message = CreateGetIdentityRequest(id, mid); |
| 22 | 439 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 22 | 440 | | switch (message.Response.Status) |
| | 441 | | { |
| | 442 | | case 200: |
| | 443 | | { |
| | 444 | | ModuleIdentity value = default; |
| 22 | 445 | | using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc |
| 22 | 446 | | value = ModuleIdentity.DeserializeModuleIdentity(document.RootElement); |
| 22 | 447 | | return Response.FromValue(value, message.Response); |
| | 448 | | } |
| | 449 | | default: |
| 0 | 450 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 451 | | } |
| 22 | 452 | | } |
| | 453 | |
|
| | 454 | | /// <summary> Gets a module identity on the device. </summary> |
| | 455 | | /// <param name="id"> The unique identifier of the device. </param> |
| | 456 | | /// <param name="mid"> The unique identifier of the module. </param> |
| | 457 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 458 | | /// <exception cref="ArgumentNullException"> <paramref name="id"/> or <paramref name="mid"/> is null. </exceptio |
| | 459 | | public Response<ModuleIdentity> GetIdentity(string id, string mid, CancellationToken cancellationToken = default |
| | 460 | | { |
| 22 | 461 | | if (id == null) |
| | 462 | | { |
| 0 | 463 | | throw new ArgumentNullException(nameof(id)); |
| | 464 | | } |
| 22 | 465 | | if (mid == null) |
| | 466 | | { |
| 0 | 467 | | throw new ArgumentNullException(nameof(mid)); |
| | 468 | | } |
| | 469 | |
|
| 22 | 470 | | using var message = CreateGetIdentityRequest(id, mid); |
| 22 | 471 | | _pipeline.Send(message, cancellationToken); |
| 22 | 472 | | switch (message.Response.Status) |
| | 473 | | { |
| | 474 | | case 200: |
| | 475 | | { |
| | 476 | | ModuleIdentity value = default; |
| 22 | 477 | | using var document = JsonDocument.Parse(message.Response.ContentStream); |
| 22 | 478 | | value = ModuleIdentity.DeserializeModuleIdentity(document.RootElement); |
| 22 | 479 | | return Response.FromValue(value, message.Response); |
| | 480 | | } |
| | 481 | | default: |
| 0 | 482 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 483 | | } |
| 22 | 484 | | } |
| | 485 | |
|
| | 486 | | internal HttpMessage CreateCreateOrUpdateIdentityRequest(string id, string mid, ModuleIdentity module, string if |
| | 487 | | { |
| 92 | 488 | | var message = _pipeline.CreateMessage(); |
| 92 | 489 | | var request = message.Request; |
| 92 | 490 | | request.Method = RequestMethod.Put; |
| 92 | 491 | | var uri = new RawRequestUriBuilder(); |
| 92 | 492 | | uri.Reset(endpoint); |
| 92 | 493 | | uri.AppendPath("/devices/", false); |
| 92 | 494 | | uri.AppendPath(id, true); |
| 92 | 495 | | uri.AppendPath("/modules/", false); |
| 92 | 496 | | uri.AppendPath(mid, true); |
| 92 | 497 | | uri.AppendQuery("api-version", apiVersion, true); |
| 92 | 498 | | request.Uri = uri; |
| 92 | 499 | | if (ifMatch != null) |
| | 500 | | { |
| 92 | 501 | | request.Headers.Add("If-Match", ifMatch); |
| | 502 | | } |
| 92 | 503 | | request.Headers.Add("Content-Type", "application/json"); |
| 92 | 504 | | var content = new Utf8JsonRequestContent(); |
| 92 | 505 | | content.JsonWriter.WriteObjectValue(module); |
| 92 | 506 | | request.Content = content; |
| 92 | 507 | | return message; |
| | 508 | | } |
| | 509 | |
|
| | 510 | | /// <summary> Creates or updates the module identity for a device in the IoT Hub. The moduleId and generationId |
| | 511 | | /// <param name="id"> The unique identifier of the device. </param> |
| | 512 | | /// <param name="mid"> The unique identifier of the module. </param> |
| | 513 | | /// <param name="module"> The module identity. </param> |
| | 514 | | /// <param name="ifMatch"> The string representing a weak ETag for the module, as per RFC7232. Should not be set |
| | 515 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 516 | | /// <exception cref="ArgumentNullException"> <paramref name="id"/>, <paramref name="mid"/>, or <paramref name="m |
| | 517 | | public async Task<Response<ModuleIdentity>> CreateOrUpdateIdentityAsync(string id, string mid, ModuleIdentity mo |
| | 518 | | { |
| 46 | 519 | | if (id == null) |
| | 520 | | { |
| 0 | 521 | | throw new ArgumentNullException(nameof(id)); |
| | 522 | | } |
| 46 | 523 | | if (mid == null) |
| | 524 | | { |
| 0 | 525 | | throw new ArgumentNullException(nameof(mid)); |
| | 526 | | } |
| 46 | 527 | | if (module == null) |
| | 528 | | { |
| 0 | 529 | | throw new ArgumentNullException(nameof(module)); |
| | 530 | | } |
| | 531 | |
|
| 46 | 532 | | using var message = CreateCreateOrUpdateIdentityRequest(id, mid, module, ifMatch); |
| 46 | 533 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 46 | 534 | | switch (message.Response.Status) |
| | 535 | | { |
| | 536 | | case 200: |
| | 537 | | case 201: |
| | 538 | | { |
| | 539 | | ModuleIdentity value = default; |
| 44 | 540 | | using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc |
| 44 | 541 | | value = ModuleIdentity.DeserializeModuleIdentity(document.RootElement); |
| 44 | 542 | | return Response.FromValue(value, message.Response); |
| | 543 | | } |
| | 544 | | default: |
| 2 | 545 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 546 | | } |
| 44 | 547 | | } |
| | 548 | |
|
| | 549 | | /// <summary> Creates or updates the module identity for a device in the IoT Hub. The moduleId and generationId |
| | 550 | | /// <param name="id"> The unique identifier of the device. </param> |
| | 551 | | /// <param name="mid"> The unique identifier of the module. </param> |
| | 552 | | /// <param name="module"> The module identity. </param> |
| | 553 | | /// <param name="ifMatch"> The string representing a weak ETag for the module, as per RFC7232. Should not be set |
| | 554 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 555 | | /// <exception cref="ArgumentNullException"> <paramref name="id"/>, <paramref name="mid"/>, or <paramref name="m |
| | 556 | | public Response<ModuleIdentity> CreateOrUpdateIdentity(string id, string mid, ModuleIdentity module, string ifMa |
| | 557 | | { |
| 46 | 558 | | if (id == null) |
| | 559 | | { |
| 0 | 560 | | throw new ArgumentNullException(nameof(id)); |
| | 561 | | } |
| 46 | 562 | | if (mid == null) |
| | 563 | | { |
| 0 | 564 | | throw new ArgumentNullException(nameof(mid)); |
| | 565 | | } |
| 46 | 566 | | if (module == null) |
| | 567 | | { |
| 0 | 568 | | throw new ArgumentNullException(nameof(module)); |
| | 569 | | } |
| | 570 | |
|
| 46 | 571 | | using var message = CreateCreateOrUpdateIdentityRequest(id, mid, module, ifMatch); |
| 46 | 572 | | _pipeline.Send(message, cancellationToken); |
| 46 | 573 | | switch (message.Response.Status) |
| | 574 | | { |
| | 575 | | case 200: |
| | 576 | | case 201: |
| | 577 | | { |
| | 578 | | ModuleIdentity value = default; |
| 44 | 579 | | using var document = JsonDocument.Parse(message.Response.ContentStream); |
| 44 | 580 | | value = ModuleIdentity.DeserializeModuleIdentity(document.RootElement); |
| 44 | 581 | | return Response.FromValue(value, message.Response); |
| | 582 | | } |
| | 583 | | default: |
| 2 | 584 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 585 | | } |
| 44 | 586 | | } |
| | 587 | |
|
| | 588 | | internal HttpMessage CreateDeleteIdentityRequest(string id, string mid, string ifMatch) |
| | 589 | | { |
| 0 | 590 | | var message = _pipeline.CreateMessage(); |
| 0 | 591 | | var request = message.Request; |
| 0 | 592 | | request.Method = RequestMethod.Delete; |
| 0 | 593 | | var uri = new RawRequestUriBuilder(); |
| 0 | 594 | | uri.Reset(endpoint); |
| 0 | 595 | | uri.AppendPath("/devices/", false); |
| 0 | 596 | | uri.AppendPath(id, true); |
| 0 | 597 | | uri.AppendPath("/modules/", false); |
| 0 | 598 | | uri.AppendPath(mid, true); |
| 0 | 599 | | uri.AppendQuery("api-version", apiVersion, true); |
| 0 | 600 | | request.Uri = uri; |
| 0 | 601 | | if (ifMatch != null) |
| | 602 | | { |
| 0 | 603 | | request.Headers.Add("If-Match", ifMatch); |
| | 604 | | } |
| 0 | 605 | | return message; |
| | 606 | | } |
| | 607 | |
|
| | 608 | | /// <summary> Deletes the module identity for a device in the IoT Hub. </summary> |
| | 609 | | /// <param name="id"> The unique identifier of the deivce. </param> |
| | 610 | | /// <param name="mid"> The unique identifier of the module. </param> |
| | 611 | | /// <param name="ifMatch"> The string representing a weak ETag for the module, as per RFC7232. The delete operat |
| | 612 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 613 | | /// <exception cref="ArgumentNullException"> <paramref name="id"/> or <paramref name="mid"/> is null. </exceptio |
| | 614 | | public async Task<Response> DeleteIdentityAsync(string id, string mid, string ifMatch = null, CancellationToken |
| | 615 | | { |
| 0 | 616 | | if (id == null) |
| | 617 | | { |
| 0 | 618 | | throw new ArgumentNullException(nameof(id)); |
| | 619 | | } |
| 0 | 620 | | if (mid == null) |
| | 621 | | { |
| 0 | 622 | | throw new ArgumentNullException(nameof(mid)); |
| | 623 | | } |
| | 624 | |
|
| 0 | 625 | | using var message = CreateDeleteIdentityRequest(id, mid, ifMatch); |
| 0 | 626 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 0 | 627 | | switch (message.Response.Status) |
| | 628 | | { |
| | 629 | | case 204: |
| 0 | 630 | | return message.Response; |
| | 631 | | default: |
| 0 | 632 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 633 | | } |
| 0 | 634 | | } |
| | 635 | |
|
| | 636 | | /// <summary> Deletes the module identity for a device in the IoT Hub. </summary> |
| | 637 | | /// <param name="id"> The unique identifier of the deivce. </param> |
| | 638 | | /// <param name="mid"> The unique identifier of the module. </param> |
| | 639 | | /// <param name="ifMatch"> The string representing a weak ETag for the module, as per RFC7232. The delete operat |
| | 640 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 641 | | /// <exception cref="ArgumentNullException"> <paramref name="id"/> or <paramref name="mid"/> is null. </exceptio |
| | 642 | | public Response DeleteIdentity(string id, string mid, string ifMatch = null, CancellationToken cancellationToken |
| | 643 | | { |
| 0 | 644 | | if (id == null) |
| | 645 | | { |
| 0 | 646 | | throw new ArgumentNullException(nameof(id)); |
| | 647 | | } |
| 0 | 648 | | if (mid == null) |
| | 649 | | { |
| 0 | 650 | | throw new ArgumentNullException(nameof(mid)); |
| | 651 | | } |
| | 652 | |
|
| 0 | 653 | | using var message = CreateDeleteIdentityRequest(id, mid, ifMatch); |
| 0 | 654 | | _pipeline.Send(message, cancellationToken); |
| 0 | 655 | | switch (message.Response.Status) |
| | 656 | | { |
| | 657 | | case 204: |
| 0 | 658 | | return message.Response; |
| | 659 | | default: |
| 0 | 660 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 661 | | } |
| 0 | 662 | | } |
| | 663 | |
|
| | 664 | | internal HttpMessage CreateInvokeMethodRequest(string deviceId, string moduleId, CloudToDeviceMethodRequest dire |
| | 665 | | { |
| 0 | 666 | | var message = _pipeline.CreateMessage(); |
| 0 | 667 | | var request = message.Request; |
| 0 | 668 | | request.Method = RequestMethod.Post; |
| 0 | 669 | | var uri = new RawRequestUriBuilder(); |
| 0 | 670 | | uri.Reset(endpoint); |
| 0 | 671 | | uri.AppendPath("/twins/", false); |
| 0 | 672 | | uri.AppendPath(deviceId, true); |
| 0 | 673 | | uri.AppendPath("/modules/", false); |
| 0 | 674 | | uri.AppendPath(moduleId, true); |
| 0 | 675 | | uri.AppendPath("/methods", false); |
| 0 | 676 | | uri.AppendQuery("api-version", apiVersion, true); |
| 0 | 677 | | request.Uri = uri; |
| 0 | 678 | | request.Headers.Add("Content-Type", "application/json"); |
| 0 | 679 | | var content = new Utf8JsonRequestContent(); |
| 0 | 680 | | content.JsonWriter.WriteObjectValue(directMethodRequest); |
| 0 | 681 | | request.Content = content; |
| 0 | 682 | | return message; |
| | 683 | | } |
| | 684 | |
|
| | 685 | | /// <summary> Invokes a direct method on a module of a device. See https://docs.microsoft.com/azure/iot-hub/iot- |
| | 686 | | /// <param name="deviceId"> The unique identifier of the device. </param> |
| | 687 | | /// <param name="moduleId"> The unique identifier of the module. </param> |
| | 688 | | /// <param name="directMethodRequest"> The parameters to execute a direct method on the module. </param> |
| | 689 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 690 | | /// <exception cref="ArgumentNullException"> <paramref name="deviceId"/>, <paramref name="moduleId"/>, or <param |
| | 691 | | public async Task<Response<CloudToDeviceMethodResponse>> InvokeMethodAsync(string deviceId, string moduleId, Clo |
| | 692 | | { |
| 0 | 693 | | if (deviceId == null) |
| | 694 | | { |
| 0 | 695 | | throw new ArgumentNullException(nameof(deviceId)); |
| | 696 | | } |
| 0 | 697 | | if (moduleId == null) |
| | 698 | | { |
| 0 | 699 | | throw new ArgumentNullException(nameof(moduleId)); |
| | 700 | | } |
| 0 | 701 | | if (directMethodRequest == null) |
| | 702 | | { |
| 0 | 703 | | throw new ArgumentNullException(nameof(directMethodRequest)); |
| | 704 | | } |
| | 705 | |
|
| 0 | 706 | | using var message = CreateInvokeMethodRequest(deviceId, moduleId, directMethodRequest); |
| 0 | 707 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 0 | 708 | | switch (message.Response.Status) |
| | 709 | | { |
| | 710 | | case 200: |
| | 711 | | { |
| | 712 | | CloudToDeviceMethodResponse value = default; |
| 0 | 713 | | using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc |
| 0 | 714 | | value = CloudToDeviceMethodResponse.DeserializeCloudToDeviceMethodResponse(document.RootElement) |
| 0 | 715 | | return Response.FromValue(value, message.Response); |
| | 716 | | } |
| | 717 | | default: |
| 0 | 718 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 719 | | } |
| 0 | 720 | | } |
| | 721 | |
|
| | 722 | | /// <summary> Invokes a direct method on a module of a device. See https://docs.microsoft.com/azure/iot-hub/iot- |
| | 723 | | /// <param name="deviceId"> The unique identifier of the device. </param> |
| | 724 | | /// <param name="moduleId"> The unique identifier of the module. </param> |
| | 725 | | /// <param name="directMethodRequest"> The parameters to execute a direct method on the module. </param> |
| | 726 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 727 | | /// <exception cref="ArgumentNullException"> <paramref name="deviceId"/>, <paramref name="moduleId"/>, or <param |
| | 728 | | public Response<CloudToDeviceMethodResponse> InvokeMethod(string deviceId, string moduleId, CloudToDeviceMethodR |
| | 729 | | { |
| 0 | 730 | | if (deviceId == null) |
| | 731 | | { |
| 0 | 732 | | throw new ArgumentNullException(nameof(deviceId)); |
| | 733 | | } |
| 0 | 734 | | if (moduleId == null) |
| | 735 | | { |
| 0 | 736 | | throw new ArgumentNullException(nameof(moduleId)); |
| | 737 | | } |
| 0 | 738 | | if (directMethodRequest == null) |
| | 739 | | { |
| 0 | 740 | | throw new ArgumentNullException(nameof(directMethodRequest)); |
| | 741 | | } |
| | 742 | |
|
| 0 | 743 | | using var message = CreateInvokeMethodRequest(deviceId, moduleId, directMethodRequest); |
| 0 | 744 | | _pipeline.Send(message, cancellationToken); |
| 0 | 745 | | switch (message.Response.Status) |
| | 746 | | { |
| | 747 | | case 200: |
| | 748 | | { |
| | 749 | | CloudToDeviceMethodResponse value = default; |
| 0 | 750 | | using var document = JsonDocument.Parse(message.Response.ContentStream); |
| 0 | 751 | | value = CloudToDeviceMethodResponse.DeserializeCloudToDeviceMethodResponse(document.RootElement) |
| 0 | 752 | | return Response.FromValue(value, message.Response); |
| | 753 | | } |
| | 754 | | default: |
| 0 | 755 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 756 | | } |
| 0 | 757 | | } |
| | 758 | | } |
| | 759 | | } |