Library/Microsoft.Bot.Connector/ConnectorAPI/Attachments.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 Attachments : IServiceOperations<ConnectorClient>, IAttachments
26  {
34  {
35  if (client == null)
36  {
37  throw new ArgumentNullException("client");
38  }
39  this.Client = client;
40  }
41 
45  public ConnectorClient Client { get; private set; }
46 
63  public async Task<HttpOperationResponse<object>> GetAttachmentInfoWithHttpMessagesAsync(string attachmentId, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
64  {
65  if (attachmentId == null)
66  {
67  throw new ValidationException(ValidationRules.CannotBeNull, "attachmentId");
68  }
69  // Tracing
70  bool _shouldTrace = ServiceClientTracing.IsEnabled;
71  string _invocationId = null;
72  if (_shouldTrace)
73  {
74  _invocationId = ServiceClientTracing.NextInvocationId.ToString();
75  Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
76  tracingParameters.Add("attachmentId", attachmentId);
77  tracingParameters.Add("cancellationToken", cancellationToken);
78  ServiceClientTracing.Enter(_invocationId, this, "GetAttachmentInfo", tracingParameters);
79  }
80  // Construct URL
81  var _baseUrl = this.Client.BaseUri.AbsoluteUri;
82  var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "v3/attachments/{attachmentId}").ToString();
83  _url = _url.Replace("{attachmentId}", Uri.EscapeDataString(attachmentId));
84  // Create HTTP transport objects
85  HttpRequestMessage _httpRequest = new HttpRequestMessage();
86  HttpResponseMessage _httpResponse = null;
87  _httpRequest.Method = new HttpMethod("GET");
88  _httpRequest.RequestUri = new Uri(_url);
89  // Set Headers
90  if (customHeaders != null)
91  {
92  foreach(var _header in customHeaders)
93  {
94  if (_httpRequest.Headers.Contains(_header.Key))
95  {
96  _httpRequest.Headers.Remove(_header.Key);
97  }
98  _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
99  }
100  }
101 
102  // Serialize Request
103  string _requestContent = null;
104  // Set Credentials
105  if (this.Client.Credentials != null)
106  {
107  cancellationToken.ThrowIfCancellationRequested();
108  await this.Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
109  }
110  // Send Request
111  if (_shouldTrace)
112  {
113  ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
114  }
115  cancellationToken.ThrowIfCancellationRequested();
116  _httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
117  if (_shouldTrace)
118  {
119  ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
120  }
121  HttpStatusCode _statusCode = _httpResponse.StatusCode;
122  cancellationToken.ThrowIfCancellationRequested();
123  string _responseContent = null;
124  if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 401 && (int)_statusCode != 403 && (int)_statusCode != 404 && (int)_statusCode != 500 && (int)_statusCode != 503)
125  {
126  var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
127  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
128  ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
129  ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
130  if (_shouldTrace)
131  {
132  ServiceClientTracing.Error(_invocationId, ex);
133  }
134  _httpRequest.Dispose();
135  if (_httpResponse != null)
136  {
137  _httpResponse.Dispose();
138  }
139  throw ex;
140  }
141  // Create Result
142  var _result = new HttpOperationResponse<object>();
143  _result.Request = _httpRequest;
144  _result.Response = _httpResponse;
145  // Deserialize Response
146  if ((int)_statusCode == 200)
147  {
148  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
149  try
150  {
151  _result.Body = SafeJsonConvert.DeserializeObject<AttachmentInfo>(_responseContent, this.Client.DeserializationSettings);
152  }
153  catch (JsonException ex)
154  {
155  _httpRequest.Dispose();
156  if (_httpResponse != null)
157  {
158  _httpResponse.Dispose();
159  }
160  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
161  }
162  }
163  // Deserialize Response
164  if ((int)_statusCode == 400)
165  {
166  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
167  try
168  {
169  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
170  }
171  catch (JsonException ex)
172  {
173  _httpRequest.Dispose();
174  if (_httpResponse != null)
175  {
176  _httpResponse.Dispose();
177  }
178  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
179  }
180  }
181  // Deserialize Response
182  if ((int)_statusCode == 404)
183  {
184  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
185  try
186  {
187  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
188  }
189  catch (JsonException ex)
190  {
191  _httpRequest.Dispose();
192  if (_httpResponse != null)
193  {
194  _httpResponse.Dispose();
195  }
196  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
197  }
198  }
199  // Deserialize Response
200  if ((int)_statusCode == 500)
201  {
202  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
203  try
204  {
205  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
206  }
207  catch (JsonException ex)
208  {
209  _httpRequest.Dispose();
210  if (_httpResponse != null)
211  {
212  _httpResponse.Dispose();
213  }
214  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
215  }
216  }
217  // Deserialize Response
218  if ((int)_statusCode == 503)
219  {
220  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
221  try
222  {
223  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
224  }
225  catch (JsonException ex)
226  {
227  _httpRequest.Dispose();
228  if (_httpResponse != null)
229  {
230  _httpResponse.Dispose();
231  }
232  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
233  }
234  }
235  if (_shouldTrace)
236  {
237  ServiceClientTracing.Exit(_invocationId, _result);
238  }
239  return _result;
240  }
241 
261  public async Task<HttpOperationResponse<object>> GetAttachmentWithHttpMessagesAsync(string attachmentId, string viewId, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
262  {
263  if (attachmentId == null)
264  {
265  throw new ValidationException(ValidationRules.CannotBeNull, "attachmentId");
266  }
267  if (viewId == null)
268  {
269  throw new ValidationException(ValidationRules.CannotBeNull, "viewId");
270  }
271  // Tracing
272  bool _shouldTrace = ServiceClientTracing.IsEnabled;
273  string _invocationId = null;
274  if (_shouldTrace)
275  {
276  _invocationId = ServiceClientTracing.NextInvocationId.ToString();
277  Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
278  tracingParameters.Add("attachmentId", attachmentId);
279  tracingParameters.Add("viewId", viewId);
280  tracingParameters.Add("cancellationToken", cancellationToken);
281  ServiceClientTracing.Enter(_invocationId, this, "GetAttachment", tracingParameters);
282  }
283  // Construct URL
284  var _baseUrl = this.Client.BaseUri.AbsoluteUri;
285  var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "v3/attachments/{attachmentId}/views/{viewId}").ToString();
286  _url = _url.Replace("{attachmentId}", Uri.EscapeDataString(attachmentId));
287  _url = _url.Replace("{viewId}", Uri.EscapeDataString(viewId));
288  // Create HTTP transport objects
289  HttpRequestMessage _httpRequest = new HttpRequestMessage();
290  HttpResponseMessage _httpResponse = null;
291  _httpRequest.Method = new HttpMethod("GET");
292  _httpRequest.RequestUri = new Uri(_url);
293  // Set Headers
294  if (customHeaders != null)
295  {
296  foreach(var _header in customHeaders)
297  {
298  if (_httpRequest.Headers.Contains(_header.Key))
299  {
300  _httpRequest.Headers.Remove(_header.Key);
301  }
302  _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
303  }
304  }
305 
306  // Serialize Request
307  string _requestContent = null;
308  // Set Credentials
309  if (this.Client.Credentials != null)
310  {
311  cancellationToken.ThrowIfCancellationRequested();
312  await this.Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
313  }
314  // Send Request
315  if (_shouldTrace)
316  {
317  ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
318  }
319  cancellationToken.ThrowIfCancellationRequested();
320  _httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
321  if (_shouldTrace)
322  {
323  ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
324  }
325  HttpStatusCode _statusCode = _httpResponse.StatusCode;
326  cancellationToken.ThrowIfCancellationRequested();
327  string _responseContent = null;
328  if ((int)_statusCode != 200 && (int)_statusCode != 301 && (int)_statusCode != 302 && (int)_statusCode != 400 && (int)_statusCode != 401 && (int)_statusCode != 403 && (int)_statusCode != 404 && (int)_statusCode != 500 && (int)_statusCode != 503)
329  {
330  var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
331  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
332  ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
333  ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
334  if (_shouldTrace)
335  {
336  ServiceClientTracing.Error(_invocationId, ex);
337  }
338  _httpRequest.Dispose();
339  if (_httpResponse != null)
340  {
341  _httpResponse.Dispose();
342  }
343  throw ex;
344  }
345  // Create Result
346  var _result = new HttpOperationResponse<object>();
347  _result.Request = _httpRequest;
348  _result.Response = _httpResponse;
349  // Deserialize Response
350  if ((int)_statusCode == 200)
351  {
352  _result.Body = await _httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
353  }
354  // Deserialize Response
355  if ((int)_statusCode == 400)
356  {
357  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
358  try
359  {
360  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, this.Client.DeserializationSettings);
361  }
362  catch (JsonException ex)
363  {
364  _httpRequest.Dispose();
365  if (_httpResponse != null)
366  {
367  _httpResponse.Dispose();
368  }
369  throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
370  }
371  }
372  // Deserialize Response
373  if ((int)_statusCode == 404)
374  {
375  _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
376  try
377  {
378  _result.Body = SafeJsonConvert.DeserializeObject<ErrorResponse>(_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 == 500)
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 == 503)
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  if (_shouldTrace)
427  {
428  ServiceClientTracing.Exit(_invocationId, _result);
429  }
430  return _result;
431  }
432 
433  }
434 }
async Task< HttpOperationResponse< object > > GetAttachmentWithHttpMessagesAsync(string attachmentId, string viewId, Dictionary< string, List< string >> customHeaders=null, CancellationToken cancellationToken=default(CancellationToken))
GetAttachment
Attachments(ConnectorClient client)
Initializes a new instance of the Attachments class.
async Task< HttpOperationResponse< object > > GetAttachmentInfoWithHttpMessagesAsync(string attachmentId, Dictionary< string, List< string >> customHeaders=null, CancellationToken cancellationToken=default(CancellationToken))
GetAttachmentInfo
´╗┐The Bot Connector REST API allows your bot to send and receive messages to channels configured in t...