< Summary

Class:Azure.Iot.Hub.Service.IotHubServiceClient
Assembly:Azure.Iot.Hub.Service
File(s):C:\Git\azure-sdk-for-net\sdk\iot\Azure.Iot.Hub.Service\src\IotHubServiceClient.cs
Covered lines:23
Uncovered lines:9
Coverable lines:32
Total lines:153
Line coverage:71.8% (23 of 32)
Covered branches:1
Total branches:2
Branch coverage:50% (1 of 2)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_Devices()-100%100%
get_Modules()-100%100%
get_Statistics()-100%100%
get_Messages()-0%100%
get_Files()-0%100%
get_Jobs()-0%100%
.ctor()-100%100%
.ctor(...)-0%100%
.ctor(...)-100%100%
.ctor(...)-0%100%
.ctor(...)-100%50%
SetEndpointToIotHubSasCredential(...)-0%100%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using Azure.Core;
 6using Azure.Core.Pipeline;
 7using Azure.Iot.Hub.Service.Authentication;
 8
 9namespace Azure.Iot.Hub.Service
 10{
 11    /// <summary>
 12    /// The IoT Hub Service Client.
 13    /// </summary>
 14    public class IotHubServiceClient
 15    {
 16        private readonly HttpPipeline _httpPipeline;
 17        private readonly ClientDiagnostics _clientDiagnostics;
 18
 19        private readonly DevicesRestClient _devicesRestClient;
 20        private readonly ModulesRestClient _modulesRestClient;
 21        private readonly QueryRestClient _queryRestClient;
 22        private readonly StatisticsRestClient _statisticsRestClient;
 23
 24        /// <summary>
 25        /// place holder for Devices.
 26        /// </summary>
 26827        public virtual DevicesClient Devices { get; private set; }
 28
 29        /// <summary>
 30        /// place holder for Modules.
 31        /// </summary>
 26632        public virtual ModulesClient Modules { get; private set; }
 33
 34        /// <summary>
 35        /// place holder for Statistics.
 36        /// </summary>
 8437        public virtual StatisticsClient Statistics { get; private set; }
 38
 39        /// <summary>
 40        /// place holder for Messages.
 41        /// </summary>
 042        public virtual CloudToDeviceMessagesClient Messages { get; private set; }
 43
 44        /// <summary>
 45        /// place holder for Files.
 46        /// </summary>
 047        public virtual FilesClient Files { get; private set; }
 48
 49        /// <summary>
 50        /// place holder for Jobs
 51        /// </summary>
 052        public virtual JobsClient Jobs { get; private set; }
 53
 54        /// <summary>
 55        /// Initializes a new instance of the <see cref="IotHubServiceClient"/> class.
 56        /// </summary>
 7657        protected IotHubServiceClient()
 58        {
 59            // This constructor only exists for mocking purposes in unit tests. It should not be used otherwise.
 7660        }
 61
 62        /// <summary>
 63        /// Initializes a new instance of the <see cref="IotHubServiceClient"/> class.
 64        /// </summary>
 65        /// <param name="connectionString">
 66        /// The IoT Hub connection string, with either "iothubowner", "service", "registryRead" or "registryReadWrite" p
 67        /// For more information, see <see href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-securit
 68        /// </param>
 69        /// <seealso cref="IotHubServiceClient(Uri, IotHubSasCredential, IotHubServiceClientOptions)">
 70        /// This other constructor provides an opportunity to override default behavior, including setting the sas token
 71        /// overriding <see href="https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/samples/Pip
 72        /// enabling <see href="https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/samples/Diagn
 73        /// and controlling <see href="https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/sample
 74        /// </seealso>
 75        public IotHubServiceClient(string connectionString)
 076            : this(connectionString, new IotHubServiceClientOptions())
 77        {
 078        }
 79
 80        /// <summary>
 81        /// Initializes a new instance of the <see cref="IotHubServiceClient"/> class.
 82        /// </summary>
 83        /// <param name="connectionString">
 84        /// The IoT Hub connection string, with either "iothubowner", "service", "registryRead" or "registryReadWrite" p
 85        /// For more information, see <see href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-securit
 86        /// </param>
 87        /// <param name="options">
 88        /// Options that allow configuration of requests sent to the IoT Hub service.
 89        /// </param>
 90        /// <seealso cref="IotHubServiceClient(Uri, IotHubSasCredential, IotHubServiceClientOptions)">
 91        /// This other constructor provides an opportunity to override default behavior, including setting the sas token
 92        /// overriding <see href="https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/samples/Pip
 93        /// enabling <see href="https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/samples/Diagn
 94        /// and controlling <see href="https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/sample
 95        /// </seealso>
 96        public IotHubServiceClient(string connectionString, IotHubServiceClientOptions options)
 7697            : this(new IotHubSasCredential(connectionString), options)
 98        {
 7699        }
 100
 101        /// <summary>
 102        /// Initializes a new instance of the <see cref="IotHubServiceClient"/> class.
 103        /// </summary>
 104        /// <param name="endpoint">
 105        /// The IoT Hub service instance endpoint to connect to.
 106        /// </param>
 107        /// <param name="credential">
 108        /// The IoT Hub credentials, to be used for authenticating against an IoT Hub instance via SAS tokens.
 109        /// </param>
 110        /// <param name="options">
 111        /// (optional) Options that allow configuration of requests sent to the IoT Hub service.
 112        /// </param>
 113        /// <code snippet="Snippet:IotHubServiceClientInitializeWithIotHubSasCredential">
 114        /// // Create an IotHubSasCredential type to use sas tokens to authenticate against your IoT Hub instance.
 115        /// // The default lifespan of the sas token is 30 minutes, and it is set to be renewed when at 15% or less of i
 116        /// var credential = new IotHubSasCredential(options.IotHubSharedAccessPolicy, options.IotHubSharedAccessKey);
 117        ///
 118        /// IotHubServiceClient hubClient = new IotHubServiceClient(options.Endpoint, credential);
 119        /// </code>
 120        public IotHubServiceClient(Uri endpoint, IotHubSasCredential credential, IotHubServiceClientOptions options = de
 0121            : this(SetEndpointToIotHubSasCredential(endpoint, credential), options)
 122        {
 0123        }
 124
 76125        private IotHubServiceClient(IotHubSasCredential credential, IotHubServiceClientOptions options)
 126        {
 76127            options ??= new IotHubServiceClientOptions();
 76128            _clientDiagnostics = new ClientDiagnostics(options);
 129
 76130            options.AddPolicy(new SasTokenAuthenticationPolicy(credential), HttpPipelinePosition.PerCall);
 76131            _httpPipeline = HttpPipelineBuilder.Build(options);
 132
 76133            _devicesRestClient = new DevicesRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.G
 76134            _modulesRestClient = new ModulesRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.G
 76135            _queryRestClient = new QueryRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.GetVe
 76136            _statisticsRestClient = new StatisticsRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, opt
 137
 76138            Devices = new DevicesClient(_devicesRestClient, _queryRestClient);
 76139            Modules = new ModulesClient(_devicesRestClient, _modulesRestClient, _queryRestClient);
 76140            Statistics = new StatisticsClient(_statisticsRestClient);
 141
 76142            Messages = new CloudToDeviceMessagesClient();
 76143            Files = new FilesClient();
 76144            Jobs = new JobsClient();
 76145        }
 146
 147        private static IotHubSasCredential SetEndpointToIotHubSasCredential(Uri endpoint, IotHubSasCredential credential
 148        {
 0149            credential.Endpoint = endpoint;
 0150            return credential;
 151        }
 152    }
 153}