< Summary

Class:Microsoft.Azure.CognitiveServices.Personalizer.Policy
Assembly:Microsoft.Azure.CognitiveServices.Personalizer
File(s):C:\Git\azure-sdk-for-net\sdk\cognitiveservices\Personalizer\src\Generated\Policy.cs
Covered lines:114
Uncovered lines:94
Coverable lines:208
Total lines:500
Line coverage:54.8% (114 of 208)
Covered branches:42
Total branches:104
Branch coverage:40.3% (42 of 104)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-80%50%
get_Client()-100%100%
GetWithHttpMessagesAsync()-53.97%37.5%
UpdateWithHttpMessagesAsync()-53.95%44.74%
ResetWithHttpMessagesAsync()-53.97%37.5%

File(s)

C:\Git\azure-sdk-for-net\sdk\cognitiveservices\Personalizer\src\Generated\Policy.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.Personalizer
 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    /// Policy operations.
 27    /// </summary>
 28    public partial class Policy : IServiceOperations<PersonalizerClient>, IPolicy
 29    {
 30        /// <summary>
 31        /// Initializes a new instance of the Policy 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>
 3439        public Policy(PersonalizerClient client)
 40        {
 3441            if (client == null)
 42            {
 043                throw new System.ArgumentNullException("client");
 44            }
 3445            Client = client;
 3446        }
 47
 48        /// <summary>
 49        /// Gets a reference to the PersonalizerClient
 50        /// </summary>
 7851        public PersonalizerClient Client { get; private set; }
 52
 53        /// <summary>
 54        /// Get Policy.
 55        /// </summary>
 56        /// <remarks>
 57        /// Get the Learning settings currently used by the Personalizer service.
 58        /// </remarks>
 59        /// <param name='customHeaders'>
 60        /// Headers that will be added to request.
 61        /// </param>
 62        /// <param name='cancellationToken'>
 63        /// The cancellation token.
 64        /// </param>
 65        /// <exception cref="HttpOperationException">
 66        /// Thrown when the operation returned an invalid status code
 67        /// </exception>
 68        /// <exception cref="SerializationException">
 69        /// Thrown when unable to deserialize the response
 70        /// </exception>
 71        /// <exception cref="ValidationException">
 72        /// Thrown when a required parameter is null
 73        /// </exception>
 74        /// <exception cref="System.ArgumentNullException">
 75        /// Thrown when a required parameter is null
 76        /// </exception>
 77        /// <return>
 78        /// A response object containing the response body and response headers.
 79        /// </return>
 80        public async Task<HttpOperationResponse<PolicyContract>> GetWithHttpMessagesAsync(Dictionary<string, List<string
 81        {
 282            if (Client.Endpoint == null)
 83            {
 084                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint");
 85            }
 86            // Tracing
 287            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 288            string _invocationId = null;
 289            if (_shouldTrace)
 90            {
 091                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 092                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 093                tracingParameters.Add("cancellationToken", cancellationToken);
 094                ServiceClientTracing.Enter(_invocationId, this, "Get", tracingParameters);
 95            }
 96            // Construct URL
 297            var _baseUrl = Client.BaseUri;
 298            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "configurations/policy";
 299            _url = _url.Replace("{Endpoint}", Client.Endpoint);
 100            // Create HTTP transport objects
 2101            var _httpRequest = new HttpRequestMessage();
 2102            HttpResponseMessage _httpResponse = null;
 2103            _httpRequest.Method = new HttpMethod("GET");
 2104            _httpRequest.RequestUri = new System.Uri(_url);
 105            // Set Headers
 106
 107
 2108            if (customHeaders != null)
 109            {
 0110                foreach(var _header in customHeaders)
 111                {
 0112                    if (_httpRequest.Headers.Contains(_header.Key))
 113                    {
 0114                        _httpRequest.Headers.Remove(_header.Key);
 115                    }
 0116                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 117                }
 118            }
 119
 120            // Serialize Request
 2121            string _requestContent = null;
 122            // Set Credentials
 2123            if (Client.Credentials != null)
 124            {
 2125                cancellationToken.ThrowIfCancellationRequested();
 2126                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 127            }
 128            // Send Request
 2129            if (_shouldTrace)
 130            {
 0131                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 132            }
 2133            cancellationToken.ThrowIfCancellationRequested();
 2134            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 2135            if (_shouldTrace)
 136            {
 0137                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 138            }
 2139            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 2140            cancellationToken.ThrowIfCancellationRequested();
 2141            string _responseContent = null;
 2142            if ((int)_statusCode != 200)
 143            {
 0144                var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _st
 0145                if (_httpResponse.Content != null) {
 0146                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 147                }
 148                else {
 0149                    _responseContent = string.Empty;
 150                }
 0151                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 0152                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 0153                if (_shouldTrace)
 154                {
 0155                    ServiceClientTracing.Error(_invocationId, ex);
 156                }
 0157                _httpRequest.Dispose();
 0158                if (_httpResponse != null)
 159                {
 0160                    _httpResponse.Dispose();
 161                }
 0162                throw ex;
 163            }
 164            // Create Result
 2165            var _result = new HttpOperationResponse<PolicyContract>();
 2166            _result.Request = _httpRequest;
 2167            _result.Response = _httpResponse;
 168            // Deserialize Response
 2169            if ((int)_statusCode == 200)
 170            {
 2171                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 172                try
 173                {
 2174                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<PolicyContract>(_responseContent
 2175                }
 0176                catch (JsonException ex)
 177                {
 0178                    _httpRequest.Dispose();
 0179                    if (_httpResponse != null)
 180                    {
 0181                        _httpResponse.Dispose();
 182                    }
 0183                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 184                }
 185            }
 2186            if (_shouldTrace)
 187            {
 0188                ServiceClientTracing.Exit(_invocationId, _result);
 189            }
 2190            return _result;
 2191        }
 192
 193        /// <summary>
 194        /// Update Policy.
 195        /// </summary>
 196        /// <remarks>
 197        /// Update the Learning settings that the Personalizer service will use to
 198        /// train models.
 199        /// </remarks>
 200        /// <param name='policy'>
 201        /// The Learning settings.
 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="SerializationException">
 213        /// Thrown when unable to deserialize the response
 214        /// </exception>
 215        /// <exception cref="ValidationException">
 216        /// Thrown when a required parameter is null
 217        /// </exception>
 218        /// <exception cref="System.ArgumentNullException">
 219        /// Thrown when a required parameter is null
 220        /// </exception>
 221        /// <return>
 222        /// A response object containing the response body and response headers.
 223        /// </return>
 224        public async Task<HttpOperationResponse<PolicyContract>> UpdateWithHttpMessagesAsync(PolicyContract policy, Dict
 225        {
 2226            if (Client.Endpoint == null)
 227            {
 0228                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint");
 229            }
 2230            if (policy == null)
 231            {
 0232                throw new ValidationException(ValidationRules.CannotBeNull, "policy");
 233            }
 2234            if (policy != null)
 235            {
 2236                policy.Validate();
 237            }
 238            // Tracing
 2239            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 2240            string _invocationId = null;
 2241            if (_shouldTrace)
 242            {
 0243                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0244                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0245                tracingParameters.Add("policy", policy);
 0246                tracingParameters.Add("cancellationToken", cancellationToken);
 0247                ServiceClientTracing.Enter(_invocationId, this, "Update", tracingParameters);
 248            }
 249            // Construct URL
 2250            var _baseUrl = Client.BaseUri;
 2251            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "configurations/policy";
 2252            _url = _url.Replace("{Endpoint}", Client.Endpoint);
 253            // Create HTTP transport objects
 2254            var _httpRequest = new HttpRequestMessage();
 2255            HttpResponseMessage _httpResponse = null;
 2256            _httpRequest.Method = new HttpMethod("PUT");
 2257            _httpRequest.RequestUri = new System.Uri(_url);
 258            // Set Headers
 259
 260
 2261            if (customHeaders != null)
 262            {
 0263                foreach(var _header in customHeaders)
 264                {
 0265                    if (_httpRequest.Headers.Contains(_header.Key))
 266                    {
 0267                        _httpRequest.Headers.Remove(_header.Key);
 268                    }
 0269                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 270                }
 271            }
 272
 273            // Serialize Request
 2274            string _requestContent = null;
 2275            if(policy != null)
 276            {
 2277                _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(policy, Client.SerializationSetting
 2278                _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8);
 2279                _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio
 280            }
 281            // Set Credentials
 2282            if (Client.Credentials != null)
 283            {
 2284                cancellationToken.ThrowIfCancellationRequested();
 2285                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 286            }
 287            // Send Request
 2288            if (_shouldTrace)
 289            {
 0290                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 291            }
 2292            cancellationToken.ThrowIfCancellationRequested();
 2293            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 2294            if (_shouldTrace)
 295            {
 0296                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 297            }
 2298            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 2299            cancellationToken.ThrowIfCancellationRequested();
 2300            string _responseContent = null;
 2301            if ((int)_statusCode != 200)
 302            {
 0303                var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st
 304                try
 305                {
 0306                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 0307                    ErrorResponse _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res
 0308                    if (_errorBody != null)
 309                    {
 0310                        ex.Body = _errorBody;
 311                    }
 0312                }
 0313                catch (JsonException)
 314                {
 315                    // Ignore the exception
 0316                }
 0317                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 0318                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 0319                if (_shouldTrace)
 320                {
 0321                    ServiceClientTracing.Error(_invocationId, ex);
 322                }
 0323                _httpRequest.Dispose();
 0324                if (_httpResponse != null)
 325                {
 0326                    _httpResponse.Dispose();
 327                }
 0328                throw ex;
 329            }
 330            // Create Result
 2331            var _result = new HttpOperationResponse<PolicyContract>();
 2332            _result.Request = _httpRequest;
 2333            _result.Response = _httpResponse;
 334            // Deserialize Response
 2335            if ((int)_statusCode == 200)
 336            {
 2337                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 338                try
 339                {
 2340                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<PolicyContract>(_responseContent
 2341                }
 0342                catch (JsonException ex)
 343                {
 0344                    _httpRequest.Dispose();
 0345                    if (_httpResponse != null)
 346                    {
 0347                        _httpResponse.Dispose();
 348                    }
 0349                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 350                }
 351            }
 2352            if (_shouldTrace)
 353            {
 0354                ServiceClientTracing.Exit(_invocationId, _result);
 355            }
 2356            return _result;
 2357        }
 358
 359        /// <summary>
 360        /// Reset Policy.
 361        /// </summary>
 362        /// <remarks>
 363        /// Resets the Learning settings of the Personalizer service to default.
 364        /// </remarks>
 365        /// <param name='customHeaders'>
 366        /// Headers that will be added to request.
 367        /// </param>
 368        /// <param name='cancellationToken'>
 369        /// The cancellation token.
 370        /// </param>
 371        /// <exception cref="HttpOperationException">
 372        /// Thrown when the operation returned an invalid status code
 373        /// </exception>
 374        /// <exception cref="SerializationException">
 375        /// Thrown when unable to deserialize the response
 376        /// </exception>
 377        /// <exception cref="ValidationException">
 378        /// Thrown when a required parameter is null
 379        /// </exception>
 380        /// <exception cref="System.ArgumentNullException">
 381        /// Thrown when a required parameter is null
 382        /// </exception>
 383        /// <return>
 384        /// A response object containing the response body and response headers.
 385        /// </return>
 386        public async Task<HttpOperationResponse<PolicyContract>> ResetWithHttpMessagesAsync(Dictionary<string, List<stri
 387        {
 2388            if (Client.Endpoint == null)
 389            {
 0390                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint");
 391            }
 392            // Tracing
 2393            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 2394            string _invocationId = null;
 2395            if (_shouldTrace)
 396            {
 0397                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0398                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0399                tracingParameters.Add("cancellationToken", cancellationToken);
 0400                ServiceClientTracing.Enter(_invocationId, this, "Reset", tracingParameters);
 401            }
 402            // Construct URL
 2403            var _baseUrl = Client.BaseUri;
 2404            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "configurations/policy";
 2405            _url = _url.Replace("{Endpoint}", Client.Endpoint);
 406            // Create HTTP transport objects
 2407            var _httpRequest = new HttpRequestMessage();
 2408            HttpResponseMessage _httpResponse = null;
 2409            _httpRequest.Method = new HttpMethod("DELETE");
 2410            _httpRequest.RequestUri = new System.Uri(_url);
 411            // Set Headers
 412
 413
 2414            if (customHeaders != null)
 415            {
 0416                foreach(var _header in customHeaders)
 417                {
 0418                    if (_httpRequest.Headers.Contains(_header.Key))
 419                    {
 0420                        _httpRequest.Headers.Remove(_header.Key);
 421                    }
 0422                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 423                }
 424            }
 425
 426            // Serialize Request
 2427            string _requestContent = null;
 428            // Set Credentials
 2429            if (Client.Credentials != null)
 430            {
 2431                cancellationToken.ThrowIfCancellationRequested();
 2432                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 433            }
 434            // Send Request
 2435            if (_shouldTrace)
 436            {
 0437                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 438            }
 2439            cancellationToken.ThrowIfCancellationRequested();
 2440            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 2441            if (_shouldTrace)
 442            {
 0443                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 444            }
 2445            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 2446            cancellationToken.ThrowIfCancellationRequested();
 2447            string _responseContent = null;
 2448            if ((int)_statusCode != 200)
 449            {
 0450                var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _st
 0451                if (_httpResponse.Content != null) {
 0452                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 453                }
 454                else {
 0455                    _responseContent = string.Empty;
 456                }
 0457                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 0458                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 0459                if (_shouldTrace)
 460                {
 0461                    ServiceClientTracing.Error(_invocationId, ex);
 462                }
 0463                _httpRequest.Dispose();
 0464                if (_httpResponse != null)
 465                {
 0466                    _httpResponse.Dispose();
 467                }
 0468                throw ex;
 469            }
 470            // Create Result
 2471            var _result = new HttpOperationResponse<PolicyContract>();
 2472            _result.Request = _httpRequest;
 2473            _result.Response = _httpResponse;
 474            // Deserialize Response
 2475            if ((int)_statusCode == 200)
 476            {
 2477                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 478                try
 479                {
 2480                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<PolicyContract>(_responseContent
 2481                }
 0482                catch (JsonException ex)
 483                {
 0484                    _httpRequest.Dispose();
 0485                    if (_httpResponse != null)
 486                    {
 0487                        _httpResponse.Dispose();
 488                    }
 0489                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 490                }
 491            }
 2492            if (_shouldTrace)
 493            {
 0494                ServiceClientTracing.Exit(_invocationId, _result);
 495            }
 2496            return _result;
 2497        }
 498
 499    }
 500}