BotState.cs
1 // Code generated by Microsoft (R) AutoRest Code Generator 0.16.0.0
2 // Changes may cause incorrect behavior and will be lost if the code is
3 // regenerated.
4 
5 namespace Microsoft.Bot.Connector
6 {
7  using System;
8  using System.Linq;
9  using System.Collections.Generic;
10  using System.Net;
11  using System.Net.Http;
12  using System.Net.Http.Headers;
13  using System.Text;
14  using System.Text.RegularExpressions;
15  using System.Threading;
16  using System.Threading.Tasks;
17  using Microsoft.Rest;
18  using Microsoft.Rest.Serialization;
19  using Newtonsoft.Json;
20 
21 
25  public partial class BotState : IServiceOperations<StateClient>, IBotState
26  {
33  public BotState(StateClient client)
34  {
35  if (client == null)
36  {
37  throw new ArgumentNullException("client");
38  }
39  this.Client = client;
40  }
41 
45  public StateClient Client { get; private set; }
46 
66  public async Task<HttpOperationResponse<object>> GetUserDataWithHttpMessagesAsync(string channelId, string userId, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
67  {
68  if (channelId == null)
69  {
70  throw new ValidationException(ValidationRules.CannotBeNull, "channelId");
71  }
72  if (userId == null)
73  {
74  throw new ValidationException(ValidationRules.CannotBeNull, "userId");
75  }
76  // Tracing
77  bool _shouldTrace = ServiceClientTracing.IsEnabled;
78  string _invocationId = null;
79  if (_shouldTrace)
80  {
81  _invocationId = ServiceClientTracing.NextInvocationId.ToString();
82  Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
83  tracingParameters.Add("channelId", channelId);
84  tracingParameters.Add("userId", userId);
85  tracingParameters.Add("cancellationToken", cancellationToken);
86  ServiceClientTracing.Enter(_invocationId, this, "GetUserData", tracingParameters);
87  }
88  // Construct URL
89  var _baseUrl = this.Client.BaseUri.AbsoluteUri;
90  var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "v3/botstate/{channelId}/users/{userId}").ToString();
91  _url = _url.Replace("{channelId}", Uri.EscapeDataString(channelId));
92  _url = _url.Replace("{userId}", Uri.EscapeDataString(userId));
93  // Create HTTP transport objects
94  HttpRequestMessage _httpRequest = new HttpRequestMessage();
95  HttpResponseMessage _httpResponse = null;
96  _httpRequest.Method = new HttpMethod("GET");
97  _httpRequest.RequestUri = new Uri(_url);
98  // Set Headers
99  if (customHeaders != null)
100  {
101  foreach(var _header in customHeaders)
102  {
103  if (_httpRequest.Headers.Contains(_header.Key))
104  {
105  _httpRequest.Headers.Remove(_header.Key);
106  }
107  _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
108  }
109  }
110 
111  // Serialize Request
112  string _requestContent = null;
113  // Set Credentials
114  if (this.Client.Credentials != null)
115  {
116  cancellationToken.ThrowIfCancellationRequested();
117  await this.Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
118  }
119  // Send Request
120  if (_shouldTrace)
121  {
122  ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
123  }
124  cancellationToken.ThrowIfCancellationRequested();
125  _httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
126  if (_shouldTrace)
127  {
128  ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
129  }
130  HttpStatusCode _statusCode = _httpResponse.StatusCode;
131  cancellationToken.ThrowIfCancellationRequested();
132  string _responseContent = null;
133  if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 401 && (int)_statusCode != 403 && (int)_statusCode != 404 && (int)_statusCode != 500 && (int)_statusCode != 503)
134  {
135  var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
136  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
137  ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
138  ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
139  if (_shouldTrace)
140  {
141  ServiceClientTracing.Error(_invocationId, ex);
142  }
143  _httpRequest.Dispose();
144  if (_httpResponse != null)
145  {
146  _httpResponse.Dispose();
147  }
148  throw ex;
149  }
150  // Create Result
151  var _result = new HttpOperationResponse<object>();
152  _result.Request = _httpRequest;
153  _result.Response = _httpResponse;
154  // Deserialize Response
155  if ((int)_statusCode == 200)
156  {
157  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
158  try
159  {
160  _result.Body = SafeJsonConvert.DeserializeObject<BotData>(_responseContent, this.Client.DeserializationSettings);
161  }
162  catch (JsonException ex)
163  {
164  _httpRequest.Dispose();
165  if (_httpResponse != null)
166  {
167  _httpResponse.Dispose();
168  }
169  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
170  }
171  }
172  // Deserialize Response
173  if ((int)_statusCode == 400)
174  {
175  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
176  try
177  {
178  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
179  }
180  catch (JsonException ex)
181  {
182  _httpRequest.Dispose();
183  if (_httpResponse != null)
184  {
185  _httpResponse.Dispose();
186  }
187  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
188  }
189  }
190  // Deserialize Response
191  if ((int)_statusCode == 404)
192  {
193  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
194  try
195  {
196  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
197  }
198  catch (JsonException ex)
199  {
200  _httpRequest.Dispose();
201  if (_httpResponse != null)
202  {
203  _httpResponse.Dispose();
204  }
205  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
206  }
207  }
208  // Deserialize Response
209  if ((int)_statusCode == 500)
210  {
211  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
212  try
213  {
214  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
215  }
216  catch (JsonException ex)
217  {
218  _httpRequest.Dispose();
219  if (_httpResponse != null)
220  {
221  _httpResponse.Dispose();
222  }
223  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
224  }
225  }
226  // Deserialize Response
227  if ((int)_statusCode == 503)
228  {
229  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
230  try
231  {
232  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
233  }
234  catch (JsonException ex)
235  {
236  _httpRequest.Dispose();
237  if (_httpResponse != null)
238  {
239  _httpResponse.Dispose();
240  }
241  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
242  }
243  }
244  if (_shouldTrace)
245  {
246  ServiceClientTracing.Exit(_invocationId, _result);
247  }
248  return _result;
249  }
250 
273  public async Task<HttpOperationResponse<object>> SetUserDataWithHttpMessagesAsync(string channelId, string userId, BotData botData, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
274  {
275  if (channelId == null)
276  {
277  throw new ValidationException(ValidationRules.CannotBeNull, "channelId");
278  }
279  if (userId == null)
280  {
281  throw new ValidationException(ValidationRules.CannotBeNull, "userId");
282  }
283  if (botData == null)
284  {
285  throw new ValidationException(ValidationRules.CannotBeNull, "botData");
286  }
287  // Tracing
288  bool _shouldTrace = ServiceClientTracing.IsEnabled;
289  string _invocationId = null;
290  if (_shouldTrace)
291  {
292  _invocationId = ServiceClientTracing.NextInvocationId.ToString();
293  Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
294  tracingParameters.Add("channelId", channelId);
295  tracingParameters.Add("userId", userId);
296  tracingParameters.Add("botData", botData);
297  tracingParameters.Add("cancellationToken", cancellationToken);
298  ServiceClientTracing.Enter(_invocationId, this, "SetUserData", tracingParameters);
299  }
300  // Construct URL
301  var _baseUrl = this.Client.BaseUri.AbsoluteUri;
302  var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "v3/botstate/{channelId}/users/{userId}").ToString();
303  _url = _url.Replace("{channelId}", Uri.EscapeDataString(channelId));
304  _url = _url.Replace("{userId}", Uri.EscapeDataString(userId));
305  // Create HTTP transport objects
306  HttpRequestMessage _httpRequest = new HttpRequestMessage();
307  HttpResponseMessage _httpResponse = null;
308  _httpRequest.Method = new HttpMethod("POST");
309  _httpRequest.RequestUri = new Uri(_url);
310  // Set Headers
311  if (customHeaders != null)
312  {
313  foreach(var _header in customHeaders)
314  {
315  if (_httpRequest.Headers.Contains(_header.Key))
316  {
317  _httpRequest.Headers.Remove(_header.Key);
318  }
319  _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
320  }
321  }
322 
323  // Serialize Request
324  string _requestContent = null;
325  if(botData != null)
326  {
327  _requestContent = SafeJsonConvert.SerializeObject(botData, this.Client.SerializationSettings);
328  _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8);
329  _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
330  }
331  // Set Credentials
332  if (this.Client.Credentials != null)
333  {
334  cancellationToken.ThrowIfCancellationRequested();
335  await this.Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
336  }
337  // Send Request
338  if (_shouldTrace)
339  {
340  ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
341  }
342  cancellationToken.ThrowIfCancellationRequested();
343  _httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
344  if (_shouldTrace)
345  {
346  ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
347  }
348  HttpStatusCode _statusCode = _httpResponse.StatusCode;
349  cancellationToken.ThrowIfCancellationRequested();
350  string _responseContent = null;
351  if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 401 && (int)_statusCode != 403 && (int)_statusCode != 404 && (int)_statusCode != 412 && (int)_statusCode != 500 && (int)_statusCode != 503)
352  {
353  var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
354  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
355  ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
356  ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
357  if (_shouldTrace)
358  {
359  ServiceClientTracing.Error(_invocationId, ex);
360  }
361  _httpRequest.Dispose();
362  if (_httpResponse != null)
363  {
364  _httpResponse.Dispose();
365  }
366  throw ex;
367  }
368  // Create Result
369  var _result = new HttpOperationResponse<object>();
370  _result.Request = _httpRequest;
371  _result.Response = _httpResponse;
372  // Deserialize Response
373  if ((int)_statusCode == 200)
374  {
375  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
376  try
377  {
378  _result.Body = SafeJsonConvert.DeserializeObject<BotData>(_responseContent, this.Client.DeserializationSettings);
379  }
380  catch (JsonException ex)
381  {
382  _httpRequest.Dispose();
383  if (_httpResponse != null)
384  {
385  _httpResponse.Dispose();
386  }
387  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
388  }
389  }
390  // Deserialize Response
391  if ((int)_statusCode == 400)
392  {
393  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
394  try
395  {
396  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
397  }
398  catch (JsonException ex)
399  {
400  _httpRequest.Dispose();
401  if (_httpResponse != null)
402  {
403  _httpResponse.Dispose();
404  }
405  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
406  }
407  }
408  // Deserialize Response
409  if ((int)_statusCode == 404)
410  {
411  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
412  try
413  {
414  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
415  }
416  catch (JsonException ex)
417  {
418  _httpRequest.Dispose();
419  if (_httpResponse != null)
420  {
421  _httpResponse.Dispose();
422  }
423  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
424  }
425  }
426  // Deserialize Response
427  if ((int)_statusCode == 412)
428  {
429  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
430  try
431  {
432  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
433  }
434  catch (JsonException ex)
435  {
436  _httpRequest.Dispose();
437  if (_httpResponse != null)
438  {
439  _httpResponse.Dispose();
440  }
441  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
442  }
443  }
444  // Deserialize Response
445  if ((int)_statusCode == 500)
446  {
447  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
448  try
449  {
450  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
451  }
452  catch (JsonException ex)
453  {
454  _httpRequest.Dispose();
455  if (_httpResponse != null)
456  {
457  _httpResponse.Dispose();
458  }
459  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
460  }
461  }
462  // Deserialize Response
463  if ((int)_statusCode == 503)
464  {
465  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
466  try
467  {
468  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
469  }
470  catch (JsonException ex)
471  {
472  _httpRequest.Dispose();
473  if (_httpResponse != null)
474  {
475  _httpResponse.Dispose();
476  }
477  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
478  }
479  }
480  if (_shouldTrace)
481  {
482  ServiceClientTracing.Exit(_invocationId, _result);
483  }
484  return _result;
485  }
486 
507  public async Task<HttpOperationResponse<object>> DeleteStateForUserWithHttpMessagesAsync(string channelId, string userId, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
508  {
509  if (channelId == null)
510  {
511  throw new ValidationException(ValidationRules.CannotBeNull, "channelId");
512  }
513  if (userId == null)
514  {
515  throw new ValidationException(ValidationRules.CannotBeNull, "userId");
516  }
517  // Tracing
518  bool _shouldTrace = ServiceClientTracing.IsEnabled;
519  string _invocationId = null;
520  if (_shouldTrace)
521  {
522  _invocationId = ServiceClientTracing.NextInvocationId.ToString();
523  Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
524  tracingParameters.Add("channelId", channelId);
525  tracingParameters.Add("userId", userId);
526  tracingParameters.Add("cancellationToken", cancellationToken);
527  ServiceClientTracing.Enter(_invocationId, this, "DeleteStateForUser", tracingParameters);
528  }
529  // Construct URL
530  var _baseUrl = this.Client.BaseUri.AbsoluteUri;
531  var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "v3/botstate/{channelId}/users/{userId}").ToString();
532  _url = _url.Replace("{channelId}", Uri.EscapeDataString(channelId));
533  _url = _url.Replace("{userId}", Uri.EscapeDataString(userId));
534  // Create HTTP transport objects
535  HttpRequestMessage _httpRequest = new HttpRequestMessage();
536  HttpResponseMessage _httpResponse = null;
537  _httpRequest.Method = new HttpMethod("DELETE");
538  _httpRequest.RequestUri = new Uri(_url);
539  // Set Headers
540  if (customHeaders != null)
541  {
542  foreach(var _header in customHeaders)
543  {
544  if (_httpRequest.Headers.Contains(_header.Key))
545  {
546  _httpRequest.Headers.Remove(_header.Key);
547  }
548  _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
549  }
550  }
551 
552  // Serialize Request
553  string _requestContent = null;
554  // Set Credentials
555  if (this.Client.Credentials != null)
556  {
557  cancellationToken.ThrowIfCancellationRequested();
558  await this.Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
559  }
560  // Send Request
561  if (_shouldTrace)
562  {
563  ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
564  }
565  cancellationToken.ThrowIfCancellationRequested();
566  _httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
567  if (_shouldTrace)
568  {
569  ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
570  }
571  HttpStatusCode _statusCode = _httpResponse.StatusCode;
572  cancellationToken.ThrowIfCancellationRequested();
573  string _responseContent = null;
574  if ((int)_statusCode != 200 && (int)_statusCode != 204 && (int)_statusCode != 400 && (int)_statusCode != 401 && (int)_statusCode != 403 && (int)_statusCode != 404 && (int)_statusCode != 412 && (int)_statusCode != 500 && (int)_statusCode != 503)
575  {
576  var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
577  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
578  ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
579  ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
580  if (_shouldTrace)
581  {
582  ServiceClientTracing.Error(_invocationId, ex);
583  }
584  _httpRequest.Dispose();
585  if (_httpResponse != null)
586  {
587  _httpResponse.Dispose();
588  }
589  throw ex;
590  }
591  // Create Result
592  var _result = new HttpOperationResponse<object>();
593  _result.Request = _httpRequest;
594  _result.Response = _httpResponse;
595  // Deserialize Response
596  if ((int)_statusCode == 200)
597  {
598  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
599  try
600  {
601  _result.Body = SafeJsonConvert.DeserializeObject<IList<string>>(_responseContent, this.Client.DeserializationSettings);
602  }
603  catch (JsonException ex)
604  {
605  _httpRequest.Dispose();
606  if (_httpResponse != null)
607  {
608  _httpResponse.Dispose();
609  }
610  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
611  }
612  }
613  // Deserialize Response
614  if ((int)_statusCode == 400)
615  {
616  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
617  try
618  {
619  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
620  }
621  catch (JsonException ex)
622  {
623  _httpRequest.Dispose();
624  if (_httpResponse != null)
625  {
626  _httpResponse.Dispose();
627  }
628  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
629  }
630  }
631  // Deserialize Response
632  if ((int)_statusCode == 404)
633  {
634  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
635  try
636  {
637  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
638  }
639  catch (JsonException ex)
640  {
641  _httpRequest.Dispose();
642  if (_httpResponse != null)
643  {
644  _httpResponse.Dispose();
645  }
646  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
647  }
648  }
649  // Deserialize Response
650  if ((int)_statusCode == 412)
651  {
652  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
653  try
654  {
655  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
656  }
657  catch (JsonException ex)
658  {
659  _httpRequest.Dispose();
660  if (_httpResponse != null)
661  {
662  _httpResponse.Dispose();
663  }
664  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
665  }
666  }
667  // Deserialize Response
668  if ((int)_statusCode == 500)
669  {
670  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
671  try
672  {
673  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
674  }
675  catch (JsonException ex)
676  {
677  _httpRequest.Dispose();
678  if (_httpResponse != null)
679  {
680  _httpResponse.Dispose();
681  }
682  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
683  }
684  }
685  // Deserialize Response
686  if ((int)_statusCode == 503)
687  {
688  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
689  try
690  {
691  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
692  }
693  catch (JsonException ex)
694  {
695  _httpRequest.Dispose();
696  if (_httpResponse != null)
697  {
698  _httpResponse.Dispose();
699  }
700  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
701  }
702  }
703  if (_shouldTrace)
704  {
705  ServiceClientTracing.Exit(_invocationId, _result);
706  }
707  return _result;
708  }
709 
729  public async Task<HttpOperationResponse<object>> GetConversationDataWithHttpMessagesAsync(string channelId, string conversationId, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
730  {
731  if (channelId == null)
732  {
733  throw new ValidationException(ValidationRules.CannotBeNull, "channelId");
734  }
735  if (conversationId == null)
736  {
737  throw new ValidationException(ValidationRules.CannotBeNull, "conversationId");
738  }
739  // Tracing
740  bool _shouldTrace = ServiceClientTracing.IsEnabled;
741  string _invocationId = null;
742  if (_shouldTrace)
743  {
744  _invocationId = ServiceClientTracing.NextInvocationId.ToString();
745  Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
746  tracingParameters.Add("channelId", channelId);
747  tracingParameters.Add("conversationId", conversationId);
748  tracingParameters.Add("cancellationToken", cancellationToken);
749  ServiceClientTracing.Enter(_invocationId, this, "GetConversationData", tracingParameters);
750  }
751  // Construct URL
752  var _baseUrl = this.Client.BaseUri.AbsoluteUri;
753  var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "v3/botstate/{channelId}/conversations/{conversationId}").ToString();
754  _url = _url.Replace("{channelId}", Uri.EscapeDataString(channelId));
755  _url = _url.Replace("{conversationId}", Uri.EscapeDataString(conversationId));
756  // Create HTTP transport objects
757  HttpRequestMessage _httpRequest = new HttpRequestMessage();
758  HttpResponseMessage _httpResponse = null;
759  _httpRequest.Method = new HttpMethod("GET");
760  _httpRequest.RequestUri = new Uri(_url);
761  // Set Headers
762  if (customHeaders != null)
763  {
764  foreach(var _header in customHeaders)
765  {
766  if (_httpRequest.Headers.Contains(_header.Key))
767  {
768  _httpRequest.Headers.Remove(_header.Key);
769  }
770  _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
771  }
772  }
773 
774  // Serialize Request
775  string _requestContent = null;
776  // Set Credentials
777  if (this.Client.Credentials != null)
778  {
779  cancellationToken.ThrowIfCancellationRequested();
780  await this.Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
781  }
782  // Send Request
783  if (_shouldTrace)
784  {
785  ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
786  }
787  cancellationToken.ThrowIfCancellationRequested();
788  _httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
789  if (_shouldTrace)
790  {
791  ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
792  }
793  HttpStatusCode _statusCode = _httpResponse.StatusCode;
794  cancellationToken.ThrowIfCancellationRequested();
795  string _responseContent = null;
796  if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 401 && (int)_statusCode != 403 && (int)_statusCode != 404 && (int)_statusCode != 500 && (int)_statusCode != 503)
797  {
798  var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
799  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
800  ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
801  ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
802  if (_shouldTrace)
803  {
804  ServiceClientTracing.Error(_invocationId, ex);
805  }
806  _httpRequest.Dispose();
807  if (_httpResponse != null)
808  {
809  _httpResponse.Dispose();
810  }
811  throw ex;
812  }
813  // Create Result
814  var _result = new HttpOperationResponse<object>();
815  _result.Request = _httpRequest;
816  _result.Response = _httpResponse;
817  // Deserialize Response
818  if ((int)_statusCode == 200)
819  {
820  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
821  try
822  {
823  _result.Body = SafeJsonConvert.DeserializeObject<BotData>(_responseContent, this.Client.DeserializationSettings);
824  }
825  catch (JsonException ex)
826  {
827  _httpRequest.Dispose();
828  if (_httpResponse != null)
829  {
830  _httpResponse.Dispose();
831  }
832  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
833  }
834  }
835  // Deserialize Response
836  if ((int)_statusCode == 400)
837  {
838  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
839  try
840  {
841  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
842  }
843  catch (JsonException ex)
844  {
845  _httpRequest.Dispose();
846  if (_httpResponse != null)
847  {
848  _httpResponse.Dispose();
849  }
850  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
851  }
852  }
853  // Deserialize Response
854  if ((int)_statusCode == 404)
855  {
856  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
857  try
858  {
859  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
860  }
861  catch (JsonException ex)
862  {
863  _httpRequest.Dispose();
864  if (_httpResponse != null)
865  {
866  _httpResponse.Dispose();
867  }
868  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
869  }
870  }
871  // Deserialize Response
872  if ((int)_statusCode == 500)
873  {
874  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
875  try
876  {
877  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
878  }
879  catch (JsonException ex)
880  {
881  _httpRequest.Dispose();
882  if (_httpResponse != null)
883  {
884  _httpResponse.Dispose();
885  }
886  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
887  }
888  }
889  // Deserialize Response
890  if ((int)_statusCode == 503)
891  {
892  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
893  try
894  {
895  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
896  }
897  catch (JsonException ex)
898  {
899  _httpRequest.Dispose();
900  if (_httpResponse != null)
901  {
902  _httpResponse.Dispose();
903  }
904  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
905  }
906  }
907  if (_shouldTrace)
908  {
909  ServiceClientTracing.Exit(_invocationId, _result);
910  }
911  return _result;
912  }
913 
936  public async Task<HttpOperationResponse<object>> SetConversationDataWithHttpMessagesAsync(string channelId, string conversationId, BotData botData, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
937  {
938  if (channelId == null)
939  {
940  throw new ValidationException(ValidationRules.CannotBeNull, "channelId");
941  }
942  if (conversationId == null)
943  {
944  throw new ValidationException(ValidationRules.CannotBeNull, "conversationId");
945  }
946  if (botData == null)
947  {
948  throw new ValidationException(ValidationRules.CannotBeNull, "botData");
949  }
950  // Tracing
951  bool _shouldTrace = ServiceClientTracing.IsEnabled;
952  string _invocationId = null;
953  if (_shouldTrace)
954  {
955  _invocationId = ServiceClientTracing.NextInvocationId.ToString();
956  Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
957  tracingParameters.Add("channelId", channelId);
958  tracingParameters.Add("conversationId", conversationId);
959  tracingParameters.Add("botData", botData);
960  tracingParameters.Add("cancellationToken", cancellationToken);
961  ServiceClientTracing.Enter(_invocationId, this, "SetConversationData", tracingParameters);
962  }
963  // Construct URL
964  var _baseUrl = this.Client.BaseUri.AbsoluteUri;
965  var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "v3/botstate/{channelId}/conversations/{conversationId}").ToString();
966  _url = _url.Replace("{channelId}", Uri.EscapeDataString(channelId));
967  _url = _url.Replace("{conversationId}", Uri.EscapeDataString(conversationId));
968  // Create HTTP transport objects
969  HttpRequestMessage _httpRequest = new HttpRequestMessage();
970  HttpResponseMessage _httpResponse = null;
971  _httpRequest.Method = new HttpMethod("POST");
972  _httpRequest.RequestUri = new Uri(_url);
973  // Set Headers
974  if (customHeaders != null)
975  {
976  foreach(var _header in customHeaders)
977  {
978  if (_httpRequest.Headers.Contains(_header.Key))
979  {
980  _httpRequest.Headers.Remove(_header.Key);
981  }
982  _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
983  }
984  }
985 
986  // Serialize Request
987  string _requestContent = null;
988  if(botData != null)
989  {
990  _requestContent = SafeJsonConvert.SerializeObject(botData, this.Client.SerializationSettings);
991  _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8);
992  _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
993  }
994  // Set Credentials
995  if (this.Client.Credentials != null)
996  {
997  cancellationToken.ThrowIfCancellationRequested();
998  await this.Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
999  }
1000  // Send Request
1001  if (_shouldTrace)
1002  {
1003  ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
1004  }
1005  cancellationToken.ThrowIfCancellationRequested();
1006  _httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
1007  if (_shouldTrace)
1008  {
1009  ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
1010  }
1011  HttpStatusCode _statusCode = _httpResponse.StatusCode;
1012  cancellationToken.ThrowIfCancellationRequested();
1013  string _responseContent = null;
1014  if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 401 && (int)_statusCode != 403 && (int)_statusCode != 404 && (int)_statusCode != 412 && (int)_statusCode != 500 && (int)_statusCode != 503)
1015  {
1016  var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
1017  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1018  ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
1019  ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
1020  if (_shouldTrace)
1021  {
1022  ServiceClientTracing.Error(_invocationId, ex);
1023  }
1024  _httpRequest.Dispose();
1025  if (_httpResponse != null)
1026  {
1027  _httpResponse.Dispose();
1028  }
1029  throw ex;
1030  }
1031  // Create Result
1032  var _result = new HttpOperationResponse<object>();
1033  _result.Request = _httpRequest;
1034  _result.Response = _httpResponse;
1035  // Deserialize Response
1036  if ((int)_statusCode == 200)
1037  {
1038  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1039  try
1040  {
1041  _result.Body = SafeJsonConvert.DeserializeObject<BotData>(_responseContent, this.Client.DeserializationSettings);
1042  }
1043  catch (JsonException ex)
1044  {
1045  _httpRequest.Dispose();
1046  if (_httpResponse != null)
1047  {
1048  _httpResponse.Dispose();
1049  }
1050  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1051  }
1052  }
1053  // Deserialize Response
1054  if ((int)_statusCode == 400)
1055  {
1056  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1057  try
1058  {
1059  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
1060  }
1061  catch (JsonException ex)
1062  {
1063  _httpRequest.Dispose();
1064  if (_httpResponse != null)
1065  {
1066  _httpResponse.Dispose();
1067  }
1068  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1069  }
1070  }
1071  // Deserialize Response
1072  if ((int)_statusCode == 404)
1073  {
1074  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1075  try
1076  {
1077  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
1078  }
1079  catch (JsonException ex)
1080  {
1081  _httpRequest.Dispose();
1082  if (_httpResponse != null)
1083  {
1084  _httpResponse.Dispose();
1085  }
1086  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1087  }
1088  }
1089  // Deserialize Response
1090  if ((int)_statusCode == 412)
1091  {
1092  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1093  try
1094  {
1095  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
1096  }
1097  catch (JsonException ex)
1098  {
1099  _httpRequest.Dispose();
1100  if (_httpResponse != null)
1101  {
1102  _httpResponse.Dispose();
1103  }
1104  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1105  }
1106  }
1107  // Deserialize Response
1108  if ((int)_statusCode == 500)
1109  {
1110  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1111  try
1112  {
1113  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
1114  }
1115  catch (JsonException ex)
1116  {
1117  _httpRequest.Dispose();
1118  if (_httpResponse != null)
1119  {
1120  _httpResponse.Dispose();
1121  }
1122  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1123  }
1124  }
1125  // Deserialize Response
1126  if ((int)_statusCode == 503)
1127  {
1128  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1129  try
1130  {
1131  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
1132  }
1133  catch (JsonException ex)
1134  {
1135  _httpRequest.Dispose();
1136  if (_httpResponse != null)
1137  {
1138  _httpResponse.Dispose();
1139  }
1140  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1141  }
1142  }
1143  if (_shouldTrace)
1144  {
1145  ServiceClientTracing.Exit(_invocationId, _result);
1146  }
1147  return _result;
1148  }
1149 
1172  public async Task<HttpOperationResponse<object>> GetPrivateConversationDataWithHttpMessagesAsync(string channelId, string conversationId, string userId, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
1173  {
1174  if (channelId == null)
1175  {
1176  throw new ValidationException(ValidationRules.CannotBeNull, "channelId");
1177  }
1178  if (conversationId == null)
1179  {
1180  throw new ValidationException(ValidationRules.CannotBeNull, "conversationId");
1181  }
1182  if (userId == null)
1183  {
1184  throw new ValidationException(ValidationRules.CannotBeNull, "userId");
1185  }
1186  // Tracing
1187  bool _shouldTrace = ServiceClientTracing.IsEnabled;
1188  string _invocationId = null;
1189  if (_shouldTrace)
1190  {
1191  _invocationId = ServiceClientTracing.NextInvocationId.ToString();
1192  Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
1193  tracingParameters.Add("channelId", channelId);
1194  tracingParameters.Add("conversationId", conversationId);
1195  tracingParameters.Add("userId", userId);
1196  tracingParameters.Add("cancellationToken", cancellationToken);
1197  ServiceClientTracing.Enter(_invocationId, this, "GetPrivateConversationData", tracingParameters);
1198  }
1199  // Construct URL
1200  var _baseUrl = this.Client.BaseUri.AbsoluteUri;
1201  var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "v3/botstate/{channelId}/conversations/{conversationId}/users/{userId}").ToString();
1202  _url = _url.Replace("{channelId}", Uri.EscapeDataString(channelId));
1203  _url = _url.Replace("{conversationId}", Uri.EscapeDataString(conversationId));
1204  _url = _url.Replace("{userId}", Uri.EscapeDataString(userId));
1205  // Create HTTP transport objects
1206  HttpRequestMessage _httpRequest = new HttpRequestMessage();
1207  HttpResponseMessage _httpResponse = null;
1208  _httpRequest.Method = new HttpMethod("GET");
1209  _httpRequest.RequestUri = new Uri(_url);
1210  // Set Headers
1211  if (customHeaders != null)
1212  {
1213  foreach(var _header in customHeaders)
1214  {
1215  if (_httpRequest.Headers.Contains(_header.Key))
1216  {
1217  _httpRequest.Headers.Remove(_header.Key);
1218  }
1219  _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
1220  }
1221  }
1222 
1223  // Serialize Request
1224  string _requestContent = null;
1225  // Set Credentials
1226  if (this.Client.Credentials != null)
1227  {
1228  cancellationToken.ThrowIfCancellationRequested();
1229  await this.Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
1230  }
1231  // Send Request
1232  if (_shouldTrace)
1233  {
1234  ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
1235  }
1236  cancellationToken.ThrowIfCancellationRequested();
1237  _httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
1238  if (_shouldTrace)
1239  {
1240  ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
1241  }
1242  HttpStatusCode _statusCode = _httpResponse.StatusCode;
1243  cancellationToken.ThrowIfCancellationRequested();
1244  string _responseContent = null;
1245  if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 401 && (int)_statusCode != 403 && (int)_statusCode != 404 && (int)_statusCode != 500 && (int)_statusCode != 503)
1246  {
1247  var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
1248  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1249  ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
1250  ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
1251  if (_shouldTrace)
1252  {
1253  ServiceClientTracing.Error(_invocationId, ex);
1254  }
1255  _httpRequest.Dispose();
1256  if (_httpResponse != null)
1257  {
1258  _httpResponse.Dispose();
1259  }
1260  throw ex;
1261  }
1262  // Create Result
1263  var _result = new HttpOperationResponse<object>();
1264  _result.Request = _httpRequest;
1265  _result.Response = _httpResponse;
1266  // Deserialize Response
1267  if ((int)_statusCode == 200)
1268  {
1269  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1270  try
1271  {
1272  _result.Body = SafeJsonConvert.DeserializeObject<BotData>(_responseContent, this.Client.DeserializationSettings);
1273  }
1274  catch (JsonException ex)
1275  {
1276  _httpRequest.Dispose();
1277  if (_httpResponse != null)
1278  {
1279  _httpResponse.Dispose();
1280  }
1281  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1282  }
1283  }
1284  // Deserialize Response
1285  if ((int)_statusCode == 400)
1286  {
1287  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1288  try
1289  {
1290  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
1291  }
1292  catch (JsonException ex)
1293  {
1294  _httpRequest.Dispose();
1295  if (_httpResponse != null)
1296  {
1297  _httpResponse.Dispose();
1298  }
1299  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1300  }
1301  }
1302  // Deserialize Response
1303  if ((int)_statusCode == 404)
1304  {
1305  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1306  try
1307  {
1308  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
1309  }
1310  catch (JsonException ex)
1311  {
1312  _httpRequest.Dispose();
1313  if (_httpResponse != null)
1314  {
1315  _httpResponse.Dispose();
1316  }
1317  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1318  }
1319  }
1320  // Deserialize Response
1321  if ((int)_statusCode == 500)
1322  {
1323  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1324  try
1325  {
1326  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
1327  }
1328  catch (JsonException ex)
1329  {
1330  _httpRequest.Dispose();
1331  if (_httpResponse != null)
1332  {
1333  _httpResponse.Dispose();
1334  }
1335  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1336  }
1337  }
1338  // Deserialize Response
1339  if ((int)_statusCode == 503)
1340  {
1341  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1342  try
1343  {
1344  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
1345  }
1346  catch (JsonException ex)
1347  {
1348  _httpRequest.Dispose();
1349  if (_httpResponse != null)
1350  {
1351  _httpResponse.Dispose();
1352  }
1353  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1354  }
1355  }
1356  if (_shouldTrace)
1357  {
1358  ServiceClientTracing.Exit(_invocationId, _result);
1359  }
1360  return _result;
1361  }
1362 
1388  public async Task<HttpOperationResponse<object>> SetPrivateConversationDataWithHttpMessagesAsync(string channelId, string conversationId, string userId, BotData botData, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
1389  {
1390  if (channelId == null)
1391  {
1392  throw new ValidationException(ValidationRules.CannotBeNull, "channelId");
1393  }
1394  if (conversationId == null)
1395  {
1396  throw new ValidationException(ValidationRules.CannotBeNull, "conversationId");
1397  }
1398  if (userId == null)
1399  {
1400  throw new ValidationException(ValidationRules.CannotBeNull, "userId");
1401  }
1402  if (botData == null)
1403  {
1404  throw new ValidationException(ValidationRules.CannotBeNull, "botData");
1405  }
1406  // Tracing
1407  bool _shouldTrace = ServiceClientTracing.IsEnabled;
1408  string _invocationId = null;
1409  if (_shouldTrace)
1410  {
1411  _invocationId = ServiceClientTracing.NextInvocationId.ToString();
1412  Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
1413  tracingParameters.Add("channelId", channelId);
1414  tracingParameters.Add("conversationId", conversationId);
1415  tracingParameters.Add("userId", userId);
1416  tracingParameters.Add("botData", botData);
1417  tracingParameters.Add("cancellationToken", cancellationToken);
1418  ServiceClientTracing.Enter(_invocationId, this, "SetPrivateConversationData", tracingParameters);
1419  }
1420  // Construct URL
1421  var _baseUrl = this.Client.BaseUri.AbsoluteUri;
1422  var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "v3/botstate/{channelId}/conversations/{conversationId}/users/{userId}").ToString();
1423  _url = _url.Replace("{channelId}", Uri.EscapeDataString(channelId));
1424  _url = _url.Replace("{conversationId}", Uri.EscapeDataString(conversationId));
1425  _url = _url.Replace("{userId}", Uri.EscapeDataString(userId));
1426  // Create HTTP transport objects
1427  HttpRequestMessage _httpRequest = new HttpRequestMessage();
1428  HttpResponseMessage _httpResponse = null;
1429  _httpRequest.Method = new HttpMethod("POST");
1430  _httpRequest.RequestUri = new Uri(_url);
1431  // Set Headers
1432  if (customHeaders != null)
1433  {
1434  foreach(var _header in customHeaders)
1435  {
1436  if (_httpRequest.Headers.Contains(_header.Key))
1437  {
1438  _httpRequest.Headers.Remove(_header.Key);
1439  }
1440  _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
1441  }
1442  }
1443 
1444  // Serialize Request
1445  string _requestContent = null;
1446  if(botData != null)
1447  {
1448  _requestContent = SafeJsonConvert.SerializeObject(botData, this.Client.SerializationSettings);
1449  _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8);
1450  _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
1451  }
1452  // Set Credentials
1453  if (this.Client.Credentials != null)
1454  {
1455  cancellationToken.ThrowIfCancellationRequested();
1456  await this.Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
1457  }
1458  // Send Request
1459  if (_shouldTrace)
1460  {
1461  ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
1462  }
1463  cancellationToken.ThrowIfCancellationRequested();
1464  _httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
1465  if (_shouldTrace)
1466  {
1467  ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
1468  }
1469  HttpStatusCode _statusCode = _httpResponse.StatusCode;
1470  cancellationToken.ThrowIfCancellationRequested();
1471  string _responseContent = null;
1472  if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 401 && (int)_statusCode != 403 && (int)_statusCode != 404 && (int)_statusCode != 412 && (int)_statusCode != 500 && (int)_statusCode != 503)
1473  {
1474  var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
1475  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1476  ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
1477  ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
1478  if (_shouldTrace)
1479  {
1480  ServiceClientTracing.Error(_invocationId, ex);
1481  }
1482  _httpRequest.Dispose();
1483  if (_httpResponse != null)
1484  {
1485  _httpResponse.Dispose();
1486  }
1487  throw ex;
1488  }
1489  // Create Result
1490  var _result = new HttpOperationResponse<object>();
1491  _result.Request = _httpRequest;
1492  _result.Response = _httpResponse;
1493  // Deserialize Response
1494  if ((int)_statusCode == 200)
1495  {
1496  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1497  try
1498  {
1499  _result.Body = SafeJsonConvert.DeserializeObject<BotData>(_responseContent, this.Client.DeserializationSettings);
1500  }
1501  catch (JsonException ex)
1502  {
1503  _httpRequest.Dispose();
1504  if (_httpResponse != null)
1505  {
1506  _httpResponse.Dispose();
1507  }
1508  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1509  }
1510  }
1511  // Deserialize Response
1512  if ((int)_statusCode == 400)
1513  {
1514  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1515  try
1516  {
1517  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
1518  }
1519  catch (JsonException ex)
1520  {
1521  _httpRequest.Dispose();
1522  if (_httpResponse != null)
1523  {
1524  _httpResponse.Dispose();
1525  }
1526  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1527  }
1528  }
1529  // Deserialize Response
1530  if ((int)_statusCode == 404)
1531  {
1532  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1533  try
1534  {
1535  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
1536  }
1537  catch (JsonException ex)
1538  {
1539  _httpRequest.Dispose();
1540  if (_httpResponse != null)
1541  {
1542  _httpResponse.Dispose();
1543  }
1544  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1545  }
1546  }
1547  // Deserialize Response
1548  if ((int)_statusCode == 412)
1549  {
1550  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1551  try
1552  {
1553  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
1554  }
1555  catch (JsonException ex)
1556  {
1557  _httpRequest.Dispose();
1558  if (_httpResponse != null)
1559  {
1560  _httpResponse.Dispose();
1561  }
1562  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1563  }
1564  }
1565  // Deserialize Response
1566  if ((int)_statusCode == 500)
1567  {
1568  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1569  try
1570  {
1571  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
1572  }
1573  catch (JsonException ex)
1574  {
1575  _httpRequest.Dispose();
1576  if (_httpResponse != null)
1577  {
1578  _httpResponse.Dispose();
1579  }
1580  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1581  }
1582  }
1583  // Deserialize Response
1584  if ((int)_statusCode == 503)
1585  {
1586  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
1587  try
1588  {
1589  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
1590  }
1591  catch (JsonException ex)
1592  {
1593  _httpRequest.Dispose();
1594  if (_httpResponse != null)
1595  {
1596  _httpResponse.Dispose();
1597  }
1598  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
1599  }
1600  }
1601  if (_shouldTrace)
1602  {
1603  ServiceClientTracing.Exit(_invocationId, _result);
1604  }
1605  return _result;
1606  }
1607 
1608  }
1609 }
async Task< HttpOperationResponse< object > > GetUserDataWithHttpMessagesAsync(string channelId, string userId, Dictionary< string, List< string >> customHeaders=null, CancellationToken cancellationToken=default(CancellationToken))
GetUserData
Definition: BotState.cs:66
async Task< HttpOperationResponse< object > > DeleteStateForUserWithHttpMessagesAsync(string channelId, string userId, Dictionary< string, List< string >> customHeaders=null, CancellationToken cancellationToken=default(CancellationToken))
DeleteStateForUser
Definition: BotState.cs:507
async Task< HttpOperationResponse< object > > SetUserDataWithHttpMessagesAsync(string channelId, string userId, BotData botData, Dictionary< string, List< string >> customHeaders=null, CancellationToken cancellationToken=default(CancellationToken))
SetUserData
Definition: BotState.cs:273
The Bot State REST API allows your bot to store and retrieve state associated with conversations cond...
Definition: StateClient.cs:57
BotState operations.
Definition: IBotState.cs:18
BotState(StateClient client)
Initializes a new instance of the BotState class.
Definition: BotState.cs:33
BotState operations.
Definition: BotState.cs:25
async Task< HttpOperationResponse< object > > SetConversationDataWithHttpMessagesAsync(string channelId, string conversationId, BotData botData, Dictionary< string, List< string >> customHeaders=null, CancellationToken cancellationToken=default(CancellationToken))
SetConversationData
Definition: BotState.cs:936
async Task< HttpOperationResponse< object > > SetPrivateConversationDataWithHttpMessagesAsync(string channelId, string conversationId, string userId, BotData botData, Dictionary< string, List< string >> customHeaders=null, CancellationToken cancellationToken=default(CancellationToken))
SetPrivateConversationData
Definition: BotState.cs:1388
async Task< HttpOperationResponse< object > > GetPrivateConversationDataWithHttpMessagesAsync(string channelId, string conversationId, string userId, Dictionary< string, List< string >> customHeaders=null, CancellationToken cancellationToken=default(CancellationToken))
GetPrivateConversationData
Definition: BotState.cs:1172
async Task< HttpOperationResponse< object > > GetConversationDataWithHttpMessagesAsync(string channelId, string conversationId, Dictionary< string, List< string >> customHeaders=null, CancellationToken cancellationToken=default(CancellationToken))
GetConversationData
Definition: BotState.cs:729