< Summary

Class:Microsoft.Azure.CognitiveServices.Personalizer.ServiceConfigurationOperations
Assembly:Microsoft.Azure.CognitiveServices.Personalizer
File(s):C:\Git\azure-sdk-for-net\sdk\cognitiveservices\Personalizer\src\Generated\ServiceConfigurationOperations.cs
Covered lines:80
Uncovered lines:65
Coverable lines:145
Total lines:359
Line coverage:55.1% (80 of 145)
Covered branches:30
Total branches:72
Branch coverage:41.6% (30 of 72)

Metrics

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

File(s)

C:\Git\azure-sdk-for-net\sdk\cognitiveservices\Personalizer\src\Generated\ServiceConfigurationOperations.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    /// ServiceConfigurationOperations operations.
 27    /// </summary>
 28    public partial class ServiceConfigurationOperations : IServiceOperations<PersonalizerClient>, IServiceConfigurationO
 29    {
 30        /// <summary>
 31        /// Initializes a new instance of the ServiceConfigurationOperations 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 ServiceConfigurationOperations(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>
 6451        public PersonalizerClient Client { get; private set; }
 52
 53        /// <summary>
 54        /// Get Service Configuration.
 55        /// </summary>
 56        /// <remarks>
 57        /// Get the Personalizer service configuration.
 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<ServiceConfiguration>> GetWithHttpMessagesAsync(Dictionary<string, List<
 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/service";
 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<ServiceConfiguration>();
 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<ServiceConfiguration>(_responseC
 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 Service Configuration.
 195        /// </summary>
 196        /// <remarks>
 197        /// Update the Personalizer service configuration.
 198        /// </remarks>
 199        /// <param name='config'>
 200        /// The personalizer service configuration.
 201        /// </param>
 202        /// <param name='customHeaders'>
 203        /// Headers that will be added to request.
 204        /// </param>
 205        /// <param name='cancellationToken'>
 206        /// The cancellation token.
 207        /// </param>
 208        /// <exception cref="ErrorResponseException">
 209        /// Thrown when the operation returned an invalid status code
 210        /// </exception>
 211        /// <exception cref="SerializationException">
 212        /// Thrown when unable to deserialize the response
 213        /// </exception>
 214        /// <exception cref="ValidationException">
 215        /// Thrown when a required parameter is null
 216        /// </exception>
 217        /// <exception cref="System.ArgumentNullException">
 218        /// Thrown when a required parameter is null
 219        /// </exception>
 220        /// <return>
 221        /// A response object containing the response body and response headers.
 222        /// </return>
 223        public async Task<HttpOperationResponse<ServiceConfiguration>> UpdateWithHttpMessagesAsync(ServiceConfiguration 
 224        {
 2225            if (Client.Endpoint == null)
 226            {
 0227                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint");
 228            }
 2229            if (config == null)
 230            {
 0231                throw new ValidationException(ValidationRules.CannotBeNull, "config");
 232            }
 2233            if (config != null)
 234            {
 2235                config.Validate();
 236            }
 237            // Tracing
 2238            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 2239            string _invocationId = null;
 2240            if (_shouldTrace)
 241            {
 0242                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0243                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0244                tracingParameters.Add("config", config);
 0245                tracingParameters.Add("cancellationToken", cancellationToken);
 0246                ServiceClientTracing.Enter(_invocationId, this, "Update", tracingParameters);
 247            }
 248            // Construct URL
 2249            var _baseUrl = Client.BaseUri;
 2250            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "configurations/service";
 2251            _url = _url.Replace("{Endpoint}", Client.Endpoint);
 252            // Create HTTP transport objects
 2253            var _httpRequest = new HttpRequestMessage();
 2254            HttpResponseMessage _httpResponse = null;
 2255            _httpRequest.Method = new HttpMethod("PUT");
 2256            _httpRequest.RequestUri = new System.Uri(_url);
 257            // Set Headers
 258
 259
 2260            if (customHeaders != null)
 261            {
 0262                foreach(var _header in customHeaders)
 263                {
 0264                    if (_httpRequest.Headers.Contains(_header.Key))
 265                    {
 0266                        _httpRequest.Headers.Remove(_header.Key);
 267                    }
 0268                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 269                }
 270            }
 271
 272            // Serialize Request
 2273            string _requestContent = null;
 2274            if(config != null)
 275            {
 2276                _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(config, Client.SerializationSetting
 2277                _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8);
 2278                _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio
 279            }
 280            // Set Credentials
 2281            if (Client.Credentials != null)
 282            {
 2283                cancellationToken.ThrowIfCancellationRequested();
 2284                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 285            }
 286            // Send Request
 2287            if (_shouldTrace)
 288            {
 0289                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 290            }
 2291            cancellationToken.ThrowIfCancellationRequested();
 2292            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 2293            if (_shouldTrace)
 294            {
 0295                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 296            }
 2297            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 2298            cancellationToken.ThrowIfCancellationRequested();
 2299            string _responseContent = null;
 2300            if ((int)_statusCode != 200)
 301            {
 0302                var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st
 303                try
 304                {
 0305                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 0306                    ErrorResponse _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res
 0307                    if (_errorBody != null)
 308                    {
 0309                        ex.Body = _errorBody;
 310                    }
 0311                }
 0312                catch (JsonException)
 313                {
 314                    // Ignore the exception
 0315                }
 0316                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 0317                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 0318                if (_shouldTrace)
 319                {
 0320                    ServiceClientTracing.Error(_invocationId, ex);
 321                }
 0322                _httpRequest.Dispose();
 0323                if (_httpResponse != null)
 324                {
 0325                    _httpResponse.Dispose();
 326                }
 0327                throw ex;
 328            }
 329            // Create Result
 2330            var _result = new HttpOperationResponse<ServiceConfiguration>();
 2331            _result.Request = _httpRequest;
 2332            _result.Response = _httpResponse;
 333            // Deserialize Response
 2334            if ((int)_statusCode == 200)
 335            {
 2336                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 337                try
 338                {
 2339                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<ServiceConfiguration>(_responseC
 2340                }
 0341                catch (JsonException ex)
 342                {
 0343                    _httpRequest.Dispose();
 0344                    if (_httpResponse != null)
 345                    {
 0346                        _httpResponse.Dispose();
 347                    }
 0348                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 349                }
 350            }
 2351            if (_shouldTrace)
 352            {
 0353                ServiceClientTracing.Exit(_invocationId, _result);
 354            }
 2355            return _result;
 2356        }
 357
 358    }
 359}