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