| | | 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.CognitiveServices.Knowledge.QnAMaker |
| | | 12 | | { |
| | | 13 | | using Microsoft.Rest; |
| | | 14 | | using Models; |
| | | 15 | | using Newtonsoft.Json; |
| | | 16 | | using System.Collections; |
| | | 17 | | using System.Collections.Generic; |
| | | 18 | | using System.IO; |
| | | 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 | | /// Alterations operations. |
| | | 27 | | /// </summary> |
| | | 28 | | public partial class Alterations : IServiceOperations<QnAMakerClient>, IAlterations |
| | | 29 | | { |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the Alterations 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> |
| | 0 | 39 | | public Alterations(QnAMakerClient client) |
| | | 40 | | { |
| | 0 | 41 | | if (client == null) |
| | | 42 | | { |
| | 0 | 43 | | throw new System.ArgumentNullException("client"); |
| | | 44 | | } |
| | 0 | 45 | | Client = client; |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets a reference to the QnAMakerClient |
| | | 50 | | /// </summary> |
| | 0 | 51 | | public QnAMakerClient Client { get; private set; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Download alterations from runtime. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name='customHeaders'> |
| | | 57 | | /// Headers that will be added to request. |
| | | 58 | | /// </param> |
| | | 59 | | /// <param name='cancellationToken'> |
| | | 60 | | /// The cancellation token. |
| | | 61 | | /// </param> |
| | | 62 | | /// <exception cref="ErrorResponseException"> |
| | | 63 | | /// Thrown when the operation returned an invalid status code |
| | | 64 | | /// </exception> |
| | | 65 | | /// <exception cref="SerializationException"> |
| | | 66 | | /// Thrown when unable to deserialize the response |
| | | 67 | | /// </exception> |
| | | 68 | | /// <exception cref="ValidationException"> |
| | | 69 | | /// Thrown when a required parameter is null |
| | | 70 | | /// </exception> |
| | | 71 | | /// <exception cref="System.ArgumentNullException"> |
| | | 72 | | /// Thrown when a required parameter is null |
| | | 73 | | /// </exception> |
| | | 74 | | /// <return> |
| | | 75 | | /// A response object containing the response body and response headers. |
| | | 76 | | /// </return> |
| | | 77 | | public async Task<HttpOperationResponse<WordAlterationsDTO>> GetWithHttpMessagesAsync(Dictionary<string, List<st |
| | | 78 | | { |
| | 0 | 79 | | if (Client.Endpoint == null) |
| | | 80 | | { |
| | 0 | 81 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | | 82 | | } |
| | | 83 | | // Tracing |
| | 0 | 84 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 0 | 85 | | string _invocationId = null; |
| | 0 | 86 | | if (_shouldTrace) |
| | | 87 | | { |
| | 0 | 88 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 89 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 90 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 91 | | ServiceClientTracing.Enter(_invocationId, this, "Get", tracingParameters); |
| | | 92 | | } |
| | | 93 | | // Construct URL |
| | 0 | 94 | | var _baseUrl = Client.BaseUri; |
| | 0 | 95 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "alterations"; |
| | 0 | 96 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | | 97 | | // Create HTTP transport objects |
| | 0 | 98 | | var _httpRequest = new HttpRequestMessage(); |
| | 0 | 99 | | HttpResponseMessage _httpResponse = null; |
| | 0 | 100 | | _httpRequest.Method = new HttpMethod("GET"); |
| | 0 | 101 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 102 | | // Set Headers |
| | | 103 | | |
| | | 104 | | |
| | 0 | 105 | | if (customHeaders != null) |
| | | 106 | | { |
| | 0 | 107 | | foreach(var _header in customHeaders) |
| | | 108 | | { |
| | 0 | 109 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 110 | | { |
| | 0 | 111 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 112 | | } |
| | 0 | 113 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | // Serialize Request |
| | 0 | 118 | | string _requestContent = null; |
| | | 119 | | // Set Credentials |
| | 0 | 120 | | if (Client.Credentials != null) |
| | | 121 | | { |
| | 0 | 122 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 123 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 124 | | } |
| | | 125 | | // Send Request |
| | 0 | 126 | | if (_shouldTrace) |
| | | 127 | | { |
| | 0 | 128 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 129 | | } |
| | 0 | 130 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 131 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 0 | 132 | | if (_shouldTrace) |
| | | 133 | | { |
| | 0 | 134 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 135 | | } |
| | 0 | 136 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 0 | 137 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 138 | | string _responseContent = null; |
| | 0 | 139 | | if ((int)_statusCode != 200) |
| | | 140 | | { |
| | 0 | 141 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | | 142 | | try |
| | | 143 | | { |
| | 0 | 144 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 145 | | ErrorResponse _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res |
| | 0 | 146 | | if (_errorBody != null) |
| | | 147 | | { |
| | 0 | 148 | | ex.Body = _errorBody; |
| | | 149 | | } |
| | 0 | 150 | | } |
| | 0 | 151 | | catch (JsonException) |
| | | 152 | | { |
| | | 153 | | // Ignore the exception |
| | 0 | 154 | | } |
| | 0 | 155 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 156 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 157 | | if (_shouldTrace) |
| | | 158 | | { |
| | 0 | 159 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 160 | | } |
| | 0 | 161 | | _httpRequest.Dispose(); |
| | 0 | 162 | | if (_httpResponse != null) |
| | | 163 | | { |
| | 0 | 164 | | _httpResponse.Dispose(); |
| | | 165 | | } |
| | 0 | 166 | | throw ex; |
| | | 167 | | } |
| | | 168 | | // Create Result |
| | 0 | 169 | | var _result = new HttpOperationResponse<WordAlterationsDTO>(); |
| | 0 | 170 | | _result.Request = _httpRequest; |
| | 0 | 171 | | _result.Response = _httpResponse; |
| | | 172 | | // Deserialize Response |
| | 0 | 173 | | if ((int)_statusCode == 200) |
| | | 174 | | { |
| | 0 | 175 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 176 | | try |
| | | 177 | | { |
| | 0 | 178 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<WordAlterationsDTO>(_responseCon |
| | 0 | 179 | | } |
| | 0 | 180 | | catch (JsonException ex) |
| | | 181 | | { |
| | 0 | 182 | | _httpRequest.Dispose(); |
| | 0 | 183 | | if (_httpResponse != null) |
| | | 184 | | { |
| | 0 | 185 | | _httpResponse.Dispose(); |
| | | 186 | | } |
| | 0 | 187 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | | 188 | | } |
| | | 189 | | } |
| | 0 | 190 | | if (_shouldTrace) |
| | | 191 | | { |
| | 0 | 192 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 193 | | } |
| | 0 | 194 | | return _result; |
| | 0 | 195 | | } |
| | | 196 | | |
| | | 197 | | /// <summary> |
| | | 198 | | /// Replace alterations data. |
| | | 199 | | /// </summary> |
| | | 200 | | /// <param name='wordAlterations'> |
| | | 201 | | /// New alterations data. |
| | | 202 | | /// </param> |
| | | 203 | | /// <param name='customHeaders'> |
| | | 204 | | /// Headers that will be added to request. |
| | | 205 | | /// </param> |
| | | 206 | | /// <param name='cancellationToken'> |
| | | 207 | | /// The cancellation token. |
| | | 208 | | /// </param> |
| | | 209 | | /// <exception cref="ErrorResponseException"> |
| | | 210 | | /// Thrown when the operation returned an invalid status code |
| | | 211 | | /// </exception> |
| | | 212 | | /// <exception cref="ValidationException"> |
| | | 213 | | /// Thrown when a required parameter is null |
| | | 214 | | /// </exception> |
| | | 215 | | /// <exception cref="System.ArgumentNullException"> |
| | | 216 | | /// Thrown when a required parameter is null |
| | | 217 | | /// </exception> |
| | | 218 | | /// <return> |
| | | 219 | | /// A response object containing the response body and response headers. |
| | | 220 | | /// </return> |
| | | 221 | | public async Task<HttpOperationResponse> ReplaceWithHttpMessagesAsync(WordAlterationsDTO wordAlterations, Dictio |
| | | 222 | | { |
| | 0 | 223 | | if (Client.Endpoint == null) |
| | | 224 | | { |
| | 0 | 225 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | | 226 | | } |
| | 0 | 227 | | if (wordAlterations == null) |
| | | 228 | | { |
| | 0 | 229 | | throw new ValidationException(ValidationRules.CannotBeNull, "wordAlterations"); |
| | | 230 | | } |
| | 0 | 231 | | if (wordAlterations != null) |
| | | 232 | | { |
| | 0 | 233 | | wordAlterations.Validate(); |
| | | 234 | | } |
| | | 235 | | // Tracing |
| | 0 | 236 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 0 | 237 | | string _invocationId = null; |
| | 0 | 238 | | if (_shouldTrace) |
| | | 239 | | { |
| | 0 | 240 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 0 | 241 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 0 | 242 | | tracingParameters.Add("wordAlterations", wordAlterations); |
| | 0 | 243 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 0 | 244 | | ServiceClientTracing.Enter(_invocationId, this, "Replace", tracingParameters); |
| | | 245 | | } |
| | | 246 | | // Construct URL |
| | 0 | 247 | | var _baseUrl = Client.BaseUri; |
| | 0 | 248 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "alterations"; |
| | 0 | 249 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | | 250 | | // Create HTTP transport objects |
| | 0 | 251 | | var _httpRequest = new HttpRequestMessage(); |
| | 0 | 252 | | HttpResponseMessage _httpResponse = null; |
| | 0 | 253 | | _httpRequest.Method = new HttpMethod("PUT"); |
| | 0 | 254 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | | 255 | | // Set Headers |
| | | 256 | | |
| | | 257 | | |
| | 0 | 258 | | if (customHeaders != null) |
| | | 259 | | { |
| | 0 | 260 | | foreach(var _header in customHeaders) |
| | | 261 | | { |
| | 0 | 262 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | | 263 | | { |
| | 0 | 264 | | _httpRequest.Headers.Remove(_header.Key); |
| | | 265 | | } |
| | 0 | 266 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | | 267 | | } |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | // Serialize Request |
| | 0 | 271 | | string _requestContent = null; |
| | 0 | 272 | | if(wordAlterations != null) |
| | | 273 | | { |
| | 0 | 274 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(wordAlterations, Client.Serializati |
| | 0 | 275 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| | 0 | 276 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | | 277 | | } |
| | | 278 | | // Set Credentials |
| | 0 | 279 | | if (Client.Credentials != null) |
| | | 280 | | { |
| | 0 | 281 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 282 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | | 283 | | } |
| | | 284 | | // Send Request |
| | 0 | 285 | | if (_shouldTrace) |
| | | 286 | | { |
| | 0 | 287 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | | 288 | | } |
| | 0 | 289 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 290 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 0 | 291 | | if (_shouldTrace) |
| | | 292 | | { |
| | 0 | 293 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | | 294 | | } |
| | 0 | 295 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 0 | 296 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 297 | | string _responseContent = null; |
| | 0 | 298 | | if ((int)_statusCode != 204) |
| | | 299 | | { |
| | 0 | 300 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | | 301 | | try |
| | | 302 | | { |
| | 0 | 303 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 0 | 304 | | ErrorResponse _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res |
| | 0 | 305 | | if (_errorBody != null) |
| | | 306 | | { |
| | 0 | 307 | | ex.Body = _errorBody; |
| | | 308 | | } |
| | 0 | 309 | | } |
| | 0 | 310 | | catch (JsonException) |
| | | 311 | | { |
| | | 312 | | // Ignore the exception |
| | 0 | 313 | | } |
| | 0 | 314 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 0 | 315 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 0 | 316 | | if (_shouldTrace) |
| | | 317 | | { |
| | 0 | 318 | | ServiceClientTracing.Error(_invocationId, ex); |
| | | 319 | | } |
| | 0 | 320 | | _httpRequest.Dispose(); |
| | 0 | 321 | | if (_httpResponse != null) |
| | | 322 | | { |
| | 0 | 323 | | _httpResponse.Dispose(); |
| | | 324 | | } |
| | 0 | 325 | | throw ex; |
| | | 326 | | } |
| | | 327 | | // Create Result |
| | 0 | 328 | | var _result = new HttpOperationResponse(); |
| | 0 | 329 | | _result.Request = _httpRequest; |
| | 0 | 330 | | _result.Response = _httpResponse; |
| | 0 | 331 | | if (_shouldTrace) |
| | | 332 | | { |
| | 0 | 333 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | | 334 | | } |
| | 0 | 335 | | return _result; |
| | 0 | 336 | | } |
| | | 337 | | |
| | | 338 | | } |
| | | 339 | | } |