< Summary

Class:Microsoft.Azure.Search.DocumentsOperations
Assembly:Microsoft.Azure.Search.Data
File(s):C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Data\src\Customizations\Documents\DocumentsOperations.Customization.cs
Covered lines:186
Uncovered lines:9
Coverable lines:195
Total lines:446
Line coverage:95.3% (186 of 195)
Covered branches:26
Total branches:32
Branch coverage:81.2% (26 of 32)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-100%100%
.ctor(...)-100%50%
get_Client()-100%100%
CountWithHttpMessagesAsync()-100%100%
AutocompleteWithHttpMessagesAsync(...)-100%100%
ContinueSearchWithHttpMessagesAsync(...)-100%100%
ContinueSearchWithHttpMessagesAsync(...)-100%100%
GetWithHttpMessagesAsync(...)-100%100%
GetWithHttpMessagesAsync(...)-100%100%
IndexWithHttpMessagesAsync(...)-100%100%
IndexWithHttpMessagesAsync(...)-100%100%
SearchWithHttpMessagesAsync(...)-100%100%
SearchWithHttpMessagesAsync(...)-100%100%
SuggestWithHttpMessagesAsync(...)-100%100%
SuggestWithHttpMessagesAsync(...)-100%100%
EnsureCustomHeaders(...)-100%100%
DoIndexAsync()-100%100%
DoContinueSearchAsync(...)-73.53%62.5%
DoSearchAsync(...)-100%83.33%
DoSuggestAsync(...)-100%75%

File(s)

C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Data\src\Customizations\Documents\DocumentsOperations.Customization.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.Threading;
 13    using System.Threading.Tasks;
 14    using Microsoft.Azure.Search.Common;
 15    using Models;
 16    using Newtonsoft.Json;
 17    using Rest;
 18    using Rest.Azure;
 19    using Serialization;
 20
 21    internal class DocumentsOperations : IServiceOperations<SearchIndexClient>, IDocumentsOperations
 22    {
 223        internal static readonly string[] SelectAll = new[] { "*" };
 24
 25        /// <summary>
 26        /// Initializes a new instance of the DocumentsOperations class.
 27        /// </summary>
 28        /// <param name='client'>
 29        /// Reference to the service client.
 30        /// </param>
 68831        internal DocumentsOperations(SearchIndexClient client)
 32        {
 68833            Client = client ?? throw new ArgumentNullException("client");
 68834        }
 35
 36        /// <summary>
 37        /// Gets a reference to the SearchIndexClient
 38        /// </summary>
 266839        public SearchIndexClient Client { get; private set; }
 40
 41        public async Task<AzureOperationResponse<long>> CountWithHttpMessagesAsync(
 42            SearchRequestOptions searchRequestOptions = default(SearchRequestOptions),
 43            Dictionary<string, List<string>> customHeaders = null,
 44            CancellationToken cancellationToken = default(CancellationToken))
 45        {
 3846            AzureOperationResponse<long?> response =
 3847                await Client.DocumentsProxy.CountWithHttpMessagesAsync(
 3848                    searchRequestOptions,
 3849                    customHeaders,
 3850                    cancellationToken).ConfigureAwait(false);
 51
 3852            return new AzureOperationResponse<long>()
 3853            {
 3854                Body = response.Body.GetValueOrDefault(),
 3855                Request = response.Request,
 3856                RequestId = response.RequestId,
 3857                Response = response.Response
 3858            };
 3859        }
 60
 61        public Task<AzureOperationResponse<AutocompleteResult>> AutocompleteWithHttpMessagesAsync(
 62            string searchText,
 63            string suggesterName,
 64            AutocompleteParameters autocompleteParameters = null,
 65            SearchRequestOptions searchRequestOptions = default(SearchRequestOptions),
 66            Dictionary<string, List<string>> customHeaders = null,
 67            CancellationToken cancellationToken = default(CancellationToken))
 68        {
 7669            if (Client.UseHttpGetForQueries)
 70            {
 3871                return Client.DocumentsProxy.AutocompleteGetWithHttpMessagesAsync(
 3872                    searchText,
 3873                    suggesterName,
 3874                    searchRequestOptions,
 3875                    autocompleteParameters,
 3876                    customHeaders,
 3877                    cancellationToken);
 78            }
 79            else
 80            {
 3881                return Client.DocumentsProxy.AutocompletePostWithHttpMessagesAsync(
 3882                    (autocompleteParameters ?? new AutocompleteParameters()).ToRequest(searchText, suggesterName),
 3883                    searchRequestOptions,
 3884                    customHeaders,
 3885                    cancellationToken);
 86            }
 87        }
 88
 89        public Task<AzureOperationResponse<DocumentSearchResult<Document>>> ContinueSearchWithHttpMessagesAsync(
 90            SearchContinuationToken continuationToken,
 91            SearchRequestOptions searchRequestOptions = default(SearchRequestOptions),
 92            Dictionary<string, List<string>> customHeaders = null,
 93            CancellationToken cancellationToken = default(CancellationToken))
 94        {
 1295            var deserializerSettings = JsonUtility.CreateDocumentDeserializerSettings(Client.DeserializationSettings);
 96
 1297            return DoContinueSearchAsync<Document>(
 1298                continuationToken,
 1299                searchRequestOptions,
 12100                customHeaders,
 12101                cancellationToken,
 12102                deserializerSettings);
 103        }
 104
 105        public Task<AzureOperationResponse<DocumentSearchResult<T>>> ContinueSearchWithHttpMessagesAsync<T>(
 106            SearchContinuationToken continuationToken,
 107            SearchRequestOptions searchRequestOptions = default(SearchRequestOptions),
 108            Dictionary<string, List<string>> customHeaders = null,
 109            CancellationToken cancellationToken = default(CancellationToken))
 110        {
 20111            var deserializerSettings = JsonUtility.CreateTypedDeserializerSettings<T>(Client.DeserializationSettings);
 112
 20113            return DoContinueSearchAsync<T>(
 20114                continuationToken,
 20115                searchRequestOptions,
 20116                customHeaders,
 20117                cancellationToken,
 20118                deserializerSettings);
 119        }
 120
 121        public Task<AzureOperationResponse<Document>> GetWithHttpMessagesAsync(
 122            string key,
 123            IEnumerable<string> selectedFields,
 124            SearchRequestOptions searchRequestOptions = default(SearchRequestOptions),
 125            Dictionary<string, List<string>> customHeaders = null,
 126            CancellationToken cancellationToken = default(CancellationToken))
 127        {
 34128            JsonSerializerSettings jsonSerializerSettings =
 34129                JsonUtility.CreateDocumentDeserializerSettings(Client.DeserializationSettings);
 130
 34131            return Client.DocumentsProxy.GetWithHttpMessagesAsync<Document>(
 34132                key,
 34133                selectedFields.ToList(),
 34134                searchRequestOptions,
 34135                EnsureCustomHeaders(customHeaders),
 34136                cancellationToken,
 34137                responseDeserializerSettings: jsonSerializerSettings);
 138        }
 139
 140        public Task<AzureOperationResponse<T>> GetWithHttpMessagesAsync<T>(
 141            string key,
 142            IEnumerable<string> selectedFields,
 143            SearchRequestOptions searchRequestOptions = default(SearchRequestOptions),
 144            Dictionary<string, List<string>> customHeaders = null,
 145            CancellationToken cancellationToken = default(CancellationToken))
 146        {
 46147            JsonSerializerSettings jsonSerializerSettings =
 46148                JsonUtility.CreateTypedDeserializerSettings<T>(Client.DeserializationSettings);
 149
 46150            return Client.DocumentsProxy.GetWithHttpMessagesAsync<T>(
 46151                key,
 46152                selectedFields.ToList(),
 46153                searchRequestOptions,
 46154                EnsureCustomHeaders(customHeaders),
 46155                cancellationToken,
 46156                responseDeserializerSettings: jsonSerializerSettings);
 157        }
 158
 159        public Task<AzureOperationResponse<DocumentIndexResult>> IndexWithHttpMessagesAsync(
 160            IndexBatch<Document> batch,
 161            SearchRequestOptions searchRequestOptions = default(SearchRequestOptions),
 162            Dictionary<string, List<string>> customHeaders = null,
 163            CancellationToken cancellationToken = default(CancellationToken))
 164        {
 40165            JsonSerializerSettings jsonSettings = JsonUtility.CreateDocumentSerializerSettings(Client.SerializationSetti
 166
 40167            return DoIndexAsync(batch, searchRequestOptions, customHeaders, jsonSettings, cancellationToken);
 168        }
 169
 170        public Task<AzureOperationResponse<DocumentIndexResult>> IndexWithHttpMessagesAsync<T>(
 171            IndexBatch<T> batch,
 172            SearchRequestOptions searchRequestOptions = default(SearchRequestOptions),
 173            Dictionary<string, List<string>> customHeaders = null,
 174            CancellationToken cancellationToken = default(CancellationToken))
 175        {
 406176            bool useCamelCase = SerializePropertyNamesAsCamelCaseAttribute.IsDefinedOnType<T>();
 406177            JsonSerializerSettings jsonSettings = JsonUtility.CreateTypedSerializerSettings<T>(Client.SerializationSetti
 178
 406179            return DoIndexAsync(batch, searchRequestOptions, customHeaders, jsonSettings, cancellationToken);
 180        }
 181
 182        public Task<AzureOperationResponse<DocumentSearchResult<Document>>> SearchWithHttpMessagesAsync(
 183            string searchText,
 184            SearchParameters searchParameters,
 185            SearchRequestOptions searchRequestOptions = default(SearchRequestOptions),
 186            Dictionary<string, List<string>> customHeaders = null,
 187            CancellationToken cancellationToken = default(CancellationToken))
 188        {
 30189            var deserializerSettings = JsonUtility.CreateDocumentDeserializerSettings(Client.DeserializationSettings);
 190
 30191            return DoSearchAsync<Document>(
 30192                searchText,
 30193                searchParameters,
 30194                searchRequestOptions,
 30195                customHeaders,
 30196                deserializerSettings,
 30197                cancellationToken);
 198        }
 199
 200        public Task<AzureOperationResponse<DocumentSearchResult<T>>> SearchWithHttpMessagesAsync<T>(
 201            string searchText,
 202            SearchParameters searchParameters,
 203            SearchRequestOptions searchRequestOptions = default(SearchRequestOptions),
 204            Dictionary<string, List<string>> customHeaders = null,
 205            CancellationToken cancellationToken = default(CancellationToken))
 206        {
 112207            var deserializerSettings = JsonUtility.CreateTypedDeserializerSettings<T>(Client.DeserializationSettings);
 208
 112209            return DoSearchAsync<T>(
 112210                searchText,
 112211                searchParameters,
 112212                searchRequestOptions,
 112213                customHeaders,
 112214                deserializerSettings,
 112215                cancellationToken);
 216        }
 217
 218        public Task<AzureOperationResponse<DocumentSuggestResult<Document>>> SuggestWithHttpMessagesAsync(
 219            string searchText,
 220            string suggesterName,
 221            SuggestParameters suggestParameters,
 222            SearchRequestOptions searchRequestOptions = default(SearchRequestOptions),
 223            Dictionary<string, List<string>> customHeaders = null,
 224            CancellationToken cancellationToken = default(CancellationToken))
 225        {
 16226            var deserializerSettings = JsonUtility.CreateDocumentDeserializerSettings(Client.DeserializationSettings);
 227
 16228            return DoSuggestAsync<Document>(
 16229                searchText,
 16230                suggesterName,
 16231                suggestParameters,
 16232                searchRequestOptions,
 16233                customHeaders,
 16234                deserializerSettings,
 16235                cancellationToken);
 236        }
 237
 238        public Task<AzureOperationResponse<DocumentSuggestResult<T>>> SuggestWithHttpMessagesAsync<T>(
 239            string searchText,
 240            string suggesterName,
 241            SuggestParameters suggestParameters,
 242            SearchRequestOptions searchRequestOptions = default(SearchRequestOptions),
 243            Dictionary<string, List<string>> customHeaders = null,
 244            CancellationToken cancellationToken = default(CancellationToken))
 245        {
 56246            var deserializerSettings = JsonUtility.CreateTypedDeserializerSettings<T>(Client.DeserializationSettings);
 247
 56248            return DoSuggestAsync<T>(
 56249                searchText,
 56250                suggesterName,
 56251                suggestParameters,
 56252                searchRequestOptions,
 56253                customHeaders,
 56254                deserializerSettings,
 56255                cancellationToken);
 256        }
 257
 258        private static Dictionary<string, List<string>> EnsureCustomHeaders(Dictionary<string, List<string>> customHeade
 259        {
 260            const string Accept = nameof(Accept);
 261            const string AcceptValue = "application/json;odata.metadata=none";
 262
 772263            customHeaders = customHeaders ?? new Dictionary<string, List<string>>();
 264
 772265            if (!customHeaders.ContainsKey(Accept))
 266            {
 772267                customHeaders[Accept] = new List<string>() { AcceptValue };
 268            }
 269
 772270            return customHeaders;
 271        }
 272
 273        private async Task<AzureOperationResponse<DocumentIndexResult>> DoIndexAsync<T>(
 274            IndexBatch<T> batch,
 275            SearchRequestOptions searchRequestOptions,
 276            Dictionary<string, List<string>> customHeaders,
 277            JsonSerializerSettings jsonSettings,
 278            CancellationToken cancellationToken)
 279        {
 446280            var result =
 446281                await Client.DocumentsProxy.IndexWithHttpMessagesAsync(
 446282                    batch,
 446283                    searchRequestOptions,
 446284                    EnsureCustomHeaders(customHeaders),
 446285                    cancellationToken,
 446286                    requestSerializerSettings: jsonSettings).ConfigureAwait(false);
 287
 444288            if (result.Response.StatusCode == (HttpStatusCode)207)
 289            {
 8290                HttpRequestMessage httpRequest = result.Request;
 8291                HttpResponseMessage httpResponse = result.Response;
 292
 293                // NOTE: It is not possible to read the http request's string content property here,
 294                // via .NET framework's HttpClient, as the Content gets disposed as soon as the request is sent.
 295                // Thus the batch is re-serialized, which is what happens in the IndexWithHttpMessagesAsync() method
 296                // In .NET Core, the Content doesn't get disposed, so it's safe to read the string content here.
 297#if FullNetFx
 298                string requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(batch, jsonSettings);
 299#else
 8300                string requestContent = await httpRequest.Content.ReadAsStringAsync().ConfigureAwait(false);
 301#endif
 8302                string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 303
 8304                var exception =
 8305                    new IndexBatchException(result.Body)
 8306                    {
 8307                        Request = new HttpRequestMessageWrapper(httpRequest, requestContent),
 8308                        Response = new HttpResponseMessageWrapper(httpResponse, responseContent)
 8309                    };
 310
 8311                if (httpResponse.Headers.Contains("request-id"))
 312                {
 8313                    exception.RequestId = httpResponse.Headers.GetValues("request-id").FirstOrDefault();
 314                }
 315
 8316                result.Dispose();
 8317                throw exception;
 318            }
 319
 436320            return result;
 436321        }
 322
 323        private Task<AzureOperationResponse<DocumentSearchResult<T>>> DoContinueSearchAsync<T>(
 324            SearchContinuationToken continuationToken,
 325            SearchRequestOptions searchRequestOptions,
 326            Dictionary<string, List<string>> customHeaders,
 327            CancellationToken cancellationToken,
 328            JsonSerializerSettings deserializerSettings)
 329        {
 330            // Validate
 32331            if (Client.ApiVersion == null)
 332            {
 0333                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.ApiVersion");
 334            }
 335
 32336            Throw.IfArgumentNull(continuationToken, nameof(continuationToken));
 337
 32338            Guid? clientRequestId = searchRequestOptions?.ClientRequestId;
 339
 340            // Tracing
 32341            bool shouldTrace = ServiceClientTracing.IsEnabled;
 32342            string invocationId = null;
 32343            if (shouldTrace)
 344            {
 0345                invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0346                var tracingParameters = new Dictionary<string, object>
 0347                {
 0348                    ["continuationToken"] = continuationToken,
 0349                    ["clientRequestId"] = clientRequestId,
 0350                    ["cancellationToken"] = cancellationToken
 0351                };
 352
 0353                ServiceClientTracing.Enter(invocationId, this, "ContinueSearch", tracingParameters);
 354            }
 355
 32356            bool useGet = continuationToken.NextPageParameters == null;
 32357            if (useGet)
 358            {
 16359                return Client.DocumentsProxy.ContinueSearchGetWithHttpMessagesAsync<T>(
 16360                    continuationToken.NextLink,
 16361                    clientRequestId,
 16362                    EnsureCustomHeaders(customHeaders),
 16363                    shouldTrace,
 16364                    invocationId,
 16365                    cancellationToken,
 16366                    responseDeserializerSettings: deserializerSettings);
 367            }
 368            else
 369            {
 16370                return Client.DocumentsProxy.ContinueSearchPostWithHttpMessagesAsync<T>(
 16371                    continuationToken.NextLink,
 16372                    continuationToken.NextPageParameters,
 16373                    clientRequestId,
 16374                    EnsureCustomHeaders(customHeaders),
 16375                    shouldTrace,
 16376                    invocationId,
 16377                    cancellationToken,
 16378                    responseDeserializerSettings: deserializerSettings);
 379            }
 380        }
 381
 382        private Task<AzureOperationResponse<DocumentSearchResult<T>>> DoSearchAsync<T>(
 383            string searchText,
 384            SearchParameters searchParameters,
 385            SearchRequestOptions searchRequestOptions,
 386            Dictionary<string, List<string>> customHeaders,
 387            JsonSerializerSettings deserializerSettings,
 388            CancellationToken cancellationToken)
 389        {
 142390            searchText = searchText ?? "*";
 391
 142392            if (Client.UseHttpGetForQueries)
 393            {
 56394                return Client.DocumentsProxy.SearchGetWithHttpMessagesAsync<T>(
 56395                    searchText,
 56396                    searchParameters,
 56397                    searchRequestOptions,
 56398                    EnsureCustomHeaders(customHeaders),
 56399                    cancellationToken,
 56400                    responseDeserializerSettings: deserializerSettings);
 401            }
 402            else
 403            {
 86404                return Client.DocumentsProxy.SearchPostWithHttpMessagesAsync<T>(
 86405                    (searchParameters ?? new SearchParameters()).ToRequest(searchText),
 86406                    searchRequestOptions,
 86407                    EnsureCustomHeaders(customHeaders),
 86408                    cancellationToken,
 86409                    responseDeserializerSettings: deserializerSettings);
 410            }
 411        }
 412
 413        private Task<AzureOperationResponse<DocumentSuggestResult<T>>> DoSuggestAsync<T>(
 414            string searchText,
 415            string suggesterName,
 416            SuggestParameters suggestParameters,
 417            SearchRequestOptions searchRequestOptions,
 418            Dictionary<string, List<string>> customHeaders,
 419            JsonSerializerSettings deserializerSettings,
 420            CancellationToken cancellationToken)
 421        {
 72422            suggestParameters = suggestParameters ?? new SuggestParameters();
 423
 72424            if (Client.UseHttpGetForQueries)
 425            {
 30426                return Client.DocumentsProxy.SuggestGetWithHttpMessagesAsync<T>(
 30427                    searchText,
 30428                    suggesterName,
 30429                    suggestParameters.EnsureSelect(),
 30430                    searchRequestOptions,
 30431                    EnsureCustomHeaders(customHeaders),
 30432                    cancellationToken,
 30433                    responseDeserializerSettings: deserializerSettings);
 434            }
 435            else
 436            {
 42437                return Client.DocumentsProxy.SuggestPostWithHttpMessagesAsync<T>(
 42438                    suggestParameters.ToRequest(searchText, suggesterName),
 42439                    searchRequestOptions,
 42440                    EnsureCustomHeaders(customHeaders),
 42441                    cancellationToken,
 42442                    responseDeserializerSettings: deserializerSettings);
 443            }
 444        }
 445    }
 446}