| | 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 | |
|
| | 11 | | namespace 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> |
| 34 | 39 | | public Events(PersonalizerClient client) |
| | 40 | | { |
| 34 | 41 | | if (client == null) |
| | 42 | | { |
| 0 | 43 | | throw new System.ArgumentNullException("client"); |
| | 44 | | } |
| 34 | 45 | | Client = client; |
| 34 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets a reference to the PersonalizerClient |
| | 50 | | /// </summary> |
| 48 | 51 | | 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 | | { |
| 2 | 86 | | if (Client.Endpoint == null) |
| | 87 | | { |
| 0 | 88 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 89 | | } |
| 2 | 90 | | if (eventId == null) |
| | 91 | | { |
| 0 | 92 | | throw new ValidationException(ValidationRules.CannotBeNull, "eventId"); |
| | 93 | | } |
| 2 | 94 | | if (eventId != null) |
| | 95 | | { |
| 2 | 96 | | if (eventId.Length > 256) |
| | 97 | | { |
| 0 | 98 | | throw new ValidationException(ValidationRules.MaxLength, "eventId", 256); |
| | 99 | | } |
| | 100 | | } |
| 2 | 101 | | if (reward == null) |
| | 102 | | { |
| 0 | 103 | | throw new ValidationException(ValidationRules.CannotBeNull, "reward"); |
| | 104 | | } |
| 2 | 105 | | if (reward != null) |
| | 106 | | { |
| 2 | 107 | | reward.Validate(); |
| | 108 | | } |
| | 109 | | // Tracing |
| 2 | 110 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 111 | | string _invocationId = null; |
| 2 | 112 | | if (_shouldTrace) |
| | 113 | | { |
| 0 | 114 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 115 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 116 | | tracingParameters.Add("eventId", eventId); |
| 0 | 117 | | tracingParameters.Add("reward", reward); |
| 0 | 118 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 119 | | ServiceClientTracing.Enter(_invocationId, this, "Reward", tracingParameters); |
| | 120 | | } |
| | 121 | | // Construct URL |
| 2 | 122 | | var _baseUrl = Client.BaseUri; |
| 2 | 123 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "events/{eventId}/reward"; |
| 2 | 124 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 2 | 125 | | _url = _url.Replace("{eventId}", System.Uri.EscapeDataString(eventId)); |
| | 126 | | // Create HTTP transport objects |
| 2 | 127 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 128 | | HttpResponseMessage _httpResponse = null; |
| 2 | 129 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 130 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 131 | | // Set Headers |
| | 132 | |
|
| | 133 | |
|
| 2 | 134 | | if (customHeaders != null) |
| | 135 | | { |
| 0 | 136 | | foreach(var _header in customHeaders) |
| | 137 | | { |
| 0 | 138 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 139 | | { |
| 0 | 140 | | _httpRequest.Headers.Remove(_header.Key); |
| | 141 | | } |
| 0 | 142 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 143 | | } |
| | 144 | | } |
| | 145 | |
|
| | 146 | | // Serialize Request |
| 2 | 147 | | string _requestContent = null; |
| 2 | 148 | | if(reward != null) |
| | 149 | | { |
| 2 | 150 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(reward, Client.SerializationSetting |
| 2 | 151 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 2 | 152 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 153 | | } |
| | 154 | | // Set Credentials |
| 2 | 155 | | if (Client.Credentials != null) |
| | 156 | | { |
| 2 | 157 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 158 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 159 | | } |
| | 160 | | // Send Request |
| 2 | 161 | | if (_shouldTrace) |
| | 162 | | { |
| 0 | 163 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 164 | | } |
| 2 | 165 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 166 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 167 | | if (_shouldTrace) |
| | 168 | | { |
| 0 | 169 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 170 | | } |
| 2 | 171 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 172 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 173 | | string _responseContent = null; |
| 2 | 174 | | if ((int)_statusCode != 204) |
| | 175 | | { |
| 0 | 176 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 177 | | try |
| | 178 | | { |
| 0 | 179 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 180 | | ErrorResponse _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res |
| 0 | 181 | | if (_errorBody != null) |
| | 182 | | { |
| 0 | 183 | | ex.Body = _errorBody; |
| | 184 | | } |
| 0 | 185 | | } |
| 0 | 186 | | catch (JsonException) |
| | 187 | | { |
| | 188 | | // Ignore the exception |
| 0 | 189 | | } |
| 0 | 190 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 191 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 192 | | if (_shouldTrace) |
| | 193 | | { |
| 0 | 194 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 195 | | } |
| 0 | 196 | | _httpRequest.Dispose(); |
| 0 | 197 | | if (_httpResponse != null) |
| | 198 | | { |
| 0 | 199 | | _httpResponse.Dispose(); |
| | 200 | | } |
| 0 | 201 | | throw ex; |
| | 202 | | } |
| | 203 | | // Create Result |
| 2 | 204 | | var _result = new HttpOperationResponse(); |
| 2 | 205 | | _result.Request = _httpRequest; |
| 2 | 206 | | _result.Response = _httpResponse; |
| 2 | 207 | | if (_shouldTrace) |
| | 208 | | { |
| 0 | 209 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 210 | | } |
| 2 | 211 | | return _result; |
| 2 | 212 | | } |
| | 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 | | { |
| 0 | 244 | | if (Client.Endpoint == null) |
| | 245 | | { |
| 0 | 246 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 247 | | } |
| 0 | 248 | | if (eventId == null) |
| | 249 | | { |
| 0 | 250 | | throw new ValidationException(ValidationRules.CannotBeNull, "eventId"); |
| | 251 | | } |
| 0 | 252 | | if (eventId != null) |
| | 253 | | { |
| 0 | 254 | | if (eventId.Length > 256) |
| | 255 | | { |
| 0 | 256 | | throw new ValidationException(ValidationRules.MaxLength, "eventId", 256); |
| | 257 | | } |
| | 258 | | } |
| | 259 | | // Tracing |
| 0 | 260 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 261 | | string _invocationId = null; |
| 0 | 262 | | if (_shouldTrace) |
| | 263 | | { |
| 0 | 264 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 265 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 266 | | tracingParameters.Add("eventId", eventId); |
| 0 | 267 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 268 | | ServiceClientTracing.Enter(_invocationId, this, "Activate", tracingParameters); |
| | 269 | | } |
| | 270 | | // Construct URL |
| 0 | 271 | | var _baseUrl = Client.BaseUri; |
| 0 | 272 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "events/{eventId}/activate"; |
| 0 | 273 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 0 | 274 | | _url = _url.Replace("{eventId}", System.Uri.EscapeDataString(eventId)); |
| | 275 | | // Create HTTP transport objects |
| 0 | 276 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 277 | | HttpResponseMessage _httpResponse = null; |
| 0 | 278 | | _httpRequest.Method = new HttpMethod("POST"); |
| 0 | 279 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 280 | | // Set Headers |
| | 281 | |
|
| | 282 | |
|
| 0 | 283 | | if (customHeaders != null) |
| | 284 | | { |
| 0 | 285 | | foreach(var _header in customHeaders) |
| | 286 | | { |
| 0 | 287 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 288 | | { |
| 0 | 289 | | _httpRequest.Headers.Remove(_header.Key); |
| | 290 | | } |
| 0 | 291 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 292 | | } |
| | 293 | | } |
| | 294 | |
|
| | 295 | | // Serialize Request |
| 0 | 296 | | string _requestContent = null; |
| | 297 | | // Set Credentials |
| 0 | 298 | | if (Client.Credentials != null) |
| | 299 | | { |
| 0 | 300 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 301 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 302 | | } |
| | 303 | | // Send Request |
| 0 | 304 | | if (_shouldTrace) |
| | 305 | | { |
| 0 | 306 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 307 | | } |
| 0 | 308 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 309 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 310 | | if (_shouldTrace) |
| | 311 | | { |
| 0 | 312 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 313 | | } |
| 0 | 314 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 315 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 316 | | string _responseContent = null; |
| 0 | 317 | | if ((int)_statusCode != 204) |
| | 318 | | { |
| 0 | 319 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 320 | | try |
| | 321 | | { |
| 0 | 322 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 323 | | ErrorResponse _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res |
| 0 | 324 | | if (_errorBody != null) |
| | 325 | | { |
| 0 | 326 | | ex.Body = _errorBody; |
| | 327 | | } |
| 0 | 328 | | } |
| 0 | 329 | | catch (JsonException) |
| | 330 | | { |
| | 331 | | // Ignore the exception |
| 0 | 332 | | } |
| 0 | 333 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 334 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 335 | | if (_shouldTrace) |
| | 336 | | { |
| 0 | 337 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 338 | | } |
| 0 | 339 | | _httpRequest.Dispose(); |
| 0 | 340 | | if (_httpResponse != null) |
| | 341 | | { |
| 0 | 342 | | _httpResponse.Dispose(); |
| | 343 | | } |
| 0 | 344 | | throw ex; |
| | 345 | | } |
| | 346 | | // Create Result |
| 0 | 347 | | var _result = new HttpOperationResponse(); |
| 0 | 348 | | _result.Request = _httpRequest; |
| 0 | 349 | | _result.Response = _httpResponse; |
| 0 | 350 | | if (_shouldTrace) |
| | 351 | | { |
| 0 | 352 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 353 | | } |
| 0 | 354 | | return _result; |
| 0 | 355 | | } |
| | 356 | |
|
| | 357 | | } |
| | 358 | | } |