< Summary

Class:Microsoft.Azure.Search.DataSourcesOperations
Assembly:Microsoft.Azure.Search.Service
File(s):C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Service\src\Customizations\DataSources\DataSourceOperations.Customization.cs
C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Service\src\Generated\DataSourcesOperations.cs
Covered lines:378
Uncovered lines:179
Coverable lines:557
Total lines:1286
Line coverage:67.8% (378 of 557)
Covered branches:222
Total branches:354
Branch coverage:62.7% (222 of 354)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
CreateOrUpdateWithHttpMessagesAsync(...)-100%50%
ExistsWithHttpMessagesAsync(...)-100%100%
.ctor(...)-80%50%
get_Client()-100%100%
CreateOrUpdateWithHttpMessagesAsync()-69.29%67.78%
DeleteWithHttpMessagesAsync()-71.03%65.28%
GetWithHttpMessagesAsync()-70%62.9%
ListWithHttpMessagesAsync()-54.17%50%
CreateWithHttpMessagesAsync()-71.43%65.15%

File(s)

C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Service\src\Customizations\DataSources\DataSourceOperations.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.Collections.Generic;
 8    using System.Threading;
 9    using System.Threading.Tasks;
 10    using Microsoft.Rest.Azure;
 11    using Models;
 12
 13    internal partial class DataSourcesOperations
 14    {
 15        /// <summary>
 16        /// Creates a new datasource or updates a datasource if it already
 17        /// exists.
 18        /// <see href="https://docs.microsoft.com/rest/api/searchservice/Update-Data-Source" />
 19        /// </summary>
 20        /// <param name='dataSource'>
 21        /// The definition of the datasource to create or update.
 22        /// </param>
 23        /// <param name='searchRequestOptions'>
 24        /// Additional parameters for the operation.
 25        /// </param>
 26        /// <param name='accessCondition'>
 27        /// Additional parameters for the operation.
 28        /// </param>
 29        /// <param name='customHeaders'>
 30        /// Headers that will be added to request.
 31        /// </param>
 32        /// <param name='cancellationToken'>
 33        /// The cancellation token.
 34        /// </param>
 35        /// <exception cref="CloudException">
 36        /// Thrown when the operation returned an invalid status code.
 37        /// </exception>
 38        /// <exception cref="Microsoft.Rest.SerializationException">
 39        /// Thrown when unable to deserialize the response.
 40        /// </exception>
 41        /// <exception cref="Microsoft.Rest.ValidationException">
 42        /// Thrown when a required parameter is null.
 43        /// </exception>
 44        /// <exception cref="System.ArgumentNullException">
 45        /// Thrown when a required parameter is null.
 46        /// </exception>
 47        /// <returns>
 48        /// A response object containing the response body and response headers.
 49        /// </returns>
 50        public Task<AzureOperationResponse<DataSource>> CreateOrUpdateWithHttpMessagesAsync(DataSource dataSource, Searc
 51        {
 3252            return CreateOrUpdateWithHttpMessagesAsync(dataSource?.Name, dataSource, searchRequestOptions, accessConditi
 53        }
 54
 55        /// <summary>
 56        /// Determines whether or not the given data source exists in the search service.
 57        /// </summary>
 58        /// <param name="dataSourceName">
 59        /// The name of the data source.
 60        /// </param>
 61        /// <param name='searchRequestOptions'>
 62        /// Additional parameters for the operation
 63        /// </param>
 64        /// <param name='customHeaders'>
 65        /// The headers that will be added to request.
 66        /// </param>
 67        /// <param name='cancellationToken'>
 68        /// The cancellation token.
 69        /// </param>
 70        /// <exception cref="CloudException">
 71        /// Thrown when the operation returned an invalid status code.
 72        /// </exception>
 73        /// <exception cref="Microsoft.Rest.SerializationException">
 74        /// Thrown when unable to deserialize the response.
 75        /// </exception>
 76        /// <exception cref="Microsoft.Rest.ValidationException">
 77        /// Thrown when a required parameter is null.
 78        /// </exception>
 79        /// <exception cref="System.ArgumentNullException">
 80        /// Thrown when a required parameter is null.
 81        /// </exception>
 82        /// <returns>
 83        /// A response with the value <c>true</c> if the data source exists; <c>false</c> otherwise.
 84        /// </returns>
 85        public Task<AzureOperationResponse<bool>> ExistsWithHttpMessagesAsync(
 86            string dataSourceName,
 87            SearchRequestOptions searchRequestOptions = default(SearchRequestOptions),
 88            Dictionary<string, List<string>> customHeaders = null,
 89            CancellationToken cancellationToken = default(CancellationToken))
 90        {
 491            return ExistsHelper.ExistsFromGetResponse(() =>
 892                this.GetWithHttpMessagesAsync(dataSourceName, searchRequestOptions, customHeaders, cancellationToken));
 93        }
 94    }
 95}

C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Service\src\Generated\DataSourcesOperations.cs

#LineLine coverage
 1// <auto-generated>
 2// Copyright (c) Microsoft Corporation. All rights reserved.
 3// Licensed under the MIT License. See License.txt in the project root for
 4// license information.
 5//
 6// Code generated by Microsoft (R) AutoRest Code Generator.
 7// Changes may cause incorrect behavior and will be lost if the code is
 8// regenerated.
 9// </auto-generated>
 10
 11namespace Microsoft.Azure.Search
 12{
 13    using Microsoft.Rest;
 14    using Microsoft.Rest.Azure;
 15    using Models;
 16    using Newtonsoft.Json;
 17    using System.Collections;
 18    using System.Collections.Generic;
 19    using System.Linq;
 20    using System.Net;
 21    using System.Net.Http;
 22    using System.Threading;
 23    using System.Threading.Tasks;
 24
 25    /// <summary>
 26    /// DataSourcesOperations operations.
 27    /// </summary>
 28    internal partial class DataSourcesOperations : IServiceOperations<SearchServiceClient>, IDataSourcesOperations
 29    {
 30        /// <summary>
 31        /// Initializes a new instance of the DataSourcesOperations class.
 32        /// </summary>
 33        /// <param name='client'>
 34        /// Reference to the service client.
 35        /// </param>
 36        /// <exception cref="System.ArgumentNullException">
 37        /// Thrown when a required parameter is null
 38        /// </exception>
 82439        internal DataSourcesOperations(SearchServiceClient client)
 40        {
 82441            if (client == null)
 42            {
 043                throw new System.ArgumentNullException("client");
 44            }
 82445            Client = client;
 82446        }
 47
 48        /// <summary>
 49        /// Gets a reference to the SearchServiceClient
 50        /// </summary>
 522251        public SearchServiceClient Client { get; private set; }
 52
 53        /// <summary>
 54        /// Creates a new datasource or updates a datasource if it already exists.
 55        /// <see href="https://docs.microsoft.com/rest/api/searchservice/Update-Data-Source" />
 56        /// </summary>
 57        /// <param name='dataSourceName'>
 58        /// The name of the datasource to create or update.
 59        /// </param>
 60        /// <param name='dataSource'>
 61        /// The definition of the datasource to create or update.
 62        /// </param>
 63        /// <param name='searchRequestOptions'>
 64        /// Additional parameters for the operation
 65        /// </param>
 66        /// <param name='accessCondition'>
 67        /// Additional parameters for the operation
 68        /// </param>
 69        /// <param name='customHeaders'>
 70        /// Headers that will be added to request.
 71        /// </param>
 72        /// <param name='cancellationToken'>
 73        /// The cancellation token.
 74        /// </param>
 75        /// <exception cref="CloudException">
 76        /// Thrown when the operation returned an invalid status code
 77        /// </exception>
 78        /// <exception cref="SerializationException">
 79        /// Thrown when unable to deserialize the response
 80        /// </exception>
 81        /// <exception cref="ValidationException">
 82        /// Thrown when a required parameter is null
 83        /// </exception>
 84        /// <exception cref="System.ArgumentNullException">
 85        /// Thrown when a required parameter is null
 86        /// </exception>
 87        /// <return>
 88        /// A response object containing the response body and response headers.
 89        /// </return>
 90        public async Task<AzureOperationResponse<DataSource>> CreateOrUpdateWithHttpMessagesAsync(string dataSourceName,
 91        {
 3292            if (Client.SearchServiceName == null)
 93            {
 094                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchServiceName");
 95            }
 3296            if (Client.SearchDnsSuffix == null)
 97            {
 098                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchDnsSuffix");
 99            }
 32100            if (dataSourceName == null)
 101            {
 0102                throw new ValidationException(ValidationRules.CannotBeNull, "dataSourceName");
 103            }
 32104            if (dataSource == null)
 105            {
 0106                throw new ValidationException(ValidationRules.CannotBeNull, "dataSource");
 107            }
 32108            if (dataSource != null)
 109            {
 32110                dataSource.Validate();
 111            }
 32112            if (Client.ApiVersion == null)
 113            {
 0114                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.ApiVersion");
 115            }
 32116            string prefer = "return=representation";
 32117            System.Guid? clientRequestId = default(System.Guid?);
 32118            if (searchRequestOptions != null)
 119            {
 0120                clientRequestId = searchRequestOptions.ClientRequestId;
 121            }
 32122            string ifMatch = default(string);
 32123            if (accessCondition != null)
 124            {
 22125                ifMatch = accessCondition.IfMatch;
 126            }
 32127            string ifNoneMatch = default(string);
 32128            if (accessCondition != null)
 129            {
 22130                ifNoneMatch = accessCondition.IfNoneMatch;
 131            }
 132            // Tracing
 32133            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 32134            string _invocationId = null;
 32135            if (_shouldTrace)
 136            {
 0137                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0138                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0139                tracingParameters.Add("dataSourceName", dataSourceName);
 0140                tracingParameters.Add("dataSource", dataSource);
 0141                tracingParameters.Add("prefer", prefer);
 0142                tracingParameters.Add("clientRequestId", clientRequestId);
 0143                tracingParameters.Add("ifMatch", ifMatch);
 0144                tracingParameters.Add("ifNoneMatch", ifNoneMatch);
 0145                tracingParameters.Add("cancellationToken", cancellationToken);
 0146                ServiceClientTracing.Enter(_invocationId, this, "CreateOrUpdate", tracingParameters);
 147            }
 148            // Construct URL
 32149            var _baseUrl = Client.BaseUri;
 32150            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "datasources('{dataSourceName}')";
 32151            _url = _url.Replace("{searchServiceName}", Client.SearchServiceName);
 32152            _url = _url.Replace("{searchDnsSuffix}", Client.SearchDnsSuffix);
 32153            _url = _url.Replace("{dataSourceName}", System.Uri.EscapeDataString(dataSourceName));
 32154            List<string> _queryParameters = new List<string>();
 32155            if (Client.ApiVersion != null)
 156            {
 32157                _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion)));
 158            }
 32159            if (_queryParameters.Count > 0)
 160            {
 32161                _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters);
 162            }
 163            // Create HTTP transport objects
 32164            var _httpRequest = new HttpRequestMessage();
 32165            HttpResponseMessage _httpResponse = null;
 32166            _httpRequest.Method = new HttpMethod("PUT");
 32167            _httpRequest.RequestUri = new System.Uri(_url);
 168            // Set Headers
 32169            if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value)
 170            {
 32171                _httpRequest.Headers.TryAddWithoutValidation("client-request-id", System.Guid.NewGuid().ToString());
 172            }
 32173            if (prefer != null)
 174            {
 32175                if (_httpRequest.Headers.Contains("Prefer"))
 176                {
 0177                    _httpRequest.Headers.Remove("Prefer");
 178                }
 32179                _httpRequest.Headers.TryAddWithoutValidation("Prefer", prefer);
 180            }
 32181            if (Client.AcceptLanguage != null)
 182            {
 32183                if (_httpRequest.Headers.Contains("accept-language"))
 184                {
 0185                    _httpRequest.Headers.Remove("accept-language");
 186                }
 32187                _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage);
 188            }
 32189            if (clientRequestId != null)
 190            {
 0191                if (_httpRequest.Headers.Contains("client-request-id"))
 192                {
 0193                    _httpRequest.Headers.Remove("client-request-id");
 194                }
 0195                _httpRequest.Headers.TryAddWithoutValidation("client-request-id", Rest.Serialization.SafeJsonConvert.Ser
 196            }
 32197            if (ifMatch != null)
 198            {
 8199                if (_httpRequest.Headers.Contains("If-Match"))
 200                {
 0201                    _httpRequest.Headers.Remove("If-Match");
 202                }
 8203                _httpRequest.Headers.TryAddWithoutValidation("If-Match", ifMatch);
 204            }
 32205            if (ifNoneMatch != null)
 206            {
 4207                if (_httpRequest.Headers.Contains("If-None-Match"))
 208                {
 0209                    _httpRequest.Headers.Remove("If-None-Match");
 210                }
 4211                _httpRequest.Headers.TryAddWithoutValidation("If-None-Match", ifNoneMatch);
 212            }
 213
 214
 32215            if (customHeaders != null)
 216            {
 0217                foreach(var _header in customHeaders)
 218                {
 0219                    if (_httpRequest.Headers.Contains(_header.Key))
 220                    {
 0221                        _httpRequest.Headers.Remove(_header.Key);
 222                    }
 0223                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 224                }
 225            }
 226
 227            // Serialize Request
 32228            string _requestContent = null;
 32229            if(dataSource != null)
 230            {
 32231                _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(dataSource, Client.SerializationSet
 32232                _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8);
 32233                _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio
 234            }
 235            // Set Credentials
 32236            if (Client.Credentials != null)
 237            {
 32238                cancellationToken.ThrowIfCancellationRequested();
 32239                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 240            }
 241            // Send Request
 32242            if (_shouldTrace)
 243            {
 0244                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 245            }
 32246            cancellationToken.ThrowIfCancellationRequested();
 32247            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 32248            if (_shouldTrace)
 249            {
 0250                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 251            }
 32252            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 32253            cancellationToken.ThrowIfCancellationRequested();
 32254            string _responseContent = null;
 32255            if ((int)_statusCode != 200 && (int)_statusCode != 201)
 256            {
 6257                var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode
 258                try
 259                {
 6260                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 6261                    CloudError _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC
 6262                    if (_errorBody != null)
 263                    {
 6264                        ex = new CloudException(_errorBody.Message);
 6265                        ex.Body = _errorBody;
 266                    }
 6267                }
 0268                catch (JsonException)
 269                {
 270                    // Ignore the exception
 0271                }
 6272                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 6273                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 6274                if (_httpResponse.Headers.Contains("request-id"))
 275                {
 6276                    ex.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault();
 277                }
 6278                if (_shouldTrace)
 279                {
 0280                    ServiceClientTracing.Error(_invocationId, ex);
 281                }
 6282                _httpRequest.Dispose();
 6283                if (_httpResponse != null)
 284                {
 6285                    _httpResponse.Dispose();
 286                }
 6287                throw ex;
 288            }
 289            // Create Result
 26290            var _result = new AzureOperationResponse<DataSource>();
 26291            _result.Request = _httpRequest;
 26292            _result.Response = _httpResponse;
 26293            if (_httpResponse.Headers.Contains("request-id"))
 294            {
 26295                _result.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault();
 296            }
 297            // Deserialize Response
 26298            if ((int)_statusCode == 200)
 299            {
 10300                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 301                try
 302                {
 10303                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<DataSource>(_responseContent, Cl
 10304                }
 0305                catch (JsonException ex)
 306                {
 0307                    _httpRequest.Dispose();
 0308                    if (_httpResponse != null)
 309                    {
 0310                        _httpResponse.Dispose();
 311                    }
 0312                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 313                }
 314            }
 315            // Deserialize Response
 26316            if ((int)_statusCode == 201)
 317            {
 16318                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 319                try
 320                {
 16321                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<DataSource>(_responseContent, Cl
 16322                }
 0323                catch (JsonException ex)
 324                {
 0325                    _httpRequest.Dispose();
 0326                    if (_httpResponse != null)
 327                    {
 0328                        _httpResponse.Dispose();
 329                    }
 0330                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 331                }
 332            }
 26333            if (_shouldTrace)
 334            {
 0335                ServiceClientTracing.Exit(_invocationId, _result);
 336            }
 26337            return _result;
 26338        }
 339
 340        /// <summary>
 341        /// Deletes a datasource.
 342        /// <see href="https://docs.microsoft.com/rest/api/searchservice/Delete-Data-Source" />
 343        /// </summary>
 344        /// <param name='dataSourceName'>
 345        /// The name of the datasource to delete.
 346        /// </param>
 347        /// <param name='searchRequestOptions'>
 348        /// Additional parameters for the operation
 349        /// </param>
 350        /// <param name='accessCondition'>
 351        /// Additional parameters for the operation
 352        /// </param>
 353        /// <param name='customHeaders'>
 354        /// Headers that will be added to request.
 355        /// </param>
 356        /// <param name='cancellationToken'>
 357        /// The cancellation token.
 358        /// </param>
 359        /// <exception cref="CloudException">
 360        /// Thrown when the operation returned an invalid status code
 361        /// </exception>
 362        /// <exception cref="ValidationException">
 363        /// Thrown when a required parameter is null
 364        /// </exception>
 365        /// <exception cref="System.ArgumentNullException">
 366        /// Thrown when a required parameter is null
 367        /// </exception>
 368        /// <return>
 369        /// A response object containing the response body and response headers.
 370        /// </return>
 371        public async Task<AzureOperationResponse> DeleteWithHttpMessagesAsync(string dataSourceName, SearchRequestOption
 372        {
 78373            if (Client.SearchServiceName == null)
 374            {
 0375                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchServiceName");
 376            }
 78377            if (Client.SearchDnsSuffix == null)
 378            {
 0379                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchDnsSuffix");
 380            }
 78381            if (dataSourceName == null)
 382            {
 0383                throw new ValidationException(ValidationRules.CannotBeNull, "dataSourceName");
 384            }
 78385            if (Client.ApiVersion == null)
 386            {
 0387                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.ApiVersion");
 388            }
 78389            System.Guid? clientRequestId = default(System.Guid?);
 78390            if (searchRequestOptions != null)
 391            {
 0392                clientRequestId = searchRequestOptions.ClientRequestId;
 393            }
 78394            string ifMatch = default(string);
 78395            if (accessCondition != null)
 396            {
 8397                ifMatch = accessCondition.IfMatch;
 398            }
 78399            string ifNoneMatch = default(string);
 78400            if (accessCondition != null)
 401            {
 8402                ifNoneMatch = accessCondition.IfNoneMatch;
 403            }
 404            // Tracing
 78405            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 78406            string _invocationId = null;
 78407            if (_shouldTrace)
 408            {
 0409                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0410                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0411                tracingParameters.Add("dataSourceName", dataSourceName);
 0412                tracingParameters.Add("clientRequestId", clientRequestId);
 0413                tracingParameters.Add("ifMatch", ifMatch);
 0414                tracingParameters.Add("ifNoneMatch", ifNoneMatch);
 0415                tracingParameters.Add("cancellationToken", cancellationToken);
 0416                ServiceClientTracing.Enter(_invocationId, this, "Delete", tracingParameters);
 417            }
 418            // Construct URL
 78419            var _baseUrl = Client.BaseUri;
 78420            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "datasources('{dataSourceName}')";
 78421            _url = _url.Replace("{searchServiceName}", Client.SearchServiceName);
 78422            _url = _url.Replace("{searchDnsSuffix}", Client.SearchDnsSuffix);
 78423            _url = _url.Replace("{dataSourceName}", System.Uri.EscapeDataString(dataSourceName));
 78424            List<string> _queryParameters = new List<string>();
 78425            if (Client.ApiVersion != null)
 426            {
 78427                _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion)));
 428            }
 78429            if (_queryParameters.Count > 0)
 430            {
 78431                _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters);
 432            }
 433            // Create HTTP transport objects
 78434            var _httpRequest = new HttpRequestMessage();
 78435            HttpResponseMessage _httpResponse = null;
 78436            _httpRequest.Method = new HttpMethod("DELETE");
 78437            _httpRequest.RequestUri = new System.Uri(_url);
 438            // Set Headers
 78439            if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value)
 440            {
 78441                _httpRequest.Headers.TryAddWithoutValidation("client-request-id", System.Guid.NewGuid().ToString());
 442            }
 78443            if (Client.AcceptLanguage != null)
 444            {
 78445                if (_httpRequest.Headers.Contains("accept-language"))
 446                {
 0447                    _httpRequest.Headers.Remove("accept-language");
 448                }
 78449                _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage);
 450            }
 78451            if (clientRequestId != null)
 452            {
 0453                if (_httpRequest.Headers.Contains("client-request-id"))
 454                {
 0455                    _httpRequest.Headers.Remove("client-request-id");
 456                }
 0457                _httpRequest.Headers.TryAddWithoutValidation("client-request-id", Rest.Serialization.SafeJsonConvert.Ser
 458            }
 78459            if (ifMatch != null)
 460            {
 8461                if (_httpRequest.Headers.Contains("If-Match"))
 462                {
 0463                    _httpRequest.Headers.Remove("If-Match");
 464                }
 8465                _httpRequest.Headers.TryAddWithoutValidation("If-Match", ifMatch);
 466            }
 78467            if (ifNoneMatch != null)
 468            {
 0469                if (_httpRequest.Headers.Contains("If-None-Match"))
 470                {
 0471                    _httpRequest.Headers.Remove("If-None-Match");
 472                }
 0473                _httpRequest.Headers.TryAddWithoutValidation("If-None-Match", ifNoneMatch);
 474            }
 475
 476
 78477            if (customHeaders != null)
 478            {
 0479                foreach(var _header in customHeaders)
 480                {
 0481                    if (_httpRequest.Headers.Contains(_header.Key))
 482                    {
 0483                        _httpRequest.Headers.Remove(_header.Key);
 484                    }
 0485                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 486                }
 487            }
 488
 489            // Serialize Request
 78490            string _requestContent = null;
 491            // Set Credentials
 78492            if (Client.Credentials != null)
 493            {
 78494                cancellationToken.ThrowIfCancellationRequested();
 78495                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 496            }
 497            // Send Request
 78498            if (_shouldTrace)
 499            {
 0500                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 501            }
 78502            cancellationToken.ThrowIfCancellationRequested();
 78503            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 78504            if (_shouldTrace)
 505            {
 0506                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 507            }
 78508            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 78509            cancellationToken.ThrowIfCancellationRequested();
 78510            string _responseContent = null;
 78511            if ((int)_statusCode != 204 && (int)_statusCode != 404)
 512            {
 4513                var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode
 514                try
 515                {
 4516                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 4517                    CloudError _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC
 4518                    if (_errorBody != null)
 519                    {
 4520                        ex = new CloudException(_errorBody.Message);
 4521                        ex.Body = _errorBody;
 522                    }
 4523                }
 0524                catch (JsonException)
 525                {
 526                    // Ignore the exception
 0527                }
 4528                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 4529                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 4530                if (_httpResponse.Headers.Contains("request-id"))
 531                {
 4532                    ex.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault();
 533                }
 4534                if (_shouldTrace)
 535                {
 0536                    ServiceClientTracing.Error(_invocationId, ex);
 537                }
 4538                _httpRequest.Dispose();
 4539                if (_httpResponse != null)
 540                {
 4541                    _httpResponse.Dispose();
 542                }
 4543                throw ex;
 544            }
 545            // Create Result
 74546            var _result = new AzureOperationResponse();
 74547            _result.Request = _httpRequest;
 74548            _result.Response = _httpResponse;
 74549            if (_httpResponse.Headers.Contains("request-id"))
 550            {
 74551                _result.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault();
 552            }
 74553            if (_shouldTrace)
 554            {
 0555                ServiceClientTracing.Exit(_invocationId, _result);
 556            }
 74557            return _result;
 74558        }
 559
 560        /// <summary>
 561        /// Retrieves a datasource definition.
 562        /// <see href="https://docs.microsoft.com/rest/api/searchservice/Get-Data-Source" />
 563        /// </summary>
 564        /// <param name='dataSourceName'>
 565        /// The name of the datasource to retrieve.
 566        /// </param>
 567        /// <param name='searchRequestOptions'>
 568        /// Additional parameters for the operation
 569        /// </param>
 570        /// <param name='customHeaders'>
 571        /// Headers that will be added to request.
 572        /// </param>
 573        /// <param name='cancellationToken'>
 574        /// The cancellation token.
 575        /// </param>
 576        /// <exception cref="CloudException">
 577        /// Thrown when the operation returned an invalid status code
 578        /// </exception>
 579        /// <exception cref="SerializationException">
 580        /// Thrown when unable to deserialize the response
 581        /// </exception>
 582        /// <exception cref="ValidationException">
 583        /// Thrown when a required parameter is null
 584        /// </exception>
 585        /// <exception cref="System.ArgumentNullException">
 586        /// Thrown when a required parameter is null
 587        /// </exception>
 588        /// <return>
 589        /// A response object containing the response body and response headers.
 590        /// </return>
 591        public async Task<AzureOperationResponse<DataSource>> GetWithHttpMessagesAsync(string dataSourceName, SearchRequ
 592        {
 38593            if (Client.SearchServiceName == null)
 594            {
 0595                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchServiceName");
 596            }
 38597            if (Client.SearchDnsSuffix == null)
 598            {
 0599                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchDnsSuffix");
 600            }
 38601            if (dataSourceName == null)
 602            {
 0603                throw new ValidationException(ValidationRules.CannotBeNull, "dataSourceName");
 604            }
 38605            if (Client.ApiVersion == null)
 606            {
 0607                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.ApiVersion");
 608            }
 38609            System.Guid? clientRequestId = default(System.Guid?);
 38610            if (searchRequestOptions != null)
 611            {
 0612                clientRequestId = searchRequestOptions.ClientRequestId;
 613            }
 614            // Tracing
 38615            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 38616            string _invocationId = null;
 38617            if (_shouldTrace)
 618            {
 0619                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0620                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0621                tracingParameters.Add("dataSourceName", dataSourceName);
 0622                tracingParameters.Add("clientRequestId", clientRequestId);
 0623                tracingParameters.Add("cancellationToken", cancellationToken);
 0624                ServiceClientTracing.Enter(_invocationId, this, "Get", tracingParameters);
 625            }
 626            // Construct URL
 38627            var _baseUrl = Client.BaseUri;
 38628            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "datasources('{dataSourceName}')";
 38629            _url = _url.Replace("{searchServiceName}", Client.SearchServiceName);
 38630            _url = _url.Replace("{searchDnsSuffix}", Client.SearchDnsSuffix);
 38631            _url = _url.Replace("{dataSourceName}", System.Uri.EscapeDataString(dataSourceName));
 38632            List<string> _queryParameters = new List<string>();
 38633            if (Client.ApiVersion != null)
 634            {
 38635                _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion)));
 636            }
 38637            if (_queryParameters.Count > 0)
 638            {
 38639                _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters);
 640            }
 641            // Create HTTP transport objects
 38642            var _httpRequest = new HttpRequestMessage();
 38643            HttpResponseMessage _httpResponse = null;
 38644            _httpRequest.Method = new HttpMethod("GET");
 38645            _httpRequest.RequestUri = new System.Uri(_url);
 646            // Set Headers
 38647            if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value)
 648            {
 38649                _httpRequest.Headers.TryAddWithoutValidation("client-request-id", System.Guid.NewGuid().ToString());
 650            }
 38651            if (Client.AcceptLanguage != null)
 652            {
 38653                if (_httpRequest.Headers.Contains("accept-language"))
 654                {
 0655                    _httpRequest.Headers.Remove("accept-language");
 656                }
 38657                _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage);
 658            }
 38659            if (clientRequestId != null)
 660            {
 0661                if (_httpRequest.Headers.Contains("client-request-id"))
 662                {
 0663                    _httpRequest.Headers.Remove("client-request-id");
 664                }
 0665                _httpRequest.Headers.TryAddWithoutValidation("client-request-id", Rest.Serialization.SafeJsonConvert.Ser
 666            }
 667
 668
 38669            if (customHeaders != null)
 670            {
 0671                foreach(var _header in customHeaders)
 672                {
 0673                    if (_httpRequest.Headers.Contains(_header.Key))
 674                    {
 0675                        _httpRequest.Headers.Remove(_header.Key);
 676                    }
 0677                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 678                }
 679            }
 680
 681            // Serialize Request
 38682            string _requestContent = null;
 683            // Set Credentials
 38684            if (Client.Credentials != null)
 685            {
 38686                cancellationToken.ThrowIfCancellationRequested();
 38687                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 688            }
 689            // Send Request
 38690            if (_shouldTrace)
 691            {
 0692                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 693            }
 38694            cancellationToken.ThrowIfCancellationRequested();
 38695            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 38696            if (_shouldTrace)
 697            {
 0698                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 699            }
 38700            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 38701            cancellationToken.ThrowIfCancellationRequested();
 38702            string _responseContent = null;
 38703            if ((int)_statusCode != 200)
 704            {
 4705                var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode
 706                try
 707                {
 4708                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 4709                    CloudError _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC
 4710                    if (_errorBody != null)
 711                    {
 4712                        ex = new CloudException(_errorBody.Message);
 4713                        ex.Body = _errorBody;
 714                    }
 4715                }
 0716                catch (JsonException)
 717                {
 718                    // Ignore the exception
 0719                }
 4720                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 4721                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 4722                if (_httpResponse.Headers.Contains("request-id"))
 723                {
 4724                    ex.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault();
 725                }
 4726                if (_shouldTrace)
 727                {
 0728                    ServiceClientTracing.Error(_invocationId, ex);
 729                }
 4730                _httpRequest.Dispose();
 4731                if (_httpResponse != null)
 732                {
 4733                    _httpResponse.Dispose();
 734                }
 4735                throw ex;
 736            }
 737            // Create Result
 34738            var _result = new AzureOperationResponse<DataSource>();
 34739            _result.Request = _httpRequest;
 34740            _result.Response = _httpResponse;
 34741            if (_httpResponse.Headers.Contains("request-id"))
 742            {
 34743                _result.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault();
 744            }
 745            // Deserialize Response
 34746            if ((int)_statusCode == 200)
 747            {
 34748                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 749                try
 750                {
 34751                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<DataSource>(_responseContent, Cl
 34752                }
 0753                catch (JsonException ex)
 754                {
 0755                    _httpRequest.Dispose();
 0756                    if (_httpResponse != null)
 757                    {
 0758                        _httpResponse.Dispose();
 759                    }
 0760                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 761                }
 762            }
 34763            if (_shouldTrace)
 764            {
 0765                ServiceClientTracing.Exit(_invocationId, _result);
 766            }
 34767            return _result;
 34768        }
 769
 770        /// <summary>
 771        /// Lists all datasources available for a search service.
 772        /// <see href="https://docs.microsoft.com/rest/api/searchservice/List-Data-Sources" />
 773        /// </summary>
 774        /// <param name='searchRequestOptions'>
 775        /// Additional parameters for the operation
 776        /// </param>
 777        /// <param name='customHeaders'>
 778        /// Headers that will be added to request.
 779        /// </param>
 780        /// <param name='cancellationToken'>
 781        /// The cancellation token.
 782        /// </param>
 783        /// <exception cref="CloudException">
 784        /// Thrown when the operation returned an invalid status code
 785        /// </exception>
 786        /// <exception cref="SerializationException">
 787        /// Thrown when unable to deserialize the response
 788        /// </exception>
 789        /// <exception cref="ValidationException">
 790        /// Thrown when a required parameter is null
 791        /// </exception>
 792        /// <exception cref="System.ArgumentNullException">
 793        /// Thrown when a required parameter is null
 794        /// </exception>
 795        /// <return>
 796        /// A response object containing the response body and response headers.
 797        /// </return>
 798        public async Task<AzureOperationResponse<DataSourceListResult>> ListWithHttpMessagesAsync(SearchRequestOptions s
 799        {
 2800            if (Client.SearchServiceName == null)
 801            {
 0802                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchServiceName");
 803            }
 2804            if (Client.SearchDnsSuffix == null)
 805            {
 0806                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchDnsSuffix");
 807            }
 2808            if (Client.ApiVersion == null)
 809            {
 0810                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.ApiVersion");
 811            }
 2812            System.Guid? clientRequestId = default(System.Guid?);
 2813            if (searchRequestOptions != null)
 814            {
 0815                clientRequestId = searchRequestOptions.ClientRequestId;
 816            }
 817            // Tracing
 2818            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 2819            string _invocationId = null;
 2820            if (_shouldTrace)
 821            {
 0822                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0823                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0824                tracingParameters.Add("clientRequestId", clientRequestId);
 0825                tracingParameters.Add("cancellationToken", cancellationToken);
 0826                ServiceClientTracing.Enter(_invocationId, this, "List", tracingParameters);
 827            }
 828            // Construct URL
 2829            var _baseUrl = Client.BaseUri;
 2830            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "datasources";
 2831            _url = _url.Replace("{searchServiceName}", Client.SearchServiceName);
 2832            _url = _url.Replace("{searchDnsSuffix}", Client.SearchDnsSuffix);
 2833            List<string> _queryParameters = new List<string>();
 2834            if (Client.ApiVersion != null)
 835            {
 2836                _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion)));
 837            }
 2838            if (_queryParameters.Count > 0)
 839            {
 2840                _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters);
 841            }
 842            // Create HTTP transport objects
 2843            var _httpRequest = new HttpRequestMessage();
 2844            HttpResponseMessage _httpResponse = null;
 2845            _httpRequest.Method = new HttpMethod("GET");
 2846            _httpRequest.RequestUri = new System.Uri(_url);
 847            // Set Headers
 2848            if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value)
 849            {
 2850                _httpRequest.Headers.TryAddWithoutValidation("client-request-id", System.Guid.NewGuid().ToString());
 851            }
 2852            if (Client.AcceptLanguage != null)
 853            {
 2854                if (_httpRequest.Headers.Contains("accept-language"))
 855                {
 0856                    _httpRequest.Headers.Remove("accept-language");
 857                }
 2858                _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage);
 859            }
 2860            if (clientRequestId != null)
 861            {
 0862                if (_httpRequest.Headers.Contains("client-request-id"))
 863                {
 0864                    _httpRequest.Headers.Remove("client-request-id");
 865                }
 0866                _httpRequest.Headers.TryAddWithoutValidation("client-request-id", Rest.Serialization.SafeJsonConvert.Ser
 867            }
 868
 869
 2870            if (customHeaders != null)
 871            {
 0872                foreach(var _header in customHeaders)
 873                {
 0874                    if (_httpRequest.Headers.Contains(_header.Key))
 875                    {
 0876                        _httpRequest.Headers.Remove(_header.Key);
 877                    }
 0878                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 879                }
 880            }
 881
 882            // Serialize Request
 2883            string _requestContent = null;
 884            // Set Credentials
 2885            if (Client.Credentials != null)
 886            {
 2887                cancellationToken.ThrowIfCancellationRequested();
 2888                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 889            }
 890            // Send Request
 2891            if (_shouldTrace)
 892            {
 0893                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 894            }
 2895            cancellationToken.ThrowIfCancellationRequested();
 2896            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 2897            if (_shouldTrace)
 898            {
 0899                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 900            }
 2901            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 2902            cancellationToken.ThrowIfCancellationRequested();
 2903            string _responseContent = null;
 2904            if ((int)_statusCode != 200)
 905            {
 0906                var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode
 907                try
 908                {
 0909                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 0910                    CloudError _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC
 0911                    if (_errorBody != null)
 912                    {
 0913                        ex = new CloudException(_errorBody.Message);
 0914                        ex.Body = _errorBody;
 915                    }
 0916                }
 0917                catch (JsonException)
 918                {
 919                    // Ignore the exception
 0920                }
 0921                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 0922                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 0923                if (_httpResponse.Headers.Contains("request-id"))
 924                {
 0925                    ex.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault();
 926                }
 0927                if (_shouldTrace)
 928                {
 0929                    ServiceClientTracing.Error(_invocationId, ex);
 930                }
 0931                _httpRequest.Dispose();
 0932                if (_httpResponse != null)
 933                {
 0934                    _httpResponse.Dispose();
 935                }
 0936                throw ex;
 937            }
 938            // Create Result
 2939            var _result = new AzureOperationResponse<DataSourceListResult>();
 2940            _result.Request = _httpRequest;
 2941            _result.Response = _httpResponse;
 2942            if (_httpResponse.Headers.Contains("request-id"))
 943            {
 2944                _result.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault();
 945            }
 946            // Deserialize Response
 2947            if ((int)_statusCode == 200)
 948            {
 2949                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 950                try
 951                {
 2952                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<DataSourceListResult>(_responseC
 2953                }
 0954                catch (JsonException ex)
 955                {
 0956                    _httpRequest.Dispose();
 0957                    if (_httpResponse != null)
 958                    {
 0959                        _httpResponse.Dispose();
 960                    }
 0961                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 962                }
 963            }
 2964            if (_shouldTrace)
 965            {
 0966                ServiceClientTracing.Exit(_invocationId, _result);
 967            }
 2968            return _result;
 2969        }
 970
 971        /// <summary>
 972        /// Creates a new datasource.
 973        /// <see href="https://docs.microsoft.com/rest/api/searchservice/Create-Data-Source" />
 974        /// </summary>
 975        /// <param name='dataSource'>
 976        /// The definition of the datasource to create.
 977        /// </param>
 978        /// <param name='searchRequestOptions'>
 979        /// Additional parameters for the operation
 980        /// </param>
 981        /// <param name='customHeaders'>
 982        /// Headers that will be added to request.
 983        /// </param>
 984        /// <param name='cancellationToken'>
 985        /// The cancellation token.
 986        /// </param>
 987        /// <exception cref="CloudException">
 988        /// Thrown when the operation returned an invalid status code
 989        /// </exception>
 990        /// <exception cref="SerializationException">
 991        /// Thrown when unable to deserialize the response
 992        /// </exception>
 993        /// <exception cref="ValidationException">
 994        /// Thrown when a required parameter is null
 995        /// </exception>
 996        /// <exception cref="System.ArgumentNullException">
 997        /// Thrown when a required parameter is null
 998        /// </exception>
 999        /// <return>
 1000        /// A response object containing the response body and response headers.
 1001        /// </return>
 1002        public async Task<AzureOperationResponse<DataSource>> CreateWithHttpMessagesAsync(DataSource dataSource, SearchR
 1003        {
 1201004            if (Client.SearchServiceName == null)
 1005            {
 01006                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchServiceName");
 1007            }
 1201008            if (Client.SearchDnsSuffix == null)
 1009            {
 01010                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchDnsSuffix");
 1011            }
 1201012            if (dataSource == null)
 1013            {
 01014                throw new ValidationException(ValidationRules.CannotBeNull, "dataSource");
 1015            }
 1201016            if (dataSource != null)
 1017            {
 1201018                dataSource.Validate();
 1019            }
 1201020            if (Client.ApiVersion == null)
 1021            {
 01022                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.ApiVersion");
 1023            }
 1201024            System.Guid? clientRequestId = default(System.Guid?);
 1201025            if (searchRequestOptions != null)
 1026            {
 01027                clientRequestId = searchRequestOptions.ClientRequestId;
 1028            }
 1029            // Tracing
 1201030            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 1201031            string _invocationId = null;
 1201032            if (_shouldTrace)
 1033            {
 01034                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 01035                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 01036                tracingParameters.Add("dataSource", dataSource);
 01037                tracingParameters.Add("clientRequestId", clientRequestId);
 01038                tracingParameters.Add("cancellationToken", cancellationToken);
 01039                ServiceClientTracing.Enter(_invocationId, this, "Create", tracingParameters);
 1040            }
 1041            // Construct URL
 1201042            var _baseUrl = Client.BaseUri;
 1201043            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "datasources";
 1201044            _url = _url.Replace("{searchServiceName}", Client.SearchServiceName);
 1201045            _url = _url.Replace("{searchDnsSuffix}", Client.SearchDnsSuffix);
 1201046            List<string> _queryParameters = new List<string>();
 1201047            if (Client.ApiVersion != null)
 1048            {
 1201049                _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion)));
 1050            }
 1201051            if (_queryParameters.Count > 0)
 1052            {
 1201053                _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters);
 1054            }
 1055            // Create HTTP transport objects
 1201056            var _httpRequest = new HttpRequestMessage();
 1201057            HttpResponseMessage _httpResponse = null;
 1201058            _httpRequest.Method = new HttpMethod("POST");
 1201059            _httpRequest.RequestUri = new System.Uri(_url);
 1060            // Set Headers
 1201061            if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value)
 1062            {
 1201063                _httpRequest.Headers.TryAddWithoutValidation("client-request-id", System.Guid.NewGuid().ToString());
 1064            }
 1201065            if (Client.AcceptLanguage != null)
 1066            {
 1201067                if (_httpRequest.Headers.Contains("accept-language"))
 1068                {
 01069                    _httpRequest.Headers.Remove("accept-language");
 1070                }
 1201071                _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage);
 1072            }
 1201073            if (clientRequestId != null)
 1074            {
 01075                if (_httpRequest.Headers.Contains("client-request-id"))
 1076                {
 01077                    _httpRequest.Headers.Remove("client-request-id");
 1078                }
 01079                _httpRequest.Headers.TryAddWithoutValidation("client-request-id", Rest.Serialization.SafeJsonConvert.Ser
 1080            }
 1081
 1082
 1201083            if (customHeaders != null)
 1084            {
 01085                foreach(var _header in customHeaders)
 1086                {
 01087                    if (_httpRequest.Headers.Contains(_header.Key))
 1088                    {
 01089                        _httpRequest.Headers.Remove(_header.Key);
 1090                    }
 01091                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 1092                }
 1093            }
 1094
 1095            // Serialize Request
 1201096            string _requestContent = null;
 1201097            if(dataSource != null)
 1098            {
 1201099                _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(dataSource, Client.SerializationSet
 1201100                _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8);
 1201101                _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio
 1102            }
 1103            // Set Credentials
 1201104            if (Client.Credentials != null)
 1105            {
 1201106                cancellationToken.ThrowIfCancellationRequested();
 1201107                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 1108            }
 1109            // Send Request
 1201110            if (_shouldTrace)
 1111            {
 01112                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 1113            }
 1201114            cancellationToken.ThrowIfCancellationRequested();
 1201115            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 1201116            if (_shouldTrace)
 1117            {
 01118                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 1119            }
 1201120            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 1201121            cancellationToken.ThrowIfCancellationRequested();
 1201122            string _responseContent = null;
 1201123            if ((int)_statusCode != 201)
 1124            {
 21125                var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode
 1126                try
 1127                {
 21128                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 21129                    CloudError _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC
 21130                    if (_errorBody != null)
 1131                    {
 21132                        ex = new CloudException(_errorBody.Message);
 21133                        ex.Body = _errorBody;
 1134                    }
 21135                }
 01136                catch (JsonException)
 1137                {
 1138                    // Ignore the exception
 01139                }
 21140                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 21141                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 21142                if (_httpResponse.Headers.Contains("request-id"))
 1143                {
 21144                    ex.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault();
 1145                }
 21146                if (_shouldTrace)
 1147                {
 01148                    ServiceClientTracing.Error(_invocationId, ex);
 1149                }
 21150                _httpRequest.Dispose();
 21151                if (_httpResponse != null)
 1152                {
 21153                    _httpResponse.Dispose();
 1154                }
 21155                throw ex;
 1156            }
 1157            // Create Result
 1181158            var _result = new AzureOperationResponse<DataSource>();
 1181159            _result.Request = _httpRequest;
 1181160            _result.Response = _httpResponse;
 1181161            if (_httpResponse.Headers.Contains("request-id"))
 1162            {
 1181163                _result.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault();
 1164            }
 1165            // Deserialize Response
 1181166            if ((int)_statusCode == 201)
 1167            {
 1181168                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 1169                try
 1170                {
 1181171                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<DataSource>(_responseContent, Cl
 1181172                }
 01173                catch (JsonException ex)
 1174                {
 01175                    _httpRequest.Dispose();
 01176                    if (_httpResponse != null)
 1177                    {
 01178                        _httpResponse.Dispose();
 1179                    }
 01180                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 1181                }
 1182            }
 1181183            if (_shouldTrace)
 1184            {
 01185                ServiceClientTracing.Exit(_invocationId, _result);
 1186            }
 1181187            return _result;
 1181188        }
 1189
 1190    }
 1191}