< Summary

Class:Microsoft.Azure.Search.ExistsHelper
Assembly:Microsoft.Azure.Search.Service
File(s):C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Service\src\Customizations\Utilities\ExistsHelper.cs
Covered lines:35
Uncovered lines:3
Coverable lines:38
Total lines:100
Line coverage:92.1% (35 of 38)
Covered branches:12
Total branches:16
Branch coverage:75% (12 of 16)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ExistsFromGetResponse()-94.12%75%
CopyRequest(...)-100%100%
CopyResponse(...)-100%100%
CopyContent(...)-100%100%
CopyHeaders(...)-100%83.33%
CopyProperties(...)-50%50%

File(s)

C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Service\src\Customizations\Utilities\ExistsHelper.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;
 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        {
 2222            var response = new AzureOperationResponse<bool>();
 23
 24            try
 25            {
 26                // Get validates indexName.
 2227                AzureOperationResponse<T> getResponse = await invokeGet().ConfigureAwait(false);
 1028                response.Body = true;
 1029                response.Request = getResponse.Request;
 1030                response.RequestId = getResponse.RequestId;
 1031                response.Response = getResponse.Response;
 1032                return response;
 33            }
 1234            catch (CloudException e)
 35            {
 1236                if (e.Response.StatusCode == HttpStatusCode.NotFound)
 37                {
 1238                    response.Body = false;
 1239                    response.Request = CopyRequest(e.Request);
 1240                    response.Response = CopyResponse(e.Response);
 41
 1242                    if (e.Response.Headers.ContainsKey("request-id"))
 43                    {
 1244                        response.RequestId = e.Response.Headers["request-id"].FirstOrDefault();
 45                    }
 46
 1247                    return response;
 48                }
 49
 050                throw;
 51            }
 2252        }
 53
 54        private static HttpRequestMessage CopyRequest(HttpRequestMessageWrapper requestWrapper)
 55        {
 1256            var request = new HttpRequestMessage(requestWrapper.Method, requestWrapper.RequestUri);
 1257            request.Content = CopyContent(requestWrapper);
 1258            CopyHeaders(requestWrapper, request.Headers);
 1259            CopyProperties(requestWrapper, request);
 1260            return request;
 61        }
 62
 63        private static HttpResponseMessage CopyResponse(HttpResponseMessageWrapper responseWrapper)
 64        {
 1265            var response = new HttpResponseMessage(responseWrapper.StatusCode);
 1266            response.Content = CopyContent(responseWrapper);
 1267            CopyHeaders(responseWrapper, response.Headers);
 1268            response.ReasonPhrase = responseWrapper.ReasonPhrase;
 1269            return response;
 70        }
 71
 72        private static StringContent CopyContent(HttpMessageWrapper source)
 73        {
 2474            return new StringContent(source.Content ?? String.Empty);
 75        }
 76
 77        private static void CopyHeaders(HttpMessageWrapper source, HttpHeaders headers)
 78        {
 2479            var sourceHeaders = source.Headers ?? Enumerable.Empty<KeyValuePair<string, IEnumerable<string>>>();
 80
 45681            foreach (KeyValuePair<string, IEnumerable<string>> header in sourceHeaders)
 82            {
 20483                string headerName = header.Key;
 103284                foreach (string headerValue in header.Value)
 85                {
 31286                    headers.TryAddWithoutValidation(headerName, headerValue);
 87                }
 88            }
 2489        }
 90
 91        private static void CopyProperties(HttpRequestMessageWrapper source, HttpRequestMessage target)
 92        {
 1293            var sourceProperties = source.Properties ?? Enumerable.Empty<KeyValuePair<string, object>>();
 094            foreach (KeyValuePair<string, object> property in sourceProperties)
 95            {
 096                target.Properties.Add(property.Key, property.Value);
 97            }
 1298        }
 99    }
 100}