< Summary

Class:Azure.Iot.Hub.Service.DevicesRestClient
Assembly:Azure.Iot.Hub.Service
File(s):C:\Git\azure-sdk-for-net\sdk\iot\Azure.Iot.Hub.Service\src\Generated\DevicesRestClient.cs
Covered lines:186
Uncovered lines:139
Coverable lines:325
Total lines:741
Line coverage:57.2% (186 of 325)
Covered branches:42
Total branches:108
Branch coverage:38.8% (42 of 108)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-88.89%50%
CreateGetDevicesRequest(...)-0%0%
GetDevicesAsync()-0%0%
GetDevices(...)-0%0%
CreateBulkRegistryOperationsRequest(...)-100%100%
BulkRegistryOperationsAsync()-80%50%
BulkRegistryOperations(...)-80%50%
CreateGetDeviceRequest(...)-100%100%
GetDeviceAsync()-80%50%
GetDevice(...)-80%50%
CreateCreateOrUpdateIdentityRequest(...)-100%100%
CreateOrUpdateIdentityAsync()-83.33%66.67%
CreateOrUpdateIdentity(...)-83.33%66.67%
CreateDeleteIdentityRequest(...)-100%100%
DeleteIdentityAsync()-75%50%
DeleteIdentity(...)-75%50%
CreateGetTwinRequest(...)-100%100%
GetTwinAsync()-80%50%
GetTwin(...)-80%50%
CreateReplaceTwinRequest(...)-0%0%
ReplaceTwinAsync()-0%0%
ReplaceTwin(...)-0%0%
CreateUpdateTwinRequest(...)-100%100%
UpdateTwinAsync()-75%50%
UpdateTwin(...)-75%50%
CreateInvokeMethodRequest(...)-0%100%
InvokeMethodAsync()-0%0%
InvokeMethod(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\iot\Azure.Iot.Hub.Service\src\Generated\DevicesRestClient.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4// <auto-generated/>
 5
 6#nullable disable
 7
 8using System;
 9using System.Collections.Generic;
 10using System.Text.Json;
 11using System.Threading;
 12using System.Threading.Tasks;
 13using Azure;
 14using Azure.Core;
 15using Azure.Core.Pipeline;
 16using Azure.Iot.Hub.Service.Models;
 17
 18namespace 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>
 7633        public DevicesRestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri endpoint = null, string
 34        {
 7635            endpoint ??= new Uri("https://fully-qualified-iothubname.azure-devices.net");
 7636            if (apiVersion == null)
 37            {
 038                throw new ArgumentNullException(nameof(apiVersion));
 39            }
 40
 7641            this.endpoint = endpoint;
 7642            this.apiVersion = apiVersion;
 7643            _clientDiagnostics = clientDiagnostics;
 7644            _pipeline = pipeline;
 7645        }
 46
 47        internal HttpMessage CreateGetDevicesRequest(int? top)
 48        {
 049            var message = _pipeline.CreateMessage();
 050            var request = message.Request;
 051            request.Method = RequestMethod.Get;
 052            var uri = new RawRequestUriBuilder();
 053            uri.Reset(endpoint);
 054            uri.AppendPath("/devices", false);
 055            if (top != null)
 56            {
 057                uri.AppendQuery("top", top.Value, true);
 58            }
 059            uri.AppendQuery("api-version", apiVersion, true);
 060            request.Uri = uri;
 061            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        {
 069            using var message = CreateGetDevicesRequest(top);
 070            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
 071            switch (message.Response.Status)
 72            {
 73                case 200:
 74                    {
 75                        IReadOnlyList<DeviceIdentity> value = default;
 076                        using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc
 077                        List<DeviceIdentity> array = new List<DeviceIdentity>();
 078                        foreach (var item in document.RootElement.EnumerateArray())
 79                        {
 080                            array.Add(DeviceIdentity.DeserializeDeviceIdentity(item));
 81                        }
 082                        value = array;
 083                        return Response.FromValue(value, message.Response);
 84                    }
 85                default:
 086                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa
 87            }
 088        }
 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        {
 095            using var message = CreateGetDevicesRequest(top);
 096            _pipeline.Send(message, cancellationToken);
 097            switch (message.Response.Status)
 98            {
 99                case 200:
 100                    {
 101                        IReadOnlyList<DeviceIdentity> value = default;
 0102                        using var document = JsonDocument.Parse(message.Response.ContentStream);
 0103                        List<DeviceIdentity> array = new List<DeviceIdentity>();
 0104                        foreach (var item in document.RootElement.EnumerateArray())
 105                        {
 0106                            array.Add(DeviceIdentity.DeserializeDeviceIdentity(item));
 107                        }
 0108                        value = array;
 0109                        return Response.FromValue(value, message.Response);
 110                    }
 111                default:
 0112                    throw _clientDiagnostics.CreateRequestFailedException(message.Response);
 113            }
 0114        }
 115
 116        internal HttpMessage CreateBulkRegistryOperationsRequest(IEnumerable<ExportImportDevice> devices)
 117        {
 60118            var message = _pipeline.CreateMessage();
 60119            var request = message.Request;
 60120            request.Method = RequestMethod.Post;
 60121            var uri = new RawRequestUriBuilder();
 60122            uri.Reset(endpoint);
 60123            uri.AppendPath("/devices", false);
 60124            uri.AppendQuery("api-version", apiVersion, true);
 60125            request.Uri = uri;
 60126            request.Headers.Add("Content-Type", "application/json");
 60127            var content = new Utf8JsonRequestContent();
 60128            content.JsonWriter.WriteStartArray();
 1192129            foreach (var item in devices)
 130            {
 536131                content.JsonWriter.WriteObjectValue(item);
 132            }
 60133            content.JsonWriter.WriteEndArray();
 60134            request.Content = content;
 60135            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        {
 30144            if (devices == null)
 145            {
 0146                throw new ArgumentNullException(nameof(devices));
 147            }
 148
 30149            using var message = CreateBulkRegistryOperationsRequest(devices);
 30150            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
 30151            switch (message.Response.Status)
 152            {
 153                case 200:
 154                case 400:
 155                    {
 156                        BulkRegistryOperationResponse value = default;
 30157                        using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc
 30158                        value = BulkRegistryOperationResponse.DeserializeBulkRegistryOperationResponse(document.RootElem
 30159                        return Response.FromValue(value, message.Response);
 160                    }
 161                default:
 0162                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa
 163            }
 30164        }
 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        {
 30172            if (devices == null)
 173            {
 0174                throw new ArgumentNullException(nameof(devices));
 175            }
 176
 30177            using var message = CreateBulkRegistryOperationsRequest(devices);
 30178            _pipeline.Send(message, cancellationToken);
 30179            switch (message.Response.Status)
 180            {
 181                case 200:
 182                case 400:
 183                    {
 184                        BulkRegistryOperationResponse value = default;
 30185                        using var document = JsonDocument.Parse(message.Response.ContentStream);
 30186                        value = BulkRegistryOperationResponse.DeserializeBulkRegistryOperationResponse(document.RootElem
 30187                        return Response.FromValue(value, message.Response);
 188                    }
 189                default:
 0190                    throw _clientDiagnostics.CreateRequestFailedException(message.Response);
 191            }
 30192        }
 193
 194        internal HttpMessage CreateGetDeviceRequest(string id)
 195        {
 12196            var message = _pipeline.CreateMessage();
 12197            var request = message.Request;
 12198            request.Method = RequestMethod.Get;
 12199            var uri = new RawRequestUriBuilder();
 12200            uri.Reset(endpoint);
 12201            uri.AppendPath("/devices/", false);
 12202            uri.AppendPath(id, true);
 12203            uri.AppendQuery("api-version", apiVersion, true);
 12204            request.Uri = uri;
 12205            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        {
 6214            if (id == null)
 215            {
 0216                throw new ArgumentNullException(nameof(id));
 217            }
 218
 6219            using var message = CreateGetDeviceRequest(id);
 6220            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
 6221            switch (message.Response.Status)
 222            {
 223                case 200:
 224                    {
 225                        DeviceIdentity value = default;
 6226                        using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc
 6227                        value = DeviceIdentity.DeserializeDeviceIdentity(document.RootElement);
 6228                        return Response.FromValue(value, message.Response);
 229                    }
 230                default:
 0231                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa
 232            }
 6233        }
 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        {
 6241            if (id == null)
 242            {
 0243                throw new ArgumentNullException(nameof(id));
 244            }
 245
 6246            using var message = CreateGetDeviceRequest(id);
 6247            _pipeline.Send(message, cancellationToken);
 6248            switch (message.Response.Status)
 249            {
 250                case 200:
 251                    {
 252                        DeviceIdentity value = default;
 6253                        using var document = JsonDocument.Parse(message.Response.ContentStream);
 6254                        value = DeviceIdentity.DeserializeDeviceIdentity(document.RootElement);
 6255                        return Response.FromValue(value, message.Response);
 256                    }
 257                default:
 0258                    throw _clientDiagnostics.CreateRequestFailedException(message.Response);
 259            }
 6260        }
 261
 262        internal HttpMessage CreateCreateOrUpdateIdentityRequest(string id, DeviceIdentity device, string ifMatch)
 263        {
 72264            var message = _pipeline.CreateMessage();
 72265            var request = message.Request;
 72266            request.Method = RequestMethod.Put;
 72267            var uri = new RawRequestUriBuilder();
 72268            uri.Reset(endpoint);
 72269            uri.AppendPath("/devices/", false);
 72270            uri.AppendPath(id, true);
 72271            uri.AppendQuery("api-version", apiVersion, true);
 72272            request.Uri = uri;
 72273            if (ifMatch != null)
 274            {
 72275                request.Headers.Add("If-Match", ifMatch);
 276            }
 72277            request.Headers.Add("Content-Type", "application/json");
 72278            var content = new Utf8JsonRequestContent();
 72279            content.JsonWriter.WriteObjectValue(device);
 72280            request.Content = content;
 72281            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        {
 36292            if (id == null)
 293            {
 0294                throw new ArgumentNullException(nameof(id));
 295            }
 36296            if (device == null)
 297            {
 0298                throw new ArgumentNullException(nameof(device));
 299            }
 300
 36301            using var message = CreateCreateOrUpdateIdentityRequest(id, device, ifMatch);
 36302            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
 36303            switch (message.Response.Status)
 304            {
 305                case 200:
 306                    {
 307                        DeviceIdentity value = default;
 34308                        using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc
 34309                        value = DeviceIdentity.DeserializeDeviceIdentity(document.RootElement);
 34310                        return Response.FromValue(value, message.Response);
 311                    }
 312                default:
 2313                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa
 314            }
 34315        }
 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        {
 36325            if (id == null)
 326            {
 0327                throw new ArgumentNullException(nameof(id));
 328            }
 36329            if (device == null)
 330            {
 0331                throw new ArgumentNullException(nameof(device));
 332            }
 333
 36334            using var message = CreateCreateOrUpdateIdentityRequest(id, device, ifMatch);
 36335            _pipeline.Send(message, cancellationToken);
 36336            switch (message.Response.Status)
 337            {
 338                case 200:
 339                    {
 340                        DeviceIdentity value = default;
 34341                        using var document = JsonDocument.Parse(message.Response.ContentStream);
 34342                        value = DeviceIdentity.DeserializeDeviceIdentity(document.RootElement);
 34343                        return Response.FromValue(value, message.Response);
 344                    }
 345                default:
 2346                    throw _clientDiagnostics.CreateRequestFailedException(message.Response);
 347            }
 34348        }
 349
 350        internal HttpMessage CreateDeleteIdentityRequest(string id, string ifMatch)
 351        {
 48352            var message = _pipeline.CreateMessage();
 48353            var request = message.Request;
 48354            request.Method = RequestMethod.Delete;
 48355            var uri = new RawRequestUriBuilder();
 48356            uri.Reset(endpoint);
 48357            uri.AppendPath("/devices/", false);
 48358            uri.AppendPath(id, true);
 48359            uri.AppendQuery("api-version", apiVersion, true);
 48360            request.Uri = uri;
 48361            if (ifMatch != null)
 362            {
 48363                request.Headers.Add("If-Match", ifMatch);
 364            }
 48365            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        {
 24375            if (id == null)
 376            {
 0377                throw new ArgumentNullException(nameof(id));
 378            }
 379
 24380            using var message = CreateDeleteIdentityRequest(id, ifMatch);
 24381            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
 24382            switch (message.Response.Status)
 383            {
 384                case 204:
 24385                    return message.Response;
 386                default:
 0387                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa
 388            }
 24389        }
 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        {
 24398            if (id == null)
 399            {
 0400                throw new ArgumentNullException(nameof(id));
 401            }
 402
 24403            using var message = CreateDeleteIdentityRequest(id, ifMatch);
 24404            _pipeline.Send(message, cancellationToken);
 24405            switch (message.Response.Status)
 406            {
 407                case 204:
 24408                    return message.Response;
 409                default:
 0410                    throw _clientDiagnostics.CreateRequestFailedException(message.Response);
 411            }
 24412        }
 413
 414        internal HttpMessage CreateGetTwinRequest(string id)
 415        {
 8416            var message = _pipeline.CreateMessage();
 8417            var request = message.Request;
 8418            request.Method = RequestMethod.Get;
 8419            var uri = new RawRequestUriBuilder();
 8420            uri.Reset(endpoint);
 8421            uri.AppendPath("/twins/", false);
 8422            uri.AppendPath(id, true);
 8423            uri.AppendQuery("api-version", apiVersion, true);
 8424            request.Uri = uri;
 8425            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        {
 4434            if (id == null)
 435            {
 0436                throw new ArgumentNullException(nameof(id));
 437            }
 438
 4439            using var message = CreateGetTwinRequest(id);
 4440            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
 4441            switch (message.Response.Status)
 442            {
 443                case 200:
 444                    {
 445                        TwinData value = default;
 4446                        using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc
 4447                        value = TwinData.DeserializeTwinData(document.RootElement);
 4448                        return Response.FromValue(value, message.Response);
 449                    }
 450                default:
 0451                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa
 452            }
 4453        }
 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        {
 4461            if (id == null)
 462            {
 0463                throw new ArgumentNullException(nameof(id));
 464            }
 465
 4466            using var message = CreateGetTwinRequest(id);
 4467            _pipeline.Send(message, cancellationToken);
 4468            switch (message.Response.Status)
 469            {
 470                case 200:
 471                    {
 472                        TwinData value = default;
 4473                        using var document = JsonDocument.Parse(message.Response.ContentStream);
 4474                        value = TwinData.DeserializeTwinData(document.RootElement);
 4475                        return Response.FromValue(value, message.Response);
 476                    }
 477                default:
 0478                    throw _clientDiagnostics.CreateRequestFailedException(message.Response);
 479            }
 4480        }
 481
 482        internal HttpMessage CreateReplaceTwinRequest(string id, TwinData deviceTwinInfo, string ifMatch)
 483        {
 0484            var message = _pipeline.CreateMessage();
 0485            var request = message.Request;
 0486            request.Method = RequestMethod.Put;
 0487            var uri = new RawRequestUriBuilder();
 0488            uri.Reset(endpoint);
 0489            uri.AppendPath("/twins/", false);
 0490            uri.AppendPath(id, true);
 0491            uri.AppendQuery("api-version", apiVersion, true);
 0492            request.Uri = uri;
 0493            if (ifMatch != null)
 494            {
 0495                request.Headers.Add("If-Match", ifMatch);
 496            }
 0497            request.Headers.Add("Content-Type", "application/json");
 0498            var content = new Utf8JsonRequestContent();
 0499            content.JsonWriter.WriteObjectValue(deviceTwinInfo);
 0500            request.Content = content;
 0501            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        {
 0512            if (id == null)
 513            {
 0514                throw new ArgumentNullException(nameof(id));
 515            }
 0516            if (deviceTwinInfo == null)
 517            {
 0518                throw new ArgumentNullException(nameof(deviceTwinInfo));
 519            }
 520
 0521            using var message = CreateReplaceTwinRequest(id, deviceTwinInfo, ifMatch);
 0522            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
 0523            switch (message.Response.Status)
 524            {
 525                case 200:
 526                    {
 527                        TwinData value = default;
 0528                        using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc
 0529                        value = TwinData.DeserializeTwinData(document.RootElement);
 0530                        return Response.FromValue(value, message.Response);
 531                    }
 532                default:
 0533                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa
 534            }
 0535        }
 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        {
 0545            if (id == null)
 546            {
 0547                throw new ArgumentNullException(nameof(id));
 548            }
 0549            if (deviceTwinInfo == null)
 550            {
 0551                throw new ArgumentNullException(nameof(deviceTwinInfo));
 552            }
 553
 0554            using var message = CreateReplaceTwinRequest(id, deviceTwinInfo, ifMatch);
 0555            _pipeline.Send(message, cancellationToken);
 0556            switch (message.Response.Status)
 557            {
 558                case 200:
 559                    {
 560                        TwinData value = default;
 0561                        using var document = JsonDocument.Parse(message.Response.ContentStream);
 0562                        value = TwinData.DeserializeTwinData(document.RootElement);
 0563                        return Response.FromValue(value, message.Response);
 564                    }
 565                default:
 0566                    throw _clientDiagnostics.CreateRequestFailedException(message.Response);
 567            }
 0568        }
 569
 570        internal HttpMessage CreateUpdateTwinRequest(string id, TwinData deviceTwinInfo, string ifMatch)
 571        {
 4572            var message = _pipeline.CreateMessage();
 4573            var request = message.Request;
 4574            request.Method = RequestMethod.Patch;
 4575            var uri = new RawRequestUriBuilder();
 4576            uri.Reset(endpoint);
 4577            uri.AppendPath("/twins/", false);
 4578            uri.AppendPath(id, true);
 4579            uri.AppendQuery("api-version", apiVersion, true);
 4580            request.Uri = uri;
 4581            if (ifMatch != null)
 582            {
 4583                request.Headers.Add("If-Match", ifMatch);
 584            }
 4585            request.Headers.Add("Content-Type", "application/json");
 4586            var content = new Utf8JsonRequestContent();
 4587            content.JsonWriter.WriteObjectValue(deviceTwinInfo);
 4588            request.Content = content;
 4589            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        {
 2600            if (id == null)
 601            {
 0602                throw new ArgumentNullException(nameof(id));
 603            }
 2604            if (deviceTwinInfo == null)
 605            {
 0606                throw new ArgumentNullException(nameof(deviceTwinInfo));
 607            }
 608
 2609            using var message = CreateUpdateTwinRequest(id, deviceTwinInfo, ifMatch);
 2610            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
 2611            switch (message.Response.Status)
 612            {
 613                case 200:
 614                    {
 615                        TwinData value = default;
 2616                        using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc
 2617                        value = TwinData.DeserializeTwinData(document.RootElement);
 2618                        return Response.FromValue(value, message.Response);
 619                    }
 620                default:
 0621                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa
 622            }
 2623        }
 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        {
 2633            if (id == null)
 634            {
 0635                throw new ArgumentNullException(nameof(id));
 636            }
 2637            if (deviceTwinInfo == null)
 638            {
 0639                throw new ArgumentNullException(nameof(deviceTwinInfo));
 640            }
 641
 2642            using var message = CreateUpdateTwinRequest(id, deviceTwinInfo, ifMatch);
 2643            _pipeline.Send(message, cancellationToken);
 2644            switch (message.Response.Status)
 645            {
 646                case 200:
 647                    {
 648                        TwinData value = default;
 2649                        using var document = JsonDocument.Parse(message.Response.ContentStream);
 2650                        value = TwinData.DeserializeTwinData(document.RootElement);
 2651                        return Response.FromValue(value, message.Response);
 652                    }
 653                default:
 0654                    throw _clientDiagnostics.CreateRequestFailedException(message.Response);
 655            }
 2656        }
 657
 658        internal HttpMessage CreateInvokeMethodRequest(string deviceId, CloudToDeviceMethodRequest directMethodRequest)
 659        {
 0660            var message = _pipeline.CreateMessage();
 0661            var request = message.Request;
 0662            request.Method = RequestMethod.Post;
 0663            var uri = new RawRequestUriBuilder();
 0664            uri.Reset(endpoint);
 0665            uri.AppendPath("/twins/", false);
 0666            uri.AppendPath(deviceId, true);
 0667            uri.AppendPath("/methods", false);
 0668            uri.AppendQuery("api-version", apiVersion, true);
 0669            request.Uri = uri;
 0670            request.Headers.Add("Content-Type", "application/json");
 0671            var content = new Utf8JsonRequestContent();
 0672            content.JsonWriter.WriteObjectValue(directMethodRequest);
 0673            request.Content = content;
 0674            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        {
 0684            if (deviceId == null)
 685            {
 0686                throw new ArgumentNullException(nameof(deviceId));
 687            }
 0688            if (directMethodRequest == null)
 689            {
 0690                throw new ArgumentNullException(nameof(directMethodRequest));
 691            }
 692
 0693            using var message = CreateInvokeMethodRequest(deviceId, directMethodRequest);
 0694            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
 0695            switch (message.Response.Status)
 696            {
 697                case 200:
 698                    {
 699                        CloudToDeviceMethodResponse value = default;
 0700                        using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc
 0701                        value = CloudToDeviceMethodResponse.DeserializeCloudToDeviceMethodResponse(document.RootElement)
 0702                        return Response.FromValue(value, message.Response);
 703                    }
 704                default:
 0705                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa
 706            }
 0707        }
 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        {
 0716            if (deviceId == null)
 717            {
 0718                throw new ArgumentNullException(nameof(deviceId));
 719            }
 0720            if (directMethodRequest == null)
 721            {
 0722                throw new ArgumentNullException(nameof(directMethodRequest));
 723            }
 724
 0725            using var message = CreateInvokeMethodRequest(deviceId, directMethodRequest);
 0726            _pipeline.Send(message, cancellationToken);
 0727            switch (message.Response.Status)
 728            {
 729                case 200:
 730                    {
 731                        CloudToDeviceMethodResponse value = default;
 0732                        using var document = JsonDocument.Parse(message.Response.ContentStream);
 0733                        value = CloudToDeviceMethodResponse.DeserializeCloudToDeviceMethodResponse(document.RootElement)
 0734                        return Response.FromValue(value, message.Response);
 735                    }
 736                default:
 0737                    throw _clientDiagnostics.CreateRequestFailedException(message.Response);
 738            }
 0739        }
 740    }
 741}