< Summary

Class:Microsoft.Azure.CognitiveServices.Personalizer.Events
Assembly:Microsoft.Azure.CognitiveServices.Personalizer
File(s):C:\Git\azure-sdk-for-net\sdk\cognitiveservices\Personalizer\src\Generated\Events.cs
Covered lines:46
Uncovered lines:99
Coverable lines:145
Total lines:358
Line coverage:31.7% (46 of 145)
Covered branches:20
Total branches:76
Branch coverage:26.3% (20 of 76)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-80%50%
get_Client()-100%100%
RewardWithHttpMessagesAsync()-55.41%47.5%
ActivateWithHttpMessagesAsync()-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\cognitiveservices\Personalizer\src\Generated\Events.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    /// Events operations.
 27    /// </summary>
 28    public partial class Events : IServiceOperations<PersonalizerClient>, IEvents
 29    {
 30        /// <summary>
 31        /// Initializes a new instance of the Events 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 Events(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>
 4851        public PersonalizerClient Client { get; private set; }
 52
 53        /// <summary>
 54        /// Post Reward.
 55        /// </summary>
 56        /// <remarks>
 57        /// Report reward that resulted from using the action specified in
 58        /// rewardActionId for the specified event.
 59        /// </remarks>
 60        /// <param name='eventId'>
 61        /// The event id this reward applies to.
 62        /// </param>
 63        /// <param name='reward'>
 64        /// The reward should be a floating point number, typically between 0 and 1.
 65        /// </param>
 66        /// <param name='customHeaders'>
 67        /// Headers that will be added to request.
 68        /// </param>
 69        /// <param name='cancellationToken'>
 70        /// The cancellation token.
 71        /// </param>
 72        /// <exception cref="ErrorResponseException">
 73        /// Thrown when the operation returned an invalid status code
 74        /// </exception>
 75        /// <exception cref="ValidationException">
 76        /// Thrown when a required parameter is null
 77        /// </exception>
 78        /// <exception cref="System.ArgumentNullException">
 79        /// Thrown when a required parameter is null
 80        /// </exception>
 81        /// <return>
 82        /// A response object containing the response body and response headers.
 83        /// </return>
 84        public async Task<HttpOperationResponse> RewardWithHttpMessagesAsync(string eventId, RewardRequest reward, Dicti
 85        {
 286            if (Client.Endpoint == null)
 87            {
 088                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint");
 89            }
 290            if (eventId == null)
 91            {
 092                throw new ValidationException(ValidationRules.CannotBeNull, "eventId");
 93            }
 294            if (eventId != null)
 95            {
 296                if (eventId.Length > 256)
 97                {
 098                    throw new ValidationException(ValidationRules.MaxLength, "eventId", 256);
 99                }
 100            }
 2101            if (reward == null)
 102            {
 0103                throw new ValidationException(ValidationRules.CannotBeNull, "reward");
 104            }
 2105            if (reward != null)
 106            {
 2107                reward.Validate();
 108            }
 109            // Tracing
 2110            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 2111            string _invocationId = null;
 2112            if (_shouldTrace)
 113            {
 0114                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0115                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0116                tracingParameters.Add("eventId", eventId);
 0117                tracingParameters.Add("reward", reward);
 0118                tracingParameters.Add("cancellationToken", cancellationToken);
 0119                ServiceClientTracing.Enter(_invocationId, this, "Reward", tracingParameters);
 120            }
 121            // Construct URL
 2122            var _baseUrl = Client.BaseUri;
 2123            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "events/{eventId}/reward";
 2124            _url = _url.Replace("{Endpoint}", Client.Endpoint);
 2125            _url = _url.Replace("{eventId}", System.Uri.EscapeDataString(eventId));
 126            // Create HTTP transport objects
 2127            var _httpRequest = new HttpRequestMessage();
 2128            HttpResponseMessage _httpResponse = null;
 2129            _httpRequest.Method = new HttpMethod("POST");
 2130            _httpRequest.RequestUri = new System.Uri(_url);
 131            // Set Headers
 132
 133
 2134            if (customHeaders != null)
 135            {
 0136                foreach(var _header in customHeaders)
 137                {
 0138                    if (_httpRequest.Headers.Contains(_header.Key))
 139                    {
 0140                        _httpRequest.Headers.Remove(_header.Key);
 141                    }
 0142                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 143                }
 144            }
 145
 146            // Serialize Request
 2147            string _requestContent = null;
 2148            if(reward != null)
 149            {
 2150                _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(reward, Client.SerializationSetting
 2151                _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8);
 2152                _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio
 153            }
 154            // Set Credentials
 2155            if (Client.Credentials != null)
 156            {
 2157                cancellationToken.ThrowIfCancellationRequested();
 2158                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 159            }
 160            // Send Request
 2161            if (_shouldTrace)
 162            {
 0163                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 164            }
 2165            cancellationToken.ThrowIfCancellationRequested();
 2166            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 2167            if (_shouldTrace)
 168            {
 0169                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 170            }
 2171            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 2172            cancellationToken.ThrowIfCancellationRequested();
 2173            string _responseContent = null;
 2174            if ((int)_statusCode != 204)
 175            {
 0176                var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st
 177                try
 178                {
 0179                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 0180                    ErrorResponse _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res
 0181                    if (_errorBody != null)
 182                    {
 0183                        ex.Body = _errorBody;
 184                    }
 0185                }
 0186                catch (JsonException)
 187                {
 188                    // Ignore the exception
 0189                }
 0190                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 0191                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 0192                if (_shouldTrace)
 193                {
 0194                    ServiceClientTracing.Error(_invocationId, ex);
 195                }
 0196                _httpRequest.Dispose();
 0197                if (_httpResponse != null)
 198                {
 0199                    _httpResponse.Dispose();
 200                }
 0201                throw ex;
 202            }
 203            // Create Result
 2204            var _result = new HttpOperationResponse();
 2205            _result.Request = _httpRequest;
 2206            _result.Response = _httpResponse;
 2207            if (_shouldTrace)
 208            {
 0209                ServiceClientTracing.Exit(_invocationId, _result);
 210            }
 2211            return _result;
 2212        }
 213
 214        /// <summary>
 215        /// Activate Event.
 216        /// </summary>
 217        /// <remarks>
 218        /// Report that the specified event was actually displayed to the user and a
 219        /// reward should be expected for it
 220        /// </remarks>
 221        /// <param name='eventId'>
 222        /// The event ID this activation applies to.
 223        /// </param>
 224        /// <param name='customHeaders'>
 225        /// Headers that will be added to request.
 226        /// </param>
 227        /// <param name='cancellationToken'>
 228        /// The cancellation token.
 229        /// </param>
 230        /// <exception cref="ErrorResponseException">
 231        /// Thrown when the operation returned an invalid status code
 232        /// </exception>
 233        /// <exception cref="ValidationException">
 234        /// Thrown when a required parameter is null
 235        /// </exception>
 236        /// <exception cref="System.ArgumentNullException">
 237        /// Thrown when a required parameter is null
 238        /// </exception>
 239        /// <return>
 240        /// A response object containing the response body and response headers.
 241        /// </return>
 242        public async Task<HttpOperationResponse> ActivateWithHttpMessagesAsync(string eventId, Dictionary<string, List<s
 243        {
 0244            if (Client.Endpoint == null)
 245            {
 0246                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint");
 247            }
 0248            if (eventId == null)
 249            {
 0250                throw new ValidationException(ValidationRules.CannotBeNull, "eventId");
 251            }
 0252            if (eventId != null)
 253            {
 0254                if (eventId.Length > 256)
 255                {
 0256                    throw new ValidationException(ValidationRules.MaxLength, "eventId", 256);
 257                }
 258            }
 259            // Tracing
 0260            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 0261            string _invocationId = null;
 0262            if (_shouldTrace)
 263            {
 0264                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0265                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0266                tracingParameters.Add("eventId", eventId);
 0267                tracingParameters.Add("cancellationToken", cancellationToken);
 0268                ServiceClientTracing.Enter(_invocationId, this, "Activate", tracingParameters);
 269            }
 270            // Construct URL
 0271            var _baseUrl = Client.BaseUri;
 0272            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "events/{eventId}/activate";
 0273            _url = _url.Replace("{Endpoint}", Client.Endpoint);
 0274            _url = _url.Replace("{eventId}", System.Uri.EscapeDataString(eventId));
 275            // Create HTTP transport objects
 0276            var _httpRequest = new HttpRequestMessage();
 0277            HttpResponseMessage _httpResponse = null;
 0278            _httpRequest.Method = new HttpMethod("POST");
 0279            _httpRequest.RequestUri = new System.Uri(_url);
 280            // Set Headers
 281
 282
 0283            if (customHeaders != null)
 284            {
 0285                foreach(var _header in customHeaders)
 286                {
 0287                    if (_httpRequest.Headers.Contains(_header.Key))
 288                    {
 0289                        _httpRequest.Headers.Remove(_header.Key);
 290                    }
 0291                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 292                }
 293            }
 294
 295            // Serialize Request
 0296            string _requestContent = null;
 297            // Set Credentials
 0298            if (Client.Credentials != null)
 299            {
 0300                cancellationToken.ThrowIfCancellationRequested();
 0301                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 302            }
 303            // Send Request
 0304            if (_shouldTrace)
 305            {
 0306                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 307            }
 0308            cancellationToken.ThrowIfCancellationRequested();
 0309            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 0310            if (_shouldTrace)
 311            {
 0312                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 313            }
 0314            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 0315            cancellationToken.ThrowIfCancellationRequested();
 0316            string _responseContent = null;
 0317            if ((int)_statusCode != 204)
 318            {
 0319                var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st
 320                try
 321                {
 0322                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 0323                    ErrorResponse _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res
 0324                    if (_errorBody != null)
 325                    {
 0326                        ex.Body = _errorBody;
 327                    }
 0328                }
 0329                catch (JsonException)
 330                {
 331                    // Ignore the exception
 0332                }
 0333                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 0334                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 0335                if (_shouldTrace)
 336                {
 0337                    ServiceClientTracing.Error(_invocationId, ex);
 338                }
 0339                _httpRequest.Dispose();
 0340                if (_httpResponse != null)
 341                {
 0342                    _httpResponse.Dispose();
 343                }
 0344                throw ex;
 345            }
 346            // Create Result
 0347            var _result = new HttpOperationResponse();
 0348            _result.Request = _httpRequest;
 0349            _result.Response = _httpResponse;
 0350            if (_shouldTrace)
 351            {
 0352                ServiceClientTracing.Exit(_invocationId, _result);
 353            }
 0354            return _result;
 0355        }
 356
 357    }
 358}