| | 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; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Net; |
| | 11 | | using System.Net.Http; |
| | 12 | | using System.Net.Http.Headers; |
| | 13 | | using System.Threading.Tasks; |
| | 14 | | using Microsoft.Rest.Azure; |
| | 15 | | using Rest; |
| | 16 | |
|
| | 17 | | internal static class ExistsHelper |
| | 18 | | { |
| | 19 | | public static async Task<AzureOperationResponse<bool>> ExistsFromGetResponse<T>( |
| | 20 | | Func<Task<AzureOperationResponse<T>>> invokeGet) |
| | 21 | | { |
| 22 | 22 | | var response = new AzureOperationResponse<bool>(); |
| | 23 | |
|
| | 24 | | try |
| | 25 | | { |
| | 26 | | // Get validates indexName. |
| 22 | 27 | | AzureOperationResponse<T> getResponse = await invokeGet().ConfigureAwait(false); |
| 10 | 28 | | response.Body = true; |
| 10 | 29 | | response.Request = getResponse.Request; |
| 10 | 30 | | response.RequestId = getResponse.RequestId; |
| 10 | 31 | | response.Response = getResponse.Response; |
| 10 | 32 | | return response; |
| | 33 | | } |
| 12 | 34 | | catch (CloudException e) |
| | 35 | | { |
| 12 | 36 | | if (e.Response.StatusCode == HttpStatusCode.NotFound) |
| | 37 | | { |
| 12 | 38 | | response.Body = false; |
| 12 | 39 | | response.Request = CopyRequest(e.Request); |
| 12 | 40 | | response.Response = CopyResponse(e.Response); |
| | 41 | |
|
| 12 | 42 | | if (e.Response.Headers.ContainsKey("request-id")) |
| | 43 | | { |
| 12 | 44 | | response.RequestId = e.Response.Headers["request-id"].FirstOrDefault(); |
| | 45 | | } |
| | 46 | |
|
| 12 | 47 | | return response; |
| | 48 | | } |
| | 49 | |
|
| 0 | 50 | | throw; |
| | 51 | | } |
| 22 | 52 | | } |
| | 53 | |
|
| | 54 | | private static HttpRequestMessage CopyRequest(HttpRequestMessageWrapper requestWrapper) |
| | 55 | | { |
| 12 | 56 | | var request = new HttpRequestMessage(requestWrapper.Method, requestWrapper.RequestUri); |
| 12 | 57 | | request.Content = CopyContent(requestWrapper); |
| 12 | 58 | | CopyHeaders(requestWrapper, request.Headers); |
| 12 | 59 | | CopyProperties(requestWrapper, request); |
| 12 | 60 | | return request; |
| | 61 | | } |
| | 62 | |
|
| | 63 | | private static HttpResponseMessage CopyResponse(HttpResponseMessageWrapper responseWrapper) |
| | 64 | | { |
| 12 | 65 | | var response = new HttpResponseMessage(responseWrapper.StatusCode); |
| 12 | 66 | | response.Content = CopyContent(responseWrapper); |
| 12 | 67 | | CopyHeaders(responseWrapper, response.Headers); |
| 12 | 68 | | response.ReasonPhrase = responseWrapper.ReasonPhrase; |
| 12 | 69 | | return response; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | private static StringContent CopyContent(HttpMessageWrapper source) |
| | 73 | | { |
| 24 | 74 | | return new StringContent(source.Content ?? String.Empty); |
| | 75 | | } |
| | 76 | |
|
| | 77 | | private static void CopyHeaders(HttpMessageWrapper source, HttpHeaders headers) |
| | 78 | | { |
| 24 | 79 | | var sourceHeaders = source.Headers ?? Enumerable.Empty<KeyValuePair<string, IEnumerable<string>>>(); |
| | 80 | |
|
| 456 | 81 | | foreach (KeyValuePair<string, IEnumerable<string>> header in sourceHeaders) |
| | 82 | | { |
| 204 | 83 | | string headerName = header.Key; |
| 1032 | 84 | | foreach (string headerValue in header.Value) |
| | 85 | | { |
| 312 | 86 | | headers.TryAddWithoutValidation(headerName, headerValue); |
| | 87 | | } |
| | 88 | | } |
| 24 | 89 | | } |
| | 90 | |
|
| | 91 | | private static void CopyProperties(HttpRequestMessageWrapper source, HttpRequestMessage target) |
| | 92 | | { |
| 12 | 93 | | var sourceProperties = source.Properties ?? Enumerable.Empty<KeyValuePair<string, object>>(); |
| 0 | 94 | | foreach (KeyValuePair<string, object> property in sourceProperties) |
| | 95 | | { |
| 0 | 96 | | target.Properties.Add(property.Key, property.Value); |
| | 97 | | } |
| 12 | 98 | | } |
| | 99 | | } |
| | 100 | | } |