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