< Summary

Class:Microsoft.Azure.CognitiveServices.Language.LUIS.Authoring.Train
Assembly:Microsoft.Azure.CognitiveServices.Language.LUIS.Authoring
File(s):C:\Git\azure-sdk-for-net\sdk\cognitiveservices\Language.LUIS.Authoring\src\Generated\Train.cs
Covered lines:79
Uncovered lines:73
Coverable lines:152
Total lines:380
Line coverage:51.9% (79 of 152)
Covered branches:27
Total branches:70
Branch coverage:38.5% (27 of 70)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-80%50%
get_Client()-100%100%
TrainVersionWithHttpMessagesAsync()-50.68%38.24%
GetStatusWithHttpMessagesAsync()-50.68%38.24%

File(s)

C:\Git\azure-sdk-for-net\sdk\cognitiveservices\Language.LUIS.Authoring\src\Generated\Train.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.CognitiveServices.Language.LUIS.Authoring
 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    /// Train operations.
 27    /// </summary>
 28    public partial class Train : IServiceOperations<LUISAuthoringClient>, ITrain
 29    {
 30        /// <summary>
 31        /// Initializes a new instance of the Train 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>
 15639        public Train(LUISAuthoringClient client)
 40        {
 15641            if (client == null)
 42            {
 043                throw new System.ArgumentNullException("client");
 44            }
 15645            Client = client;
 15646        }
 47
 48        /// <summary>
 49        /// Gets a reference to the LUISAuthoringClient
 50        /// </summary>
 23651        public LUISAuthoringClient Client { get; private set; }
 52
 53        /// <summary>
 54        /// Sends a training request for a version of a specified LUIS app. This POST
 55        /// request initiates a request asynchronously. To determine whether the
 56        /// training request is successful, submit a GET request to get training
 57        /// status. Note: The application version is not fully trained unless all the
 58        /// models (intents and entities) are trained successfully or are up to date.
 59        /// To verify training success, get the training status at least once after
 60        /// training is complete.
 61        /// </summary>
 62        /// <param name='appId'>
 63        /// The application ID.
 64        /// </param>
 65        /// <param name='versionId'>
 66        /// The version ID.
 67        /// </param>
 68        /// <param name='customHeaders'>
 69        /// Headers that will be added to request.
 70        /// </param>
 71        /// <param name='cancellationToken'>
 72        /// The cancellation token.
 73        /// </param>
 74        /// <exception cref="ErrorResponseException">
 75        /// Thrown when the operation returned an invalid status code
 76        /// </exception>
 77        /// <exception cref="SerializationException">
 78        /// Thrown when unable to deserialize the response
 79        /// </exception>
 80        /// <exception cref="ValidationException">
 81        /// Thrown when a required parameter is null
 82        /// </exception>
 83        /// <exception cref="System.ArgumentNullException">
 84        /// Thrown when a required parameter is null
 85        /// </exception>
 86        /// <return>
 87        /// A response object containing the response body and response headers.
 88        /// </return>
 89        public async Task<HttpOperationResponse<EnqueueTrainingResponse>> TrainVersionWithHttpMessagesAsync(System.Guid 
 90        {
 391            if (Client.Endpoint == null)
 92            {
 093                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint");
 94            }
 395            if (versionId == null)
 96            {
 097                throw new ValidationException(ValidationRules.CannotBeNull, "versionId");
 98            }
 99            // Tracing
 3100            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 3101            string _invocationId = null;
 3102            if (_shouldTrace)
 103            {
 0104                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0105                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0106                tracingParameters.Add("appId", appId);
 0107                tracingParameters.Add("versionId", versionId);
 0108                tracingParameters.Add("cancellationToken", cancellationToken);
 0109                ServiceClientTracing.Enter(_invocationId, this, "TrainVersion", tracingParameters);
 110            }
 111            // Construct URL
 3112            var _baseUrl = Client.BaseUri;
 3113            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "apps/{appId}/versions/{versionId}/train";
 3114            _url = _url.Replace("{Endpoint}", Client.Endpoint);
 3115            _url = _url.Replace("{appId}", System.Uri.EscapeDataString(Rest.Serialization.SafeJsonConvert.SerializeObjec
 3116            _url = _url.Replace("{versionId}", System.Uri.EscapeDataString(versionId));
 117            // Create HTTP transport objects
 3118            var _httpRequest = new HttpRequestMessage();
 3119            HttpResponseMessage _httpResponse = null;
 3120            _httpRequest.Method = new HttpMethod("POST");
 3121            _httpRequest.RequestUri = new System.Uri(_url);
 122            // Set Headers
 123
 124
 3125            if (customHeaders != null)
 126            {
 0127                foreach(var _header in customHeaders)
 128                {
 0129                    if (_httpRequest.Headers.Contains(_header.Key))
 130                    {
 0131                        _httpRequest.Headers.Remove(_header.Key);
 132                    }
 0133                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 134                }
 135            }
 136
 137            // Serialize Request
 3138            string _requestContent = null;
 139            // Set Credentials
 3140            if (Client.Credentials != null)
 141            {
 3142                cancellationToken.ThrowIfCancellationRequested();
 3143                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 144            }
 145            // Send Request
 3146            if (_shouldTrace)
 147            {
 0148                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 149            }
 3150            cancellationToken.ThrowIfCancellationRequested();
 3151            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 3152            if (_shouldTrace)
 153            {
 0154                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 155            }
 3156            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 3157            cancellationToken.ThrowIfCancellationRequested();
 3158            string _responseContent = null;
 3159            if ((int)_statusCode != 202)
 160            {
 0161                var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st
 162                try
 163                {
 0164                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 0165                    ErrorResponse _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res
 0166                    if (_errorBody != null)
 167                    {
 0168                        ex.Body = _errorBody;
 169                    }
 0170                }
 0171                catch (JsonException)
 172                {
 173                    // Ignore the exception
 0174                }
 0175                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 0176                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 0177                if (_shouldTrace)
 178                {
 0179                    ServiceClientTracing.Error(_invocationId, ex);
 180                }
 0181                _httpRequest.Dispose();
 0182                if (_httpResponse != null)
 183                {
 0184                    _httpResponse.Dispose();
 185                }
 0186                throw ex;
 187            }
 188            // Create Result
 3189            var _result = new HttpOperationResponse<EnqueueTrainingResponse>();
 3190            _result.Request = _httpRequest;
 3191            _result.Response = _httpResponse;
 192            // Deserialize Response
 3193            if ((int)_statusCode == 202)
 194            {
 3195                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 196                try
 197                {
 3198                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<EnqueueTrainingResponse>(_respon
 3199                }
 0200                catch (JsonException ex)
 201                {
 0202                    _httpRequest.Dispose();
 0203                    if (_httpResponse != null)
 204                    {
 0205                        _httpResponse.Dispose();
 206                    }
 0207                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 208                }
 209            }
 3210            if (_shouldTrace)
 211            {
 0212                ServiceClientTracing.Exit(_invocationId, _result);
 213            }
 3214            return _result;
 3215        }
 216
 217        /// <summary>
 218        /// Gets the training status of all models (intents and entities) for the
 219        /// specified LUIS app. You must call the train API to train the LUIS app
 220        /// before you call this API to get training status. "appID" specifies the LUIS
 221        /// app ID. "versionId" specifies the version number of the LUIS app. For
 222        /// example, "0.1".
 223        /// </summary>
 224        /// <param name='appId'>
 225        /// The application ID.
 226        /// </param>
 227        /// <param name='versionId'>
 228        /// The version ID.
 229        /// </param>
 230        /// <param name='customHeaders'>
 231        /// Headers that will be added to request.
 232        /// </param>
 233        /// <param name='cancellationToken'>
 234        /// The cancellation token.
 235        /// </param>
 236        /// <exception cref="ErrorResponseException">
 237        /// Thrown when the operation returned an invalid status code
 238        /// </exception>
 239        /// <exception cref="SerializationException">
 240        /// Thrown when unable to deserialize the response
 241        /// </exception>
 242        /// <exception cref="ValidationException">
 243        /// Thrown when a required parameter is null
 244        /// </exception>
 245        /// <exception cref="System.ArgumentNullException">
 246        /// Thrown when a required parameter is null
 247        /// </exception>
 248        /// <return>
 249        /// A response object containing the response body and response headers.
 250        /// </return>
 251        public async Task<HttpOperationResponse<IList<ModelTrainingInfo>>> GetStatusWithHttpMessagesAsync(System.Guid ap
 252        {
 7253            if (Client.Endpoint == null)
 254            {
 0255                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint");
 256            }
 7257            if (versionId == null)
 258            {
 0259                throw new ValidationException(ValidationRules.CannotBeNull, "versionId");
 260            }
 261            // Tracing
 7262            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 7263            string _invocationId = null;
 7264            if (_shouldTrace)
 265            {
 0266                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0267                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0268                tracingParameters.Add("appId", appId);
 0269                tracingParameters.Add("versionId", versionId);
 0270                tracingParameters.Add("cancellationToken", cancellationToken);
 0271                ServiceClientTracing.Enter(_invocationId, this, "GetStatus", tracingParameters);
 272            }
 273            // Construct URL
 7274            var _baseUrl = Client.BaseUri;
 7275            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "apps/{appId}/versions/{versionId}/train";
 7276            _url = _url.Replace("{Endpoint}", Client.Endpoint);
 7277            _url = _url.Replace("{appId}", System.Uri.EscapeDataString(Rest.Serialization.SafeJsonConvert.SerializeObjec
 7278            _url = _url.Replace("{versionId}", System.Uri.EscapeDataString(versionId));
 279            // Create HTTP transport objects
 7280            var _httpRequest = new HttpRequestMessage();
 7281            HttpResponseMessage _httpResponse = null;
 7282            _httpRequest.Method = new HttpMethod("GET");
 7283            _httpRequest.RequestUri = new System.Uri(_url);
 284            // Set Headers
 285
 286
 7287            if (customHeaders != null)
 288            {
 0289                foreach(var _header in customHeaders)
 290                {
 0291                    if (_httpRequest.Headers.Contains(_header.Key))
 292                    {
 0293                        _httpRequest.Headers.Remove(_header.Key);
 294                    }
 0295                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 296                }
 297            }
 298
 299            // Serialize Request
 7300            string _requestContent = null;
 301            // Set Credentials
 7302            if (Client.Credentials != null)
 303            {
 7304                cancellationToken.ThrowIfCancellationRequested();
 7305                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 306            }
 307            // Send Request
 7308            if (_shouldTrace)
 309            {
 0310                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 311            }
 7312            cancellationToken.ThrowIfCancellationRequested();
 7313            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 7314            if (_shouldTrace)
 315            {
 0316                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 317            }
 7318            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 7319            cancellationToken.ThrowIfCancellationRequested();
 7320            string _responseContent = null;
 7321            if ((int)_statusCode != 200)
 322            {
 0323                var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st
 324                try
 325                {
 0326                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 0327                    ErrorResponse _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res
 0328                    if (_errorBody != null)
 329                    {
 0330                        ex.Body = _errorBody;
 331                    }
 0332                }
 0333                catch (JsonException)
 334                {
 335                    // Ignore the exception
 0336                }
 0337                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 0338                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 0339                if (_shouldTrace)
 340                {
 0341                    ServiceClientTracing.Error(_invocationId, ex);
 342                }
 0343                _httpRequest.Dispose();
 0344                if (_httpResponse != null)
 345                {
 0346                    _httpResponse.Dispose();
 347                }
 0348                throw ex;
 349            }
 350            // Create Result
 7351            var _result = new HttpOperationResponse<IList<ModelTrainingInfo>>();
 7352            _result.Request = _httpRequest;
 7353            _result.Response = _httpResponse;
 354            // Deserialize Response
 7355            if ((int)_statusCode == 200)
 356            {
 7357                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 358                try
 359                {
 7360                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<IList<ModelTrainingInfo>>(_respo
 7361                }
 0362                catch (JsonException ex)
 363                {
 0364                    _httpRequest.Dispose();
 0365                    if (_httpResponse != null)
 366                    {
 0367                        _httpResponse.Dispose();
 368                    }
 0369                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 370                }
 371            }
 7372            if (_shouldTrace)
 373            {
 0374                ServiceClientTracing.Exit(_invocationId, _result);
 375            }
 7376            return _result;
 7377        }
 378
 379    }
 380}