| | 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.CognitiveServices.Knowledge.QnAMaker |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using System.Net.Http; |
| | 9 | | using System.Threading; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | | using Microsoft.Rest; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Allows authentication to the API using a basic apiKey mechanism |
| | 15 | | /// </summary> |
| | 16 | | public class EndpointKeyServiceClientCredentials : ServiceClientCredentials |
| | 17 | | { |
| | 18 | | private readonly string endpointKey; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Creates a new instance of the EndpointKeyServiceClientCredentials class |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="endpointKey">The Endpoint key to authenticate and authorize as</param> |
| 2 | 24 | | public EndpointKeyServiceClientCredentials(string endpointKey) |
| | 25 | | { |
| 2 | 26 | | this.endpointKey = endpointKey; |
| 2 | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Add the Basic Authentication Header to each outgoing request |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="request">The outgoing request</param> |
| | 33 | | /// <param name="cancellationToken">A token to cancel the operation</param> |
| | 34 | | public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| | 35 | | { |
| 2 | 36 | | if (request == null) |
| 0 | 37 | | throw new ArgumentNullException("request"); |
| | 38 | |
|
| 2 | 39 | | request.Headers.Add("Authorization", $"EndpointKey {this.endpointKey}"); |
| | 40 | |
|
| 2 | 41 | | return Task.FromResult<object>(null); |
| | 42 | | } |
| | 43 | | } |
| | 44 | | } |