| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using Azure.Core; |
| | 6 | | using Azure.Core.Pipeline; |
| | 7 | | using Azure.Iot.Hub.Service.Authentication; |
| | 8 | |
|
| | 9 | | namespace 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> |
| 268 | 27 | | public virtual DevicesClient Devices { get; private set; } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// place holder for Modules. |
| | 31 | | /// </summary> |
| 266 | 32 | | public virtual ModulesClient Modules { get; private set; } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// place holder for Statistics. |
| | 36 | | /// </summary> |
| 84 | 37 | | public virtual StatisticsClient Statistics { get; private set; } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// place holder for Messages. |
| | 41 | | /// </summary> |
| 0 | 42 | | public virtual CloudToDeviceMessagesClient Messages { get; private set; } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// place holder for Files. |
| | 46 | | /// </summary> |
| 0 | 47 | | public virtual FilesClient Files { get; private set; } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// place holder for Jobs |
| | 51 | | /// </summary> |
| 0 | 52 | | public virtual JobsClient Jobs { get; private set; } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Initializes a new instance of the <see cref="IotHubServiceClient"/> class. |
| | 56 | | /// </summary> |
| 76 | 57 | | protected IotHubServiceClient() |
| | 58 | | { |
| | 59 | | // This constructor only exists for mocking purposes in unit tests. It should not be used otherwise. |
| 76 | 60 | | } |
| | 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) |
| 0 | 76 | | : this(connectionString, new IotHubServiceClientOptions()) |
| | 77 | | { |
| 0 | 78 | | } |
| | 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) |
| 76 | 97 | | : this(new IotHubSasCredential(connectionString), options) |
| | 98 | | { |
| 76 | 99 | | } |
| | 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 |
| 0 | 121 | | : this(SetEndpointToIotHubSasCredential(endpoint, credential), options) |
| | 122 | | { |
| 0 | 123 | | } |
| | 124 | |
|
| 76 | 125 | | private IotHubServiceClient(IotHubSasCredential credential, IotHubServiceClientOptions options) |
| | 126 | | { |
| 76 | 127 | | options ??= new IotHubServiceClientOptions(); |
| 76 | 128 | | _clientDiagnostics = new ClientDiagnostics(options); |
| | 129 | |
|
| 76 | 130 | | options.AddPolicy(new SasTokenAuthenticationPolicy(credential), HttpPipelinePosition.PerCall); |
| 76 | 131 | | _httpPipeline = HttpPipelineBuilder.Build(options); |
| | 132 | |
|
| 76 | 133 | | _devicesRestClient = new DevicesRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.G |
| 76 | 134 | | _modulesRestClient = new ModulesRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.G |
| 76 | 135 | | _queryRestClient = new QueryRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.GetVe |
| 76 | 136 | | _statisticsRestClient = new StatisticsRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, opt |
| | 137 | |
|
| 76 | 138 | | Devices = new DevicesClient(_devicesRestClient, _queryRestClient); |
| 76 | 139 | | Modules = new ModulesClient(_devicesRestClient, _modulesRestClient, _queryRestClient); |
| 76 | 140 | | Statistics = new StatisticsClient(_statisticsRestClient); |
| | 141 | |
|
| 76 | 142 | | Messages = new CloudToDeviceMessagesClient(); |
| 76 | 143 | | Files = new FilesClient(); |
| 76 | 144 | | Jobs = new JobsClient(); |
| 76 | 145 | | } |
| | 146 | |
|
| | 147 | | private static IotHubSasCredential SetEndpointToIotHubSasCredential(Uri endpoint, IotHubSasCredential credential |
| | 148 | | { |
| 0 | 149 | | credential.Endpoint = endpoint; |
| 0 | 150 | | return credential; |
| | 151 | | } |
| | 152 | | } |
| | 153 | | } |