< Summary

Class:Azure.Storage.StorageClientOptions
Assembly:Azure.Storage.Blobs
File(s):C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Common\src\Shared\StorageClientOptions.cs
Covered lines:22
Uncovered lines:2
Coverable lines:24
Total lines:114
Line coverage:91.6% (22 of 24)
Covered branches:9
Total branches:14
Branch coverage:64.2% (9 of 14)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
Initialize(...)-100%100%
AsPolicy(...)-100%50%
AsPolicy(...)-100%50%
GetAuthenticationPolicy(...)-50%62.5%
Build(...)-100%100%
Build(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Common\src\Shared\StorageClientOptions.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Net.Http;
 7using Azure.Core;
 8using Azure.Core.Pipeline;
 9
 10namespace Azure.Storage
 11{
 12    /// <summary>
 13    /// Defines the client configuration options for connecting to Azure
 14    /// Storage.
 15    /// </summary>
 16    internal static class StorageClientOptions
 17    {
 18        /// <summary>
 19        /// The default scope used for token authentication with Storage.
 20        /// </summary>
 21        private const string StorageScope = "https://storage.azure.com/.default";
 22
 23        /// <summary>
 24        /// Set common ClientOptions defaults for Azure Storage.
 25        /// </summary>
 26        /// <param name="options">Storage ClientOptions.</param>
 27        public static void Initialize(this ClientOptions options)
 28        {
 29            // We're going to use the default ResponseClassifier to decide
 30            // which errors are retriable, but may extend this in the future.
 31
 32            // We'll use the standard RetryPolicy with a few more retries
 1533433            options.Retry.MaxRetries = Constants.MaxReliabilityRetries;
 1533434        }
 35
 36        /// <summary>
 37        /// Get an authentication policy to sign Storage requests.
 38        /// </summary>
 39        /// <param name="credential">Credential to use.</param>
 40        /// <returns>An authentication policy.</returns>
 41        public static HttpPipelinePolicy AsPolicy(this StorageSharedKeyCredential credential) =>
 421642            new StorageSharedKeyPipelinePolicy(
 421643                credential ?? throw Errors.ArgumentNull(nameof(credential)));
 44
 45        /// <summary>
 46        /// Get an authentication policy to sign Storage requests.
 47        /// </summary>
 48        /// <param name="credential">Credential to use.</param>
 49        /// <returns>An authentication policy.</returns>
 50        public static HttpPipelinePolicy AsPolicy(this TokenCredential credential) =>
 10051            new BearerTokenAuthenticationPolicy(
 10052                credential ?? throw Errors.ArgumentNull(nameof(credential)),
 10053                StorageScope);
 54
 55        /// <summary>
 56        /// Get an optional authentication policy to sign Storage requests.
 57        /// </summary>
 58        /// <param name="credentials">Optional credentials to use.</param>
 59        /// <returns>An optional authentication policy.</returns>
 60        public static HttpPipelinePolicy GetAuthenticationPolicy(object credentials = null)
 61        {
 62            // Use the credentials to decide on the authentication policy
 63            switch (credentials)
 64            {
 65                case SharedAccessSignatureCredentials _:
 66                case null: // Anonymous authentication
 3267                    return null;
 68                case StorageSharedKeyCredential sharedKey:
 12469                    return sharedKey.AsPolicy();
 70                case TokenCredential token:
 071                    return token.AsPolicy();
 72                default:
 073                    throw Errors.InvalidCredentials(credentials.GetType().FullName);
 74            }
 75        }
 76
 77        /// <summary>
 78        /// Create an HttpPipeline from Storage ClientOptions.
 79        /// </summary>
 80        /// <param name="options">The Storage ClientOptions.</param>
 81        /// <param name="authentication">Optional authentication policy.</param>
 82        /// <param name="geoRedundantSecondaryStorageUri">The secondary URI to be used for retries on failed read reques
 83        /// <returns>An HttpPipeline to use for Storage requests.</returns>
 84        public static HttpPipeline Build(this ClientOptions options, HttpPipelinePolicy authentication = null, Uri geoRe
 85        {
 474086            List<HttpPipelinePolicy> perRetryClientPolicies = new List<HttpPipelinePolicy>();
 474087            StorageResponseClassifier classifier = new StorageResponseClassifier();
 474088            if (geoRedundantSecondaryStorageUri != null)
 89            {
 2890                perRetryClientPolicies.Add(new GeoRedundantReadPolicy(geoRedundantSecondaryStorageUri));
 2891                classifier.SecondaryStorageUri = geoRedundantSecondaryStorageUri;
 92            }
 93
 474094            perRetryClientPolicies.Add(new StorageRequestValidationPipelinePolicy(options));
 474095            perRetryClientPolicies.Add(authentication); // authentication needs to be the last of the perRetry client po
 96
 474097            return HttpPipelineBuilder.Build(
 474098               options,
 474099               Array.Empty<HttpPipelinePolicy>(),
 4740100               perRetryClientPolicies.ToArray(),
 4740101               classifier);
 102        }
 103
 104        /// <summary>
 105        /// Create an HttpPipeline from Storage ClientOptions.
 106        /// </summary>
 107        /// <param name="options">The Storage ClientOptions.</param>
 108        /// <param name="credentials">Optional authentication credentials.</param>
 109        /// <param name="geoRedundantSecondaryStorageUri">The secondary URI to be used for retries on failed read reques
 110        /// <returns>An HttpPipeline to use for Storage requests.</returns>
 111        public static HttpPipeline Build(this ClientOptions options, object credentials, Uri geoRedundantSecondaryStorag
 148112            Build(options, GetAuthenticationPolicy(credentials), geoRedundantSecondaryStorageUri);
 113    }
 114}