< Summary

Class:Microsoft.Azure.Search.SearchCredentials
Assembly:Microsoft.Azure.Search.Common
File(s):C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Common\src\Customizations\Search\SearchCredentials.cs
Covered lines:8
Uncovered lines:0
Coverable lines:8
Total lines:59
Line coverage:100% (8 of 8)
Covered branches:0
Total branches:0

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
get_ApiKey()-100%100%
ProcessHttpRequestAsync(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Common\src\Customizations\Search\SearchCredentials.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License. See License.txt in the project root for
 3// license information.
 4
 5namespace Microsoft.Azure.Search
 6{
 7    using System.Net.Http;
 8    using System.Threading;
 9    using System.Threading.Tasks;
 10    using Common;
 11    using Rest;
 12
 13    /// <summary>
 14    /// Credentials used to authenticate to a search service.
 15    /// <see href="https://docs.microsoft.com/rest/api/searchservice/">Azure Cognitive Search Service REST</see>
 16    /// </summary>
 17    /// <remarks>
 18    /// See <see href="https://docs.microsoft.com/azure/search/search-security-api-keys">Create and manage api-keys for 
 19    /// </remarks>
 20    public class SearchCredentials : ServiceClientCredentials
 21    {
 22        /// <summary>
 23        /// Initializes a new instance of the SearchCredentials class with a query key or an admin key. Use a query
 24        /// key if your application does not require write access to the Search Service or index.
 25        /// </summary>
 26        /// <param name="apiKey">api-key used to authenticate to the search service.</param>
 27        /// <remarks>
 28        /// If your application performs only query operations on an index, we recommend passing a query key for the
 29        /// <paramref name="apiKey"/> parameter. This ensures that you have read-only access to the index, which is
 30        /// consistent with the principle of least privilege.
 31        /// </remarks>
 146032        public SearchCredentials(string apiKey)
 33        {
 146034            Throw.IfArgumentNullOrEmpty(apiKey, "apiKey");
 146035            ApiKey = apiKey;
 146036        }
 37
 38        /// <summary>
 39        /// api-key used to authenticate to a search service. Can be either a query key for querying only, or
 40        /// an admin key that enables index and document management as well.
 41        /// </summary>
 239042        public string ApiKey { get; }
 43
 44        /// <summary>
 45        /// Adds the credentials to the given HTTP request.
 46        /// </summary>
 47        /// <param name="request">HTTP request</param>
 48        /// <param name="cancellationToken">Cancellation token</param>
 49        /// <returns>A Task to track the progress of the async operation.</returns>
 50        public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
 51        {
 238252            Throw.IfArgumentNull(request, "request");
 53
 238254            request.Headers.Add("api-key", ApiKey);
 55
 238256            return base.ProcessHttpRequestAsync(request, cancellationToken);
 57        }
 58    }
 59}