| | 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 | |
|
| | 5 | | namespace 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> |
| 1460 | 32 | | public SearchCredentials(string apiKey) |
| | 33 | | { |
| 1460 | 34 | | Throw.IfArgumentNullOrEmpty(apiKey, "apiKey"); |
| 1460 | 35 | | ApiKey = apiKey; |
| 1460 | 36 | | } |
| | 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> |
| 2390 | 42 | | 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 | | { |
| 2382 | 52 | | Throw.IfArgumentNull(request, "request"); |
| | 53 | |
|
| 2382 | 54 | | request.Headers.Add("api-key", ApiKey); |
| | 55 | |
|
| 2382 | 56 | | return base.ProcessHttpRequestAsync(request, cancellationToken); |
| | 57 | | } |
| | 58 | | } |
| | 59 | | } |