| | 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 | |
|
| | 11 | | namespace 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> |
| 824 | 39 | | internal DataSourcesOperations(SearchServiceClient client) |
| | 40 | | { |
| 824 | 41 | | if (client == null) |
| | 42 | | { |
| 0 | 43 | | throw new System.ArgumentNullException("client"); |
| | 44 | | } |
| 824 | 45 | | Client = client; |
| 824 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets a reference to the SearchServiceClient |
| | 50 | | /// </summary> |
| 5222 | 51 | | 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 | | { |
| 32 | 92 | | if (Client.SearchServiceName == null) |
| | 93 | | { |
| 0 | 94 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchServiceName"); |
| | 95 | | } |
| 32 | 96 | | if (Client.SearchDnsSuffix == null) |
| | 97 | | { |
| 0 | 98 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchDnsSuffix"); |
| | 99 | | } |
| 32 | 100 | | if (dataSourceName == null) |
| | 101 | | { |
| 0 | 102 | | throw new ValidationException(ValidationRules.CannotBeNull, "dataSourceName"); |
| | 103 | | } |
| 32 | 104 | | if (dataSource == null) |
| | 105 | | { |
| 0 | 106 | | throw new ValidationException(ValidationRules.CannotBeNull, "dataSource"); |
| | 107 | | } |
| 32 | 108 | | if (dataSource != null) |
| | 109 | | { |
| 32 | 110 | | dataSource.Validate(); |
| | 111 | | } |
| 32 | 112 | | if (Client.ApiVersion == null) |
| | 113 | | { |
| 0 | 114 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.ApiVersion"); |
| | 115 | | } |
| 32 | 116 | | string prefer = "return=representation"; |
| 32 | 117 | | System.Guid? clientRequestId = default(System.Guid?); |
| 32 | 118 | | if (searchRequestOptions != null) |
| | 119 | | { |
| 0 | 120 | | clientRequestId = searchRequestOptions.ClientRequestId; |
| | 121 | | } |
| 32 | 122 | | string ifMatch = default(string); |
| 32 | 123 | | if (accessCondition != null) |
| | 124 | | { |
| 22 | 125 | | ifMatch = accessCondition.IfMatch; |
| | 126 | | } |
| 32 | 127 | | string ifNoneMatch = default(string); |
| 32 | 128 | | if (accessCondition != null) |
| | 129 | | { |
| 22 | 130 | | ifNoneMatch = accessCondition.IfNoneMatch; |
| | 131 | | } |
| | 132 | | // Tracing |
| 32 | 133 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 32 | 134 | | string _invocationId = null; |
| 32 | 135 | | if (_shouldTrace) |
| | 136 | | { |
| 0 | 137 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 138 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 139 | | tracingParameters.Add("dataSourceName", dataSourceName); |
| 0 | 140 | | tracingParameters.Add("dataSource", dataSource); |
| 0 | 141 | | tracingParameters.Add("prefer", prefer); |
| 0 | 142 | | tracingParameters.Add("clientRequestId", clientRequestId); |
| 0 | 143 | | tracingParameters.Add("ifMatch", ifMatch); |
| 0 | 144 | | tracingParameters.Add("ifNoneMatch", ifNoneMatch); |
| 0 | 145 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 146 | | ServiceClientTracing.Enter(_invocationId, this, "CreateOrUpdate", tracingParameters); |
| | 147 | | } |
| | 148 | | // Construct URL |
| 32 | 149 | | var _baseUrl = Client.BaseUri; |
| 32 | 150 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "datasources('{dataSourceName}')"; |
| 32 | 151 | | _url = _url.Replace("{searchServiceName}", Client.SearchServiceName); |
| 32 | 152 | | _url = _url.Replace("{searchDnsSuffix}", Client.SearchDnsSuffix); |
| 32 | 153 | | _url = _url.Replace("{dataSourceName}", System.Uri.EscapeDataString(dataSourceName)); |
| 32 | 154 | | List<string> _queryParameters = new List<string>(); |
| 32 | 155 | | if (Client.ApiVersion != null) |
| | 156 | | { |
| 32 | 157 | | _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion))); |
| | 158 | | } |
| 32 | 159 | | if (_queryParameters.Count > 0) |
| | 160 | | { |
| 32 | 161 | | _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| | 162 | | } |
| | 163 | | // Create HTTP transport objects |
| 32 | 164 | | var _httpRequest = new HttpRequestMessage(); |
| 32 | 165 | | HttpResponseMessage _httpResponse = null; |
| 32 | 166 | | _httpRequest.Method = new HttpMethod("PUT"); |
| 32 | 167 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 168 | | // Set Headers |
| 32 | 169 | | if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value) |
| | 170 | | { |
| 32 | 171 | | _httpRequest.Headers.TryAddWithoutValidation("client-request-id", System.Guid.NewGuid().ToString()); |
| | 172 | | } |
| 32 | 173 | | if (prefer != null) |
| | 174 | | { |
| 32 | 175 | | if (_httpRequest.Headers.Contains("Prefer")) |
| | 176 | | { |
| 0 | 177 | | _httpRequest.Headers.Remove("Prefer"); |
| | 178 | | } |
| 32 | 179 | | _httpRequest.Headers.TryAddWithoutValidation("Prefer", prefer); |
| | 180 | | } |
| 32 | 181 | | if (Client.AcceptLanguage != null) |
| | 182 | | { |
| 32 | 183 | | if (_httpRequest.Headers.Contains("accept-language")) |
| | 184 | | { |
| 0 | 185 | | _httpRequest.Headers.Remove("accept-language"); |
| | 186 | | } |
| 32 | 187 | | _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage); |
| | 188 | | } |
| 32 | 189 | | if (clientRequestId != null) |
| | 190 | | { |
| 0 | 191 | | if (_httpRequest.Headers.Contains("client-request-id")) |
| | 192 | | { |
| 0 | 193 | | _httpRequest.Headers.Remove("client-request-id"); |
| | 194 | | } |
| 0 | 195 | | _httpRequest.Headers.TryAddWithoutValidation("client-request-id", Rest.Serialization.SafeJsonConvert.Ser |
| | 196 | | } |
| 32 | 197 | | if (ifMatch != null) |
| | 198 | | { |
| 8 | 199 | | if (_httpRequest.Headers.Contains("If-Match")) |
| | 200 | | { |
| 0 | 201 | | _httpRequest.Headers.Remove("If-Match"); |
| | 202 | | } |
| 8 | 203 | | _httpRequest.Headers.TryAddWithoutValidation("If-Match", ifMatch); |
| | 204 | | } |
| 32 | 205 | | if (ifNoneMatch != null) |
| | 206 | | { |
| 4 | 207 | | if (_httpRequest.Headers.Contains("If-None-Match")) |
| | 208 | | { |
| 0 | 209 | | _httpRequest.Headers.Remove("If-None-Match"); |
| | 210 | | } |
| 4 | 211 | | _httpRequest.Headers.TryAddWithoutValidation("If-None-Match", ifNoneMatch); |
| | 212 | | } |
| | 213 | |
|
| | 214 | |
|
| 32 | 215 | | if (customHeaders != null) |
| | 216 | | { |
| 0 | 217 | | foreach(var _header in customHeaders) |
| | 218 | | { |
| 0 | 219 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 220 | | { |
| 0 | 221 | | _httpRequest.Headers.Remove(_header.Key); |
| | 222 | | } |
| 0 | 223 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 224 | | } |
| | 225 | | } |
| | 226 | |
|
| | 227 | | // Serialize Request |
| 32 | 228 | | string _requestContent = null; |
| 32 | 229 | | if(dataSource != null) |
| | 230 | | { |
| 32 | 231 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(dataSource, Client.SerializationSet |
| 32 | 232 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 32 | 233 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 234 | | } |
| | 235 | | // Set Credentials |
| 32 | 236 | | if (Client.Credentials != null) |
| | 237 | | { |
| 32 | 238 | | cancellationToken.ThrowIfCancellationRequested(); |
| 32 | 239 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 240 | | } |
| | 241 | | // Send Request |
| 32 | 242 | | if (_shouldTrace) |
| | 243 | | { |
| 0 | 244 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 245 | | } |
| 32 | 246 | | cancellationToken.ThrowIfCancellationRequested(); |
| 32 | 247 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 32 | 248 | | if (_shouldTrace) |
| | 249 | | { |
| 0 | 250 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 251 | | } |
| 32 | 252 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 32 | 253 | | cancellationToken.ThrowIfCancellationRequested(); |
| 32 | 254 | | string _responseContent = null; |
| 32 | 255 | | if ((int)_statusCode != 200 && (int)_statusCode != 201) |
| | 256 | | { |
| 6 | 257 | | var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode |
| | 258 | | try |
| | 259 | | { |
| 6 | 260 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 6 | 261 | | CloudError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC |
| 6 | 262 | | if (_errorBody != null) |
| | 263 | | { |
| 6 | 264 | | ex = new CloudException(_errorBody.Message); |
| 6 | 265 | | ex.Body = _errorBody; |
| | 266 | | } |
| 6 | 267 | | } |
| 0 | 268 | | catch (JsonException) |
| | 269 | | { |
| | 270 | | // Ignore the exception |
| 0 | 271 | | } |
| 6 | 272 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 6 | 273 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 6 | 274 | | if (_httpResponse.Headers.Contains("request-id")) |
| | 275 | | { |
| 6 | 276 | | ex.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault(); |
| | 277 | | } |
| 6 | 278 | | if (_shouldTrace) |
| | 279 | | { |
| 0 | 280 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 281 | | } |
| 6 | 282 | | _httpRequest.Dispose(); |
| 6 | 283 | | if (_httpResponse != null) |
| | 284 | | { |
| 6 | 285 | | _httpResponse.Dispose(); |
| | 286 | | } |
| 6 | 287 | | throw ex; |
| | 288 | | } |
| | 289 | | // Create Result |
| 26 | 290 | | var _result = new AzureOperationResponse<DataSource>(); |
| 26 | 291 | | _result.Request = _httpRequest; |
| 26 | 292 | | _result.Response = _httpResponse; |
| 26 | 293 | | if (_httpResponse.Headers.Contains("request-id")) |
| | 294 | | { |
| 26 | 295 | | _result.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault(); |
| | 296 | | } |
| | 297 | | // Deserialize Response |
| 26 | 298 | | if ((int)_statusCode == 200) |
| | 299 | | { |
| 10 | 300 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 301 | | try |
| | 302 | | { |
| 10 | 303 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<DataSource>(_responseContent, Cl |
| 10 | 304 | | } |
| 0 | 305 | | catch (JsonException ex) |
| | 306 | | { |
| 0 | 307 | | _httpRequest.Dispose(); |
| 0 | 308 | | if (_httpResponse != null) |
| | 309 | | { |
| 0 | 310 | | _httpResponse.Dispose(); |
| | 311 | | } |
| 0 | 312 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 313 | | } |
| | 314 | | } |
| | 315 | | // Deserialize Response |
| 26 | 316 | | if ((int)_statusCode == 201) |
| | 317 | | { |
| 16 | 318 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 319 | | try |
| | 320 | | { |
| 16 | 321 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<DataSource>(_responseContent, Cl |
| 16 | 322 | | } |
| 0 | 323 | | catch (JsonException ex) |
| | 324 | | { |
| 0 | 325 | | _httpRequest.Dispose(); |
| 0 | 326 | | if (_httpResponse != null) |
| | 327 | | { |
| 0 | 328 | | _httpResponse.Dispose(); |
| | 329 | | } |
| 0 | 330 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 331 | | } |
| | 332 | | } |
| 26 | 333 | | if (_shouldTrace) |
| | 334 | | { |
| 0 | 335 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 336 | | } |
| 26 | 337 | | return _result; |
| 26 | 338 | | } |
| | 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 | | { |
| 78 | 373 | | if (Client.SearchServiceName == null) |
| | 374 | | { |
| 0 | 375 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchServiceName"); |
| | 376 | | } |
| 78 | 377 | | if (Client.SearchDnsSuffix == null) |
| | 378 | | { |
| 0 | 379 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchDnsSuffix"); |
| | 380 | | } |
| 78 | 381 | | if (dataSourceName == null) |
| | 382 | | { |
| 0 | 383 | | throw new ValidationException(ValidationRules.CannotBeNull, "dataSourceName"); |
| | 384 | | } |
| 78 | 385 | | if (Client.ApiVersion == null) |
| | 386 | | { |
| 0 | 387 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.ApiVersion"); |
| | 388 | | } |
| 78 | 389 | | System.Guid? clientRequestId = default(System.Guid?); |
| 78 | 390 | | if (searchRequestOptions != null) |
| | 391 | | { |
| 0 | 392 | | clientRequestId = searchRequestOptions.ClientRequestId; |
| | 393 | | } |
| 78 | 394 | | string ifMatch = default(string); |
| 78 | 395 | | if (accessCondition != null) |
| | 396 | | { |
| 8 | 397 | | ifMatch = accessCondition.IfMatch; |
| | 398 | | } |
| 78 | 399 | | string ifNoneMatch = default(string); |
| 78 | 400 | | if (accessCondition != null) |
| | 401 | | { |
| 8 | 402 | | ifNoneMatch = accessCondition.IfNoneMatch; |
| | 403 | | } |
| | 404 | | // Tracing |
| 78 | 405 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 78 | 406 | | string _invocationId = null; |
| 78 | 407 | | if (_shouldTrace) |
| | 408 | | { |
| 0 | 409 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 410 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 411 | | tracingParameters.Add("dataSourceName", dataSourceName); |
| 0 | 412 | | tracingParameters.Add("clientRequestId", clientRequestId); |
| 0 | 413 | | tracingParameters.Add("ifMatch", ifMatch); |
| 0 | 414 | | tracingParameters.Add("ifNoneMatch", ifNoneMatch); |
| 0 | 415 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 416 | | ServiceClientTracing.Enter(_invocationId, this, "Delete", tracingParameters); |
| | 417 | | } |
| | 418 | | // Construct URL |
| 78 | 419 | | var _baseUrl = Client.BaseUri; |
| 78 | 420 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "datasources('{dataSourceName}')"; |
| 78 | 421 | | _url = _url.Replace("{searchServiceName}", Client.SearchServiceName); |
| 78 | 422 | | _url = _url.Replace("{searchDnsSuffix}", Client.SearchDnsSuffix); |
| 78 | 423 | | _url = _url.Replace("{dataSourceName}", System.Uri.EscapeDataString(dataSourceName)); |
| 78 | 424 | | List<string> _queryParameters = new List<string>(); |
| 78 | 425 | | if (Client.ApiVersion != null) |
| | 426 | | { |
| 78 | 427 | | _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion))); |
| | 428 | | } |
| 78 | 429 | | if (_queryParameters.Count > 0) |
| | 430 | | { |
| 78 | 431 | | _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| | 432 | | } |
| | 433 | | // Create HTTP transport objects |
| 78 | 434 | | var _httpRequest = new HttpRequestMessage(); |
| 78 | 435 | | HttpResponseMessage _httpResponse = null; |
| 78 | 436 | | _httpRequest.Method = new HttpMethod("DELETE"); |
| 78 | 437 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 438 | | // Set Headers |
| 78 | 439 | | if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value) |
| | 440 | | { |
| 78 | 441 | | _httpRequest.Headers.TryAddWithoutValidation("client-request-id", System.Guid.NewGuid().ToString()); |
| | 442 | | } |
| 78 | 443 | | if (Client.AcceptLanguage != null) |
| | 444 | | { |
| 78 | 445 | | if (_httpRequest.Headers.Contains("accept-language")) |
| | 446 | | { |
| 0 | 447 | | _httpRequest.Headers.Remove("accept-language"); |
| | 448 | | } |
| 78 | 449 | | _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage); |
| | 450 | | } |
| 78 | 451 | | if (clientRequestId != null) |
| | 452 | | { |
| 0 | 453 | | if (_httpRequest.Headers.Contains("client-request-id")) |
| | 454 | | { |
| 0 | 455 | | _httpRequest.Headers.Remove("client-request-id"); |
| | 456 | | } |
| 0 | 457 | | _httpRequest.Headers.TryAddWithoutValidation("client-request-id", Rest.Serialization.SafeJsonConvert.Ser |
| | 458 | | } |
| 78 | 459 | | if (ifMatch != null) |
| | 460 | | { |
| 8 | 461 | | if (_httpRequest.Headers.Contains("If-Match")) |
| | 462 | | { |
| 0 | 463 | | _httpRequest.Headers.Remove("If-Match"); |
| | 464 | | } |
| 8 | 465 | | _httpRequest.Headers.TryAddWithoutValidation("If-Match", ifMatch); |
| | 466 | | } |
| 78 | 467 | | if (ifNoneMatch != null) |
| | 468 | | { |
| 0 | 469 | | if (_httpRequest.Headers.Contains("If-None-Match")) |
| | 470 | | { |
| 0 | 471 | | _httpRequest.Headers.Remove("If-None-Match"); |
| | 472 | | } |
| 0 | 473 | | _httpRequest.Headers.TryAddWithoutValidation("If-None-Match", ifNoneMatch); |
| | 474 | | } |
| | 475 | |
|
| | 476 | |
|
| 78 | 477 | | if (customHeaders != null) |
| | 478 | | { |
| 0 | 479 | | foreach(var _header in customHeaders) |
| | 480 | | { |
| 0 | 481 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 482 | | { |
| 0 | 483 | | _httpRequest.Headers.Remove(_header.Key); |
| | 484 | | } |
| 0 | 485 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 486 | | } |
| | 487 | | } |
| | 488 | |
|
| | 489 | | // Serialize Request |
| 78 | 490 | | string _requestContent = null; |
| | 491 | | // Set Credentials |
| 78 | 492 | | if (Client.Credentials != null) |
| | 493 | | { |
| 78 | 494 | | cancellationToken.ThrowIfCancellationRequested(); |
| 78 | 495 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 496 | | } |
| | 497 | | // Send Request |
| 78 | 498 | | if (_shouldTrace) |
| | 499 | | { |
| 0 | 500 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 501 | | } |
| 78 | 502 | | cancellationToken.ThrowIfCancellationRequested(); |
| 78 | 503 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 78 | 504 | | if (_shouldTrace) |
| | 505 | | { |
| 0 | 506 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 507 | | } |
| 78 | 508 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 78 | 509 | | cancellationToken.ThrowIfCancellationRequested(); |
| 78 | 510 | | string _responseContent = null; |
| 78 | 511 | | if ((int)_statusCode != 204 && (int)_statusCode != 404) |
| | 512 | | { |
| 4 | 513 | | var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode |
| | 514 | | try |
| | 515 | | { |
| 4 | 516 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 4 | 517 | | CloudError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC |
| 4 | 518 | | if (_errorBody != null) |
| | 519 | | { |
| 4 | 520 | | ex = new CloudException(_errorBody.Message); |
| 4 | 521 | | ex.Body = _errorBody; |
| | 522 | | } |
| 4 | 523 | | } |
| 0 | 524 | | catch (JsonException) |
| | 525 | | { |
| | 526 | | // Ignore the exception |
| 0 | 527 | | } |
| 4 | 528 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 4 | 529 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 4 | 530 | | if (_httpResponse.Headers.Contains("request-id")) |
| | 531 | | { |
| 4 | 532 | | ex.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault(); |
| | 533 | | } |
| 4 | 534 | | if (_shouldTrace) |
| | 535 | | { |
| 0 | 536 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 537 | | } |
| 4 | 538 | | _httpRequest.Dispose(); |
| 4 | 539 | | if (_httpResponse != null) |
| | 540 | | { |
| 4 | 541 | | _httpResponse.Dispose(); |
| | 542 | | } |
| 4 | 543 | | throw ex; |
| | 544 | | } |
| | 545 | | // Create Result |
| 74 | 546 | | var _result = new AzureOperationResponse(); |
| 74 | 547 | | _result.Request = _httpRequest; |
| 74 | 548 | | _result.Response = _httpResponse; |
| 74 | 549 | | if (_httpResponse.Headers.Contains("request-id")) |
| | 550 | | { |
| 74 | 551 | | _result.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault(); |
| | 552 | | } |
| 74 | 553 | | if (_shouldTrace) |
| | 554 | | { |
| 0 | 555 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 556 | | } |
| 74 | 557 | | return _result; |
| 74 | 558 | | } |
| | 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 | | { |
| 38 | 593 | | if (Client.SearchServiceName == null) |
| | 594 | | { |
| 0 | 595 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchServiceName"); |
| | 596 | | } |
| 38 | 597 | | if (Client.SearchDnsSuffix == null) |
| | 598 | | { |
| 0 | 599 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchDnsSuffix"); |
| | 600 | | } |
| 38 | 601 | | if (dataSourceName == null) |
| | 602 | | { |
| 0 | 603 | | throw new ValidationException(ValidationRules.CannotBeNull, "dataSourceName"); |
| | 604 | | } |
| 38 | 605 | | if (Client.ApiVersion == null) |
| | 606 | | { |
| 0 | 607 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.ApiVersion"); |
| | 608 | | } |
| 38 | 609 | | System.Guid? clientRequestId = default(System.Guid?); |
| 38 | 610 | | if (searchRequestOptions != null) |
| | 611 | | { |
| 0 | 612 | | clientRequestId = searchRequestOptions.ClientRequestId; |
| | 613 | | } |
| | 614 | | // Tracing |
| 38 | 615 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 38 | 616 | | string _invocationId = null; |
| 38 | 617 | | if (_shouldTrace) |
| | 618 | | { |
| 0 | 619 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 620 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 621 | | tracingParameters.Add("dataSourceName", dataSourceName); |
| 0 | 622 | | tracingParameters.Add("clientRequestId", clientRequestId); |
| 0 | 623 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 624 | | ServiceClientTracing.Enter(_invocationId, this, "Get", tracingParameters); |
| | 625 | | } |
| | 626 | | // Construct URL |
| 38 | 627 | | var _baseUrl = Client.BaseUri; |
| 38 | 628 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "datasources('{dataSourceName}')"; |
| 38 | 629 | | _url = _url.Replace("{searchServiceName}", Client.SearchServiceName); |
| 38 | 630 | | _url = _url.Replace("{searchDnsSuffix}", Client.SearchDnsSuffix); |
| 38 | 631 | | _url = _url.Replace("{dataSourceName}", System.Uri.EscapeDataString(dataSourceName)); |
| 38 | 632 | | List<string> _queryParameters = new List<string>(); |
| 38 | 633 | | if (Client.ApiVersion != null) |
| | 634 | | { |
| 38 | 635 | | _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion))); |
| | 636 | | } |
| 38 | 637 | | if (_queryParameters.Count > 0) |
| | 638 | | { |
| 38 | 639 | | _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| | 640 | | } |
| | 641 | | // Create HTTP transport objects |
| 38 | 642 | | var _httpRequest = new HttpRequestMessage(); |
| 38 | 643 | | HttpResponseMessage _httpResponse = null; |
| 38 | 644 | | _httpRequest.Method = new HttpMethod("GET"); |
| 38 | 645 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 646 | | // Set Headers |
| 38 | 647 | | if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value) |
| | 648 | | { |
| 38 | 649 | | _httpRequest.Headers.TryAddWithoutValidation("client-request-id", System.Guid.NewGuid().ToString()); |
| | 650 | | } |
| 38 | 651 | | if (Client.AcceptLanguage != null) |
| | 652 | | { |
| 38 | 653 | | if (_httpRequest.Headers.Contains("accept-language")) |
| | 654 | | { |
| 0 | 655 | | _httpRequest.Headers.Remove("accept-language"); |
| | 656 | | } |
| 38 | 657 | | _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage); |
| | 658 | | } |
| 38 | 659 | | if (clientRequestId != null) |
| | 660 | | { |
| 0 | 661 | | if (_httpRequest.Headers.Contains("client-request-id")) |
| | 662 | | { |
| 0 | 663 | | _httpRequest.Headers.Remove("client-request-id"); |
| | 664 | | } |
| 0 | 665 | | _httpRequest.Headers.TryAddWithoutValidation("client-request-id", Rest.Serialization.SafeJsonConvert.Ser |
| | 666 | | } |
| | 667 | |
|
| | 668 | |
|
| 38 | 669 | | if (customHeaders != null) |
| | 670 | | { |
| 0 | 671 | | foreach(var _header in customHeaders) |
| | 672 | | { |
| 0 | 673 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 674 | | { |
| 0 | 675 | | _httpRequest.Headers.Remove(_header.Key); |
| | 676 | | } |
| 0 | 677 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 678 | | } |
| | 679 | | } |
| | 680 | |
|
| | 681 | | // Serialize Request |
| 38 | 682 | | string _requestContent = null; |
| | 683 | | // Set Credentials |
| 38 | 684 | | if (Client.Credentials != null) |
| | 685 | | { |
| 38 | 686 | | cancellationToken.ThrowIfCancellationRequested(); |
| 38 | 687 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 688 | | } |
| | 689 | | // Send Request |
| 38 | 690 | | if (_shouldTrace) |
| | 691 | | { |
| 0 | 692 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 693 | | } |
| 38 | 694 | | cancellationToken.ThrowIfCancellationRequested(); |
| 38 | 695 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 38 | 696 | | if (_shouldTrace) |
| | 697 | | { |
| 0 | 698 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 699 | | } |
| 38 | 700 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 38 | 701 | | cancellationToken.ThrowIfCancellationRequested(); |
| 38 | 702 | | string _responseContent = null; |
| 38 | 703 | | if ((int)_statusCode != 200) |
| | 704 | | { |
| 4 | 705 | | var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode |
| | 706 | | try |
| | 707 | | { |
| 4 | 708 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 4 | 709 | | CloudError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC |
| 4 | 710 | | if (_errorBody != null) |
| | 711 | | { |
| 4 | 712 | | ex = new CloudException(_errorBody.Message); |
| 4 | 713 | | ex.Body = _errorBody; |
| | 714 | | } |
| 4 | 715 | | } |
| 0 | 716 | | catch (JsonException) |
| | 717 | | { |
| | 718 | | // Ignore the exception |
| 0 | 719 | | } |
| 4 | 720 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 4 | 721 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 4 | 722 | | if (_httpResponse.Headers.Contains("request-id")) |
| | 723 | | { |
| 4 | 724 | | ex.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault(); |
| | 725 | | } |
| 4 | 726 | | if (_shouldTrace) |
| | 727 | | { |
| 0 | 728 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 729 | | } |
| 4 | 730 | | _httpRequest.Dispose(); |
| 4 | 731 | | if (_httpResponse != null) |
| | 732 | | { |
| 4 | 733 | | _httpResponse.Dispose(); |
| | 734 | | } |
| 4 | 735 | | throw ex; |
| | 736 | | } |
| | 737 | | // Create Result |
| 34 | 738 | | var _result = new AzureOperationResponse<DataSource>(); |
| 34 | 739 | | _result.Request = _httpRequest; |
| 34 | 740 | | _result.Response = _httpResponse; |
| 34 | 741 | | if (_httpResponse.Headers.Contains("request-id")) |
| | 742 | | { |
| 34 | 743 | | _result.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault(); |
| | 744 | | } |
| | 745 | | // Deserialize Response |
| 34 | 746 | | if ((int)_statusCode == 200) |
| | 747 | | { |
| 34 | 748 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 749 | | try |
| | 750 | | { |
| 34 | 751 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<DataSource>(_responseContent, Cl |
| 34 | 752 | | } |
| 0 | 753 | | catch (JsonException ex) |
| | 754 | | { |
| 0 | 755 | | _httpRequest.Dispose(); |
| 0 | 756 | | if (_httpResponse != null) |
| | 757 | | { |
| 0 | 758 | | _httpResponse.Dispose(); |
| | 759 | | } |
| 0 | 760 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 761 | | } |
| | 762 | | } |
| 34 | 763 | | if (_shouldTrace) |
| | 764 | | { |
| 0 | 765 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 766 | | } |
| 34 | 767 | | return _result; |
| 34 | 768 | | } |
| | 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 | | { |
| 2 | 800 | | if (Client.SearchServiceName == null) |
| | 801 | | { |
| 0 | 802 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchServiceName"); |
| | 803 | | } |
| 2 | 804 | | if (Client.SearchDnsSuffix == null) |
| | 805 | | { |
| 0 | 806 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchDnsSuffix"); |
| | 807 | | } |
| 2 | 808 | | if (Client.ApiVersion == null) |
| | 809 | | { |
| 0 | 810 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.ApiVersion"); |
| | 811 | | } |
| 2 | 812 | | System.Guid? clientRequestId = default(System.Guid?); |
| 2 | 813 | | if (searchRequestOptions != null) |
| | 814 | | { |
| 0 | 815 | | clientRequestId = searchRequestOptions.ClientRequestId; |
| | 816 | | } |
| | 817 | | // Tracing |
| 2 | 818 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 819 | | string _invocationId = null; |
| 2 | 820 | | if (_shouldTrace) |
| | 821 | | { |
| 0 | 822 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 823 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 824 | | tracingParameters.Add("clientRequestId", clientRequestId); |
| 0 | 825 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 826 | | ServiceClientTracing.Enter(_invocationId, this, "List", tracingParameters); |
| | 827 | | } |
| | 828 | | // Construct URL |
| 2 | 829 | | var _baseUrl = Client.BaseUri; |
| 2 | 830 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "datasources"; |
| 2 | 831 | | _url = _url.Replace("{searchServiceName}", Client.SearchServiceName); |
| 2 | 832 | | _url = _url.Replace("{searchDnsSuffix}", Client.SearchDnsSuffix); |
| 2 | 833 | | List<string> _queryParameters = new List<string>(); |
| 2 | 834 | | if (Client.ApiVersion != null) |
| | 835 | | { |
| 2 | 836 | | _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion))); |
| | 837 | | } |
| 2 | 838 | | if (_queryParameters.Count > 0) |
| | 839 | | { |
| 2 | 840 | | _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| | 841 | | } |
| | 842 | | // Create HTTP transport objects |
| 2 | 843 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 844 | | HttpResponseMessage _httpResponse = null; |
| 2 | 845 | | _httpRequest.Method = new HttpMethod("GET"); |
| 2 | 846 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 847 | | // Set Headers |
| 2 | 848 | | if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value) |
| | 849 | | { |
| 2 | 850 | | _httpRequest.Headers.TryAddWithoutValidation("client-request-id", System.Guid.NewGuid().ToString()); |
| | 851 | | } |
| 2 | 852 | | if (Client.AcceptLanguage != null) |
| | 853 | | { |
| 2 | 854 | | if (_httpRequest.Headers.Contains("accept-language")) |
| | 855 | | { |
| 0 | 856 | | _httpRequest.Headers.Remove("accept-language"); |
| | 857 | | } |
| 2 | 858 | | _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage); |
| | 859 | | } |
| 2 | 860 | | if (clientRequestId != null) |
| | 861 | | { |
| 0 | 862 | | if (_httpRequest.Headers.Contains("client-request-id")) |
| | 863 | | { |
| 0 | 864 | | _httpRequest.Headers.Remove("client-request-id"); |
| | 865 | | } |
| 0 | 866 | | _httpRequest.Headers.TryAddWithoutValidation("client-request-id", Rest.Serialization.SafeJsonConvert.Ser |
| | 867 | | } |
| | 868 | |
|
| | 869 | |
|
| 2 | 870 | | if (customHeaders != null) |
| | 871 | | { |
| 0 | 872 | | foreach(var _header in customHeaders) |
| | 873 | | { |
| 0 | 874 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 875 | | { |
| 0 | 876 | | _httpRequest.Headers.Remove(_header.Key); |
| | 877 | | } |
| 0 | 878 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 879 | | } |
| | 880 | | } |
| | 881 | |
|
| | 882 | | // Serialize Request |
| 2 | 883 | | string _requestContent = null; |
| | 884 | | // Set Credentials |
| 2 | 885 | | if (Client.Credentials != null) |
| | 886 | | { |
| 2 | 887 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 888 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 889 | | } |
| | 890 | | // Send Request |
| 2 | 891 | | if (_shouldTrace) |
| | 892 | | { |
| 0 | 893 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 894 | | } |
| 2 | 895 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 896 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 897 | | if (_shouldTrace) |
| | 898 | | { |
| 0 | 899 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 900 | | } |
| 2 | 901 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 902 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 903 | | string _responseContent = null; |
| 2 | 904 | | if ((int)_statusCode != 200) |
| | 905 | | { |
| 0 | 906 | | var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode |
| | 907 | | try |
| | 908 | | { |
| 0 | 909 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 910 | | CloudError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC |
| 0 | 911 | | if (_errorBody != null) |
| | 912 | | { |
| 0 | 913 | | ex = new CloudException(_errorBody.Message); |
| 0 | 914 | | ex.Body = _errorBody; |
| | 915 | | } |
| 0 | 916 | | } |
| 0 | 917 | | catch (JsonException) |
| | 918 | | { |
| | 919 | | // Ignore the exception |
| 0 | 920 | | } |
| 0 | 921 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 922 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 923 | | if (_httpResponse.Headers.Contains("request-id")) |
| | 924 | | { |
| 0 | 925 | | ex.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault(); |
| | 926 | | } |
| 0 | 927 | | if (_shouldTrace) |
| | 928 | | { |
| 0 | 929 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 930 | | } |
| 0 | 931 | | _httpRequest.Dispose(); |
| 0 | 932 | | if (_httpResponse != null) |
| | 933 | | { |
| 0 | 934 | | _httpResponse.Dispose(); |
| | 935 | | } |
| 0 | 936 | | throw ex; |
| | 937 | | } |
| | 938 | | // Create Result |
| 2 | 939 | | var _result = new AzureOperationResponse<DataSourceListResult>(); |
| 2 | 940 | | _result.Request = _httpRequest; |
| 2 | 941 | | _result.Response = _httpResponse; |
| 2 | 942 | | if (_httpResponse.Headers.Contains("request-id")) |
| | 943 | | { |
| 2 | 944 | | _result.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault(); |
| | 945 | | } |
| | 946 | | // Deserialize Response |
| 2 | 947 | | if ((int)_statusCode == 200) |
| | 948 | | { |
| 2 | 949 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 950 | | try |
| | 951 | | { |
| 2 | 952 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<DataSourceListResult>(_responseC |
| 2 | 953 | | } |
| 0 | 954 | | catch (JsonException ex) |
| | 955 | | { |
| 0 | 956 | | _httpRequest.Dispose(); |
| 0 | 957 | | if (_httpResponse != null) |
| | 958 | | { |
| 0 | 959 | | _httpResponse.Dispose(); |
| | 960 | | } |
| 0 | 961 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 962 | | } |
| | 963 | | } |
| 2 | 964 | | if (_shouldTrace) |
| | 965 | | { |
| 0 | 966 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 967 | | } |
| 2 | 968 | | return _result; |
| 2 | 969 | | } |
| | 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 | | { |
| 120 | 1004 | | if (Client.SearchServiceName == null) |
| | 1005 | | { |
| 0 | 1006 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchServiceName"); |
| | 1007 | | } |
| 120 | 1008 | | if (Client.SearchDnsSuffix == null) |
| | 1009 | | { |
| 0 | 1010 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.SearchDnsSuffix"); |
| | 1011 | | } |
| 120 | 1012 | | if (dataSource == null) |
| | 1013 | | { |
| 0 | 1014 | | throw new ValidationException(ValidationRules.CannotBeNull, "dataSource"); |
| | 1015 | | } |
| 120 | 1016 | | if (dataSource != null) |
| | 1017 | | { |
| 120 | 1018 | | dataSource.Validate(); |
| | 1019 | | } |
| 120 | 1020 | | if (Client.ApiVersion == null) |
| | 1021 | | { |
| 0 | 1022 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.ApiVersion"); |
| | 1023 | | } |
| 120 | 1024 | | System.Guid? clientRequestId = default(System.Guid?); |
| 120 | 1025 | | if (searchRequestOptions != null) |
| | 1026 | | { |
| 0 | 1027 | | clientRequestId = searchRequestOptions.ClientRequestId; |
| | 1028 | | } |
| | 1029 | | // Tracing |
| 120 | 1030 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 120 | 1031 | | string _invocationId = null; |
| 120 | 1032 | | if (_shouldTrace) |
| | 1033 | | { |
| 0 | 1034 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 1035 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 1036 | | tracingParameters.Add("dataSource", dataSource); |
| 0 | 1037 | | tracingParameters.Add("clientRequestId", clientRequestId); |
| 0 | 1038 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 1039 | | ServiceClientTracing.Enter(_invocationId, this, "Create", tracingParameters); |
| | 1040 | | } |
| | 1041 | | // Construct URL |
| 120 | 1042 | | var _baseUrl = Client.BaseUri; |
| 120 | 1043 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "datasources"; |
| 120 | 1044 | | _url = _url.Replace("{searchServiceName}", Client.SearchServiceName); |
| 120 | 1045 | | _url = _url.Replace("{searchDnsSuffix}", Client.SearchDnsSuffix); |
| 120 | 1046 | | List<string> _queryParameters = new List<string>(); |
| 120 | 1047 | | if (Client.ApiVersion != null) |
| | 1048 | | { |
| 120 | 1049 | | _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion))); |
| | 1050 | | } |
| 120 | 1051 | | if (_queryParameters.Count > 0) |
| | 1052 | | { |
| 120 | 1053 | | _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| | 1054 | | } |
| | 1055 | | // Create HTTP transport objects |
| 120 | 1056 | | var _httpRequest = new HttpRequestMessage(); |
| 120 | 1057 | | HttpResponseMessage _httpResponse = null; |
| 120 | 1058 | | _httpRequest.Method = new HttpMethod("POST"); |
| 120 | 1059 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 1060 | | // Set Headers |
| 120 | 1061 | | if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value) |
| | 1062 | | { |
| 120 | 1063 | | _httpRequest.Headers.TryAddWithoutValidation("client-request-id", System.Guid.NewGuid().ToString()); |
| | 1064 | | } |
| 120 | 1065 | | if (Client.AcceptLanguage != null) |
| | 1066 | | { |
| 120 | 1067 | | if (_httpRequest.Headers.Contains("accept-language")) |
| | 1068 | | { |
| 0 | 1069 | | _httpRequest.Headers.Remove("accept-language"); |
| | 1070 | | } |
| 120 | 1071 | | _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage); |
| | 1072 | | } |
| 120 | 1073 | | if (clientRequestId != null) |
| | 1074 | | { |
| 0 | 1075 | | if (_httpRequest.Headers.Contains("client-request-id")) |
| | 1076 | | { |
| 0 | 1077 | | _httpRequest.Headers.Remove("client-request-id"); |
| | 1078 | | } |
| 0 | 1079 | | _httpRequest.Headers.TryAddWithoutValidation("client-request-id", Rest.Serialization.SafeJsonConvert.Ser |
| | 1080 | | } |
| | 1081 | |
|
| | 1082 | |
|
| 120 | 1083 | | if (customHeaders != null) |
| | 1084 | | { |
| 0 | 1085 | | foreach(var _header in customHeaders) |
| | 1086 | | { |
| 0 | 1087 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1088 | | { |
| 0 | 1089 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1090 | | } |
| 0 | 1091 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1092 | | } |
| | 1093 | | } |
| | 1094 | |
|
| | 1095 | | // Serialize Request |
| 120 | 1096 | | string _requestContent = null; |
| 120 | 1097 | | if(dataSource != null) |
| | 1098 | | { |
| 120 | 1099 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(dataSource, Client.SerializationSet |
| 120 | 1100 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 120 | 1101 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 1102 | | } |
| | 1103 | | // Set Credentials |
| 120 | 1104 | | if (Client.Credentials != null) |
| | 1105 | | { |
| 120 | 1106 | | cancellationToken.ThrowIfCancellationRequested(); |
| 120 | 1107 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1108 | | } |
| | 1109 | | // Send Request |
| 120 | 1110 | | if (_shouldTrace) |
| | 1111 | | { |
| 0 | 1112 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1113 | | } |
| 120 | 1114 | | cancellationToken.ThrowIfCancellationRequested(); |
| 120 | 1115 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 120 | 1116 | | if (_shouldTrace) |
| | 1117 | | { |
| 0 | 1118 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1119 | | } |
| 120 | 1120 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 120 | 1121 | | cancellationToken.ThrowIfCancellationRequested(); |
| 120 | 1122 | | string _responseContent = null; |
| 120 | 1123 | | if ((int)_statusCode != 201) |
| | 1124 | | { |
| 2 | 1125 | | var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode |
| | 1126 | | try |
| | 1127 | | { |
| 2 | 1128 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 2 | 1129 | | CloudError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC |
| 2 | 1130 | | if (_errorBody != null) |
| | 1131 | | { |
| 2 | 1132 | | ex = new CloudException(_errorBody.Message); |
| 2 | 1133 | | ex.Body = _errorBody; |
| | 1134 | | } |
| 2 | 1135 | | } |
| 0 | 1136 | | catch (JsonException) |
| | 1137 | | { |
| | 1138 | | // Ignore the exception |
| 0 | 1139 | | } |
| 2 | 1140 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 2 | 1141 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 2 | 1142 | | if (_httpResponse.Headers.Contains("request-id")) |
| | 1143 | | { |
| 2 | 1144 | | ex.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault(); |
| | 1145 | | } |
| 2 | 1146 | | if (_shouldTrace) |
| | 1147 | | { |
| 0 | 1148 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1149 | | } |
| 2 | 1150 | | _httpRequest.Dispose(); |
| 2 | 1151 | | if (_httpResponse != null) |
| | 1152 | | { |
| 2 | 1153 | | _httpResponse.Dispose(); |
| | 1154 | | } |
| 2 | 1155 | | throw ex; |
| | 1156 | | } |
| | 1157 | | // Create Result |
| 118 | 1158 | | var _result = new AzureOperationResponse<DataSource>(); |
| 118 | 1159 | | _result.Request = _httpRequest; |
| 118 | 1160 | | _result.Response = _httpResponse; |
| 118 | 1161 | | if (_httpResponse.Headers.Contains("request-id")) |
| | 1162 | | { |
| 118 | 1163 | | _result.RequestId = _httpResponse.Headers.GetValues("request-id").FirstOrDefault(); |
| | 1164 | | } |
| | 1165 | | // Deserialize Response |
| 118 | 1166 | | if ((int)_statusCode == 201) |
| | 1167 | | { |
| 118 | 1168 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 1169 | | try |
| | 1170 | | { |
| 118 | 1171 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<DataSource>(_responseContent, Cl |
| 118 | 1172 | | } |
| 0 | 1173 | | catch (JsonException ex) |
| | 1174 | | { |
| 0 | 1175 | | _httpRequest.Dispose(); |
| 0 | 1176 | | if (_httpResponse != null) |
| | 1177 | | { |
| 0 | 1178 | | _httpResponse.Dispose(); |
| | 1179 | | } |
| 0 | 1180 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 1181 | | } |
| | 1182 | | } |
| 118 | 1183 | | if (_shouldTrace) |
| | 1184 | | { |
| 0 | 1185 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1186 | | } |
| 118 | 1187 | | return _result; |
| 118 | 1188 | | } |
| | 1189 | |
|
| | 1190 | | } |
| | 1191 | | } |