< Summary

Class:Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime.PredictionOperations
Assembly:Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime
File(s):C:\Git\azure-sdk-for-net\sdk\cognitiveservices\Language.LUIS.Runtime\src\Generated\PredictionOperations.cs
Covered lines:122
Uncovered lines:72
Coverable lines:194
Total lines:468
Line coverage:62.8% (122 of 194)
Covered branches:57
Total branches:98
Branch coverage:58.1% (57 of 98)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-80%50%
get_Client()-100%100%
GetVersionPredictionWithHttpMessagesAsync()-55.32%52.08%
GetSlotPredictionWithHttpMessagesAsync()-69.15%64.58%

File(s)

C:\Git\azure-sdk-for-net\sdk\cognitiveservices\Language.LUIS.Runtime\src\Generated\PredictionOperations.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.Runtime
 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    /// PredictionOperations operations.
 27    /// </summary>
 28    public partial class PredictionOperations : IServiceOperations<LUISRuntimeClient>, IPredictionOperations
 29    {
 30        /// <summary>
 31        /// Initializes a new instance of the PredictionOperations 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>
 839        public PredictionOperations(LUISRuntimeClient client)
 40        {
 841            if (client == null)
 42            {
 043                throw new System.ArgumentNullException("client");
 44            }
 845            Client = client;
 846        }
 47
 48        /// <summary>
 49        /// Gets a reference to the LUISRuntimeClient
 50        /// </summary>
 7251        public LUISRuntimeClient Client { get; private set; }
 52
 53        /// <summary>
 54        /// Gets the predictions for an application version.
 55        /// </summary>
 56        /// <param name='appId'>
 57        /// The application ID.
 58        /// </param>
 59        /// <param name='versionId'>
 60        /// The application version ID.
 61        /// </param>
 62        /// <param name='predictionRequest'>
 63        /// The prediction request parameters.
 64        /// </param>
 65        /// <param name='verbose'>
 66        /// Indicates whether to get extra metadata for the entities predictions or
 67        /// not.
 68        /// </param>
 69        /// <param name='showAllIntents'>
 70        /// Indicates whether to return all the intents in the response or just the top
 71        /// intent.
 72        /// </param>
 73        /// <param name='log'>
 74        /// Indicates whether to log the endpoint query or not.
 75        /// </param>
 76        /// <param name='customHeaders'>
 77        /// Headers that will be added to request.
 78        /// </param>
 79        /// <param name='cancellationToken'>
 80        /// The cancellation token.
 81        /// </param>
 82        /// <exception cref="ErrorException">
 83        /// Thrown when the operation returned an invalid status code
 84        /// </exception>
 85        /// <exception cref="SerializationException">
 86        /// Thrown when unable to deserialize the response
 87        /// </exception>
 88        /// <exception cref="ValidationException">
 89        /// Thrown when a required parameter is null
 90        /// </exception>
 91        /// <exception cref="System.ArgumentNullException">
 92        /// Thrown when a required parameter is null
 93        /// </exception>
 94        /// <return>
 95        /// A response object containing the response body and response headers.
 96        /// </return>
 97        public async Task<HttpOperationResponse<PredictionResponse>> GetVersionPredictionWithHttpMessagesAsync(System.Gu
 98        {
 299            if (Client.Endpoint == null)
 100            {
 0101                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint");
 102            }
 2103            if (versionId == null)
 104            {
 0105                throw new ValidationException(ValidationRules.CannotBeNull, "versionId");
 106            }
 2107            if (predictionRequest == null)
 108            {
 0109                throw new ValidationException(ValidationRules.CannotBeNull, "predictionRequest");
 110            }
 2111            if (predictionRequest != null)
 112            {
 2113                predictionRequest.Validate();
 114            }
 115            // Tracing
 2116            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 2117            string _invocationId = null;
 2118            if (_shouldTrace)
 119            {
 0120                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0121                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0122                tracingParameters.Add("appId", appId);
 0123                tracingParameters.Add("versionId", versionId);
 0124                tracingParameters.Add("verbose", verbose);
 0125                tracingParameters.Add("showAllIntents", showAllIntents);
 0126                tracingParameters.Add("log", log);
 0127                tracingParameters.Add("predictionRequest", predictionRequest);
 0128                tracingParameters.Add("cancellationToken", cancellationToken);
 0129                ServiceClientTracing.Enter(_invocationId, this, "GetVersionPrediction", tracingParameters);
 130            }
 131            // Construct URL
 2132            var _baseUrl = Client.BaseUri;
 2133            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "apps/{appId}/versions/{versionId}/predict";
 2134            _url = _url.Replace("{Endpoint}", Client.Endpoint);
 2135            _url = _url.Replace("{appId}", System.Uri.EscapeDataString(Rest.Serialization.SafeJsonConvert.SerializeObjec
 2136            _url = _url.Replace("{versionId}", System.Uri.EscapeDataString(versionId));
 2137            List<string> _queryParameters = new List<string>();
 2138            if (verbose != null)
 139            {
 2140                _queryParameters.Add(string.Format("verbose={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJso
 141            }
 2142            if (showAllIntents != null)
 143            {
 2144                _queryParameters.Add(string.Format("show-all-intents={0}", System.Uri.EscapeDataString(Rest.Serializatio
 145            }
 2146            if (log != null)
 147            {
 0148                _queryParameters.Add(string.Format("log={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJsonCon
 149            }
 2150            if (_queryParameters.Count > 0)
 151            {
 2152                _url += "?" + string.Join("&", _queryParameters);
 153            }
 154            // Create HTTP transport objects
 2155            var _httpRequest = new HttpRequestMessage();
 2156            HttpResponseMessage _httpResponse = null;
 2157            _httpRequest.Method = new HttpMethod("POST");
 2158            _httpRequest.RequestUri = new System.Uri(_url);
 159            // Set Headers
 160
 161
 2162            if (customHeaders != null)
 163            {
 0164                foreach(var _header in customHeaders)
 165                {
 0166                    if (_httpRequest.Headers.Contains(_header.Key))
 167                    {
 0168                        _httpRequest.Headers.Remove(_header.Key);
 169                    }
 0170                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 171                }
 172            }
 173
 174            // Serialize Request
 2175            string _requestContent = null;
 2176            if(predictionRequest != null)
 177            {
 2178                _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(predictionRequest, Client.Serializa
 2179                _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8);
 2180                _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio
 181            }
 182            // Set Credentials
 2183            if (Client.Credentials != null)
 184            {
 2185                cancellationToken.ThrowIfCancellationRequested();
 2186                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 187            }
 188            // Send Request
 2189            if (_shouldTrace)
 190            {
 0191                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 192            }
 2193            cancellationToken.ThrowIfCancellationRequested();
 2194            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 2195            if (_shouldTrace)
 196            {
 0197                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 198            }
 2199            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 2200            cancellationToken.ThrowIfCancellationRequested();
 2201            string _responseContent = null;
 2202            if ((int)_statusCode != 200)
 203            {
 0204                var ex = new ErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusCode
 205                try
 206                {
 0207                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 0208                    Error _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<Error>(_responseContent, Cl
 0209                    if (_errorBody != null)
 210                    {
 0211                        ex.Body = _errorBody;
 212                    }
 0213                }
 0214                catch (JsonException)
 215                {
 216                    // Ignore the exception
 0217                }
 0218                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 0219                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 0220                if (_shouldTrace)
 221                {
 0222                    ServiceClientTracing.Error(_invocationId, ex);
 223                }
 0224                _httpRequest.Dispose();
 0225                if (_httpResponse != null)
 226                {
 0227                    _httpResponse.Dispose();
 228                }
 0229                throw ex;
 230            }
 231            // Create Result
 2232            var _result = new HttpOperationResponse<PredictionResponse>();
 2233            _result.Request = _httpRequest;
 2234            _result.Response = _httpResponse;
 235            // Deserialize Response
 2236            if ((int)_statusCode == 200)
 237            {
 2238                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 239                try
 240                {
 2241                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<PredictionResponse>(_responseCon
 2242                }
 0243                catch (JsonException ex)
 244                {
 0245                    _httpRequest.Dispose();
 0246                    if (_httpResponse != null)
 247                    {
 0248                        _httpResponse.Dispose();
 249                    }
 0250                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 251                }
 252            }
 2253            if (_shouldTrace)
 254            {
 0255                ServiceClientTracing.Exit(_invocationId, _result);
 256            }
 2257            return _result;
 2258        }
 259
 260        /// <summary>
 261        /// Gets the predictions for an application slot.
 262        /// </summary>
 263        /// <param name='appId'>
 264        /// The application ID.
 265        /// </param>
 266        /// <param name='slotName'>
 267        /// The application slot name.
 268        /// </param>
 269        /// <param name='predictionRequest'>
 270        /// The prediction request parameters.
 271        /// </param>
 272        /// <param name='verbose'>
 273        /// Indicates whether to get extra metadata for the entities predictions or
 274        /// not.
 275        /// </param>
 276        /// <param name='showAllIntents'>
 277        /// Indicates whether to return all the intents in the response or just the top
 278        /// intent.
 279        /// </param>
 280        /// <param name='log'>
 281        /// Indicates whether to log the endpoint query or not.
 282        /// </param>
 283        /// <param name='customHeaders'>
 284        /// Headers that will be added to request.
 285        /// </param>
 286        /// <param name='cancellationToken'>
 287        /// The cancellation token.
 288        /// </param>
 289        /// <exception cref="ErrorException">
 290        /// Thrown when the operation returned an invalid status code
 291        /// </exception>
 292        /// <exception cref="SerializationException">
 293        /// Thrown when unable to deserialize the response
 294        /// </exception>
 295        /// <exception cref="ValidationException">
 296        /// Thrown when a required parameter is null
 297        /// </exception>
 298        /// <exception cref="System.ArgumentNullException">
 299        /// Thrown when a required parameter is null
 300        /// </exception>
 301        /// <return>
 302        /// A response object containing the response body and response headers.
 303        /// </return>
 304        public async Task<HttpOperationResponse<PredictionResponse>> GetSlotPredictionWithHttpMessagesAsync(System.Guid 
 305        {
 6306            if (Client.Endpoint == null)
 307            {
 0308                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint");
 309            }
 6310            if (slotName == null)
 311            {
 0312                throw new ValidationException(ValidationRules.CannotBeNull, "slotName");
 313            }
 6314            if (predictionRequest == null)
 315            {
 0316                throw new ValidationException(ValidationRules.CannotBeNull, "predictionRequest");
 317            }
 6318            if (predictionRequest != null)
 319            {
 6320                predictionRequest.Validate();
 321            }
 322            // Tracing
 4323            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 4324            string _invocationId = null;
 4325            if (_shouldTrace)
 326            {
 0327                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0328                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0329                tracingParameters.Add("appId", appId);
 0330                tracingParameters.Add("slotName", slotName);
 0331                tracingParameters.Add("verbose", verbose);
 0332                tracingParameters.Add("showAllIntents", showAllIntents);
 0333                tracingParameters.Add("log", log);
 0334                tracingParameters.Add("predictionRequest", predictionRequest);
 0335                tracingParameters.Add("cancellationToken", cancellationToken);
 0336                ServiceClientTracing.Enter(_invocationId, this, "GetSlotPrediction", tracingParameters);
 337            }
 338            // Construct URL
 4339            var _baseUrl = Client.BaseUri;
 4340            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "apps/{appId}/slots/{slotName}/predict";
 4341            _url = _url.Replace("{Endpoint}", Client.Endpoint);
 4342            _url = _url.Replace("{appId}", System.Uri.EscapeDataString(Rest.Serialization.SafeJsonConvert.SerializeObjec
 4343            _url = _url.Replace("{slotName}", System.Uri.EscapeDataString(slotName));
 4344            List<string> _queryParameters = new List<string>();
 4345            if (verbose != null)
 346            {
 2347                _queryParameters.Add(string.Format("verbose={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJso
 348            }
 4349            if (showAllIntents != null)
 350            {
 2351                _queryParameters.Add(string.Format("show-all-intents={0}", System.Uri.EscapeDataString(Rest.Serializatio
 352            }
 4353            if (log != null)
 354            {
 0355                _queryParameters.Add(string.Format("log={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJsonCon
 356            }
 4357            if (_queryParameters.Count > 0)
 358            {
 2359                _url += "?" + string.Join("&", _queryParameters);
 360            }
 361            // Create HTTP transport objects
 4362            var _httpRequest = new HttpRequestMessage();
 4363            HttpResponseMessage _httpResponse = null;
 4364            _httpRequest.Method = new HttpMethod("POST");
 4365            _httpRequest.RequestUri = new System.Uri(_url);
 366            // Set Headers
 367
 368
 4369            if (customHeaders != null)
 370            {
 0371                foreach(var _header in customHeaders)
 372                {
 0373                    if (_httpRequest.Headers.Contains(_header.Key))
 374                    {
 0375                        _httpRequest.Headers.Remove(_header.Key);
 376                    }
 0377                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 378                }
 379            }
 380
 381            // Serialize Request
 4382            string _requestContent = null;
 4383            if(predictionRequest != null)
 384            {
 4385                _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(predictionRequest, Client.Serializa
 4386                _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8);
 4387                _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio
 388            }
 389            // Set Credentials
 4390            if (Client.Credentials != null)
 391            {
 4392                cancellationToken.ThrowIfCancellationRequested();
 4393                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 394            }
 395            // Send Request
 4396            if (_shouldTrace)
 397            {
 0398                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 399            }
 4400            cancellationToken.ThrowIfCancellationRequested();
 4401            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 4402            if (_shouldTrace)
 403            {
 0404                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 405            }
 4406            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 4407            cancellationToken.ThrowIfCancellationRequested();
 4408            string _responseContent = null;
 4409            if ((int)_statusCode != 200)
 410            {
 2411                var ex = new ErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusCode
 412                try
 413                {
 2414                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 2415                    Error _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<Error>(_responseContent, Cl
 2416                    if (_errorBody != null)
 417                    {
 2418                        ex.Body = _errorBody;
 419                    }
 2420                }
 0421                catch (JsonException)
 422                {
 423                    // Ignore the exception
 0424                }
 2425                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 2426                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 2427                if (_shouldTrace)
 428                {
 0429                    ServiceClientTracing.Error(_invocationId, ex);
 430                }
 2431                _httpRequest.Dispose();
 2432                if (_httpResponse != null)
 433                {
 2434                    _httpResponse.Dispose();
 435                }
 2436                throw ex;
 437            }
 438            // Create Result
 2439            var _result = new HttpOperationResponse<PredictionResponse>();
 2440            _result.Request = _httpRequest;
 2441            _result.Response = _httpResponse;
 442            // Deserialize Response
 2443            if ((int)_statusCode == 200)
 444            {
 2445                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 446                try
 447                {
 2448                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<PredictionResponse>(_responseCon
 2449                }
 0450                catch (JsonException ex)
 451                {
 0452                    _httpRequest.Dispose();
 0453                    if (_httpResponse != null)
 454                    {
 0455                        _httpResponse.Dispose();
 456                    }
 0457                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 458                }
 459            }
 2460            if (_shouldTrace)
 461            {
 0462                ServiceClientTracing.Exit(_invocationId, _result);
 463            }
 2464            return _result;
 2465        }
 466
 467    }
 468}