PageRenderTime 3ms CodeModel.GetById 2ms app.highlight 27ms RepoModel.GetById 2ms app.codeStats 0ms

/src/MediaServicesManagement/Generated/AccountOperations.cs

https://github.com/edumunoz/azure-sdk-for-net
C# | 970 lines | 730 code | 80 blank | 160 comment | 192 complexity | fbf1656eedd062137b87bf64d08b3545 MD5 | raw file
  1// 
  2// Copyright (c) Microsoft and contributors.  All rights reserved.
  3// 
  4// Licensed under the Apache License, Version 2.0 (the "License");
  5// you may not use this file except in compliance with the License.
  6// You may obtain a copy of the License at
  7//   http://www.apache.org/licenses/LICENSE-2.0
  8// 
  9// Unless required by applicable law or agreed to in writing, software
 10// distributed under the License is distributed on an "AS IS" BASIS,
 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12// 
 13// See the License for the specific language governing permissions and
 14// limitations under the License.
 15// 
 16
 17// Warning: This code was generated by a tool.
 18// 
 19// Changes to this file may cause incorrect behavior and will be lost if the
 20// code is regenerated.
 21
 22using System;
 23using System.Collections.Generic;
 24using System.Linq;
 25using System.Net;
 26using System.Net.Http;
 27using System.Net.Http.Headers;
 28using System.Text;
 29using System.Threading;
 30using System.Threading.Tasks;
 31using System.Xml.Linq;
 32using Hyak.Common;
 33using Hyak.Common.Internals;
 34using Microsoft.Azure;
 35using Microsoft.WindowsAzure.Management.MediaServices;
 36using Microsoft.WindowsAzure.Management.MediaServices.Models;
 37using Newtonsoft.Json.Linq;
 38
 39namespace Microsoft.WindowsAzure.Management.MediaServices
 40{
 41    internal partial class AccountOperations : IServiceOperations<MediaServicesManagementClient>, IAccountOperations
 42    {
 43        /// <summary>
 44        /// Initializes a new instance of the AccountOperations class.
 45        /// </summary>
 46        /// <param name='client'>
 47        /// Reference to the service client.
 48        /// </param>
 49        internal AccountOperations(MediaServicesManagementClient client)
 50        {
 51            this._client = client;
 52        }
 53        
 54        private MediaServicesManagementClient _client;
 55        
 56        /// <summary>
 57        /// Gets a reference to the
 58        /// Microsoft.WindowsAzure.Management.MediaServices.MediaServicesManagementClient.
 59        /// </summary>
 60        public MediaServicesManagementClient Client
 61        {
 62            get { return this._client; }
 63        }
 64        
 65        /// <summary>
 66        /// The Create Media Services Account operation creates a new media
 67        /// services account in Windows Azure.  (see
 68        /// http://msdn.microsoft.com/en-us/library/windowsazure/dn194267.aspx
 69        /// for more information)
 70        /// </summary>
 71        /// <param name='parameters'>
 72        /// Required. Parameters supplied to the Create Media Services Account
 73        /// operation.
 74        /// </param>
 75        /// <param name='cancellationToken'>
 76        /// Cancellation token.
 77        /// </param>
 78        /// <returns>
 79        /// The Create Media Services Account operation response.
 80        /// </returns>
 81        public async Task<MediaServicesAccountCreateResponse> CreateAsync(MediaServicesAccountCreateParameters parameters, CancellationToken cancellationToken)
 82        {
 83            // Validate
 84            if (parameters == null)
 85            {
 86                throw new ArgumentNullException("parameters");
 87            }
 88            if (parameters.AccountName == null)
 89            {
 90                throw new ArgumentNullException("parameters.AccountName");
 91            }
 92            if (parameters.AccountName.Length < 3)
 93            {
 94                throw new ArgumentOutOfRangeException("parameters.AccountName");
 95            }
 96            if (parameters.AccountName.Length > 24)
 97            {
 98                throw new ArgumentOutOfRangeException("parameters.AccountName");
 99            }
100            if (parameters.BlobStorageEndpointUri == null)
101            {
102                throw new ArgumentNullException("parameters.BlobStorageEndpointUri");
103            }
104            if (parameters.Region == null)
105            {
106                throw new ArgumentNullException("parameters.Region");
107            }
108            if (parameters.Region.Length < 3)
109            {
110                throw new ArgumentOutOfRangeException("parameters.Region");
111            }
112            if (parameters.Region.Length > 256)
113            {
114                throw new ArgumentOutOfRangeException("parameters.Region");
115            }
116            if (parameters.StorageAccountKey == null)
117            {
118                throw new ArgumentNullException("parameters.StorageAccountKey");
119            }
120            if (parameters.StorageAccountKey.Length < 14)
121            {
122                throw new ArgumentOutOfRangeException("parameters.StorageAccountKey");
123            }
124            if (parameters.StorageAccountKey.Length > 256)
125            {
126                throw new ArgumentOutOfRangeException("parameters.StorageAccountKey");
127            }
128            if (parameters.StorageAccountName == null)
129            {
130                throw new ArgumentNullException("parameters.StorageAccountName");
131            }
132            if (parameters.StorageAccountName.Length < 3)
133            {
134                throw new ArgumentOutOfRangeException("parameters.StorageAccountName");
135            }
136            if (parameters.StorageAccountName.Length > 24)
137            {
138                throw new ArgumentOutOfRangeException("parameters.StorageAccountName");
139            }
140            foreach (char storageAccountNameChar in parameters.StorageAccountName)
141            {
142                if (char.IsLower(storageAccountNameChar) == false && char.IsDigit(storageAccountNameChar) == false)
143                {
144                    throw new ArgumentOutOfRangeException("parameters.StorageAccountName");
145                }
146            }
147            
148            // Tracing
149            bool shouldTrace = TracingAdapter.IsEnabled;
150            string invocationId = null;
151            if (shouldTrace)
152            {
153                invocationId = TracingAdapter.NextInvocationId.ToString();
154                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
155                tracingParameters.Add("parameters", parameters);
156                TracingAdapter.Enter(invocationId, this, "CreateAsync", tracingParameters);
157            }
158            
159            // Construct URL
160            string url = "";
161            url = url + "/";
162            if (this.Client.Credentials.SubscriptionId != null)
163            {
164                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
165            }
166            url = url + "/services/mediaservices/Accounts";
167            string baseUrl = this.Client.BaseUri.AbsoluteUri;
168            // Trim '/' character from the end of baseUrl and beginning of url.
169            if (baseUrl[baseUrl.Length - 1] == '/')
170            {
171                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
172            }
173            if (url[0] == '/')
174            {
175                url = url.Substring(1);
176            }
177            url = baseUrl + "/" + url;
178            url = url.Replace(" ", "%20");
179            
180            // Create HTTP transport objects
181            HttpRequestMessage httpRequest = null;
182            try
183            {
184                httpRequest = new HttpRequestMessage();
185                httpRequest.Method = HttpMethod.Post;
186                httpRequest.RequestUri = new Uri(url);
187                
188                // Set Headers
189                httpRequest.Headers.Add("x-ms-version", "2011-10-01");
190                
191                // Set Credentials
192                cancellationToken.ThrowIfCancellationRequested();
193                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
194                
195                // Serialize Request
196                string requestContent = null;
197                XDocument requestDoc = new XDocument();
198                
199                XElement accountCreationRequestElement = new XElement(XName.Get("AccountCreationRequest", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
200                requestDoc.Add(accountCreationRequestElement);
201                
202                XElement accountNameElement = new XElement(XName.Get("AccountName", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
203                accountNameElement.Value = parameters.AccountName;
204                accountCreationRequestElement.Add(accountNameElement);
205                
206                XElement blobStorageEndpointUriElement = new XElement(XName.Get("BlobStorageEndpointUri", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
207                blobStorageEndpointUriElement.Value = parameters.BlobStorageEndpointUri.AbsoluteUri;
208                accountCreationRequestElement.Add(blobStorageEndpointUriElement);
209                
210                XElement regionElement = new XElement(XName.Get("Region", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
211                regionElement.Value = parameters.Region;
212                accountCreationRequestElement.Add(regionElement);
213                
214                XElement storageAccountKeyElement = new XElement(XName.Get("StorageAccountKey", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
215                storageAccountKeyElement.Value = parameters.StorageAccountKey;
216                accountCreationRequestElement.Add(storageAccountKeyElement);
217                
218                XElement storageAccountNameElement = new XElement(XName.Get("StorageAccountName", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
219                storageAccountNameElement.Value = parameters.StorageAccountName;
220                accountCreationRequestElement.Add(storageAccountNameElement);
221                
222                requestContent = requestDoc.ToString();
223                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
224                httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/xml");
225                
226                // Send Request
227                HttpResponseMessage httpResponse = null;
228                try
229                {
230                    if (shouldTrace)
231                    {
232                        TracingAdapter.SendRequest(invocationId, httpRequest);
233                    }
234                    cancellationToken.ThrowIfCancellationRequested();
235                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
236                    if (shouldTrace)
237                    {
238                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
239                    }
240                    HttpStatusCode statusCode = httpResponse.StatusCode;
241                    if (statusCode != HttpStatusCode.Created)
242                    {
243                        cancellationToken.ThrowIfCancellationRequested();
244                        CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
245                        if (shouldTrace)
246                        {
247                            TracingAdapter.Error(invocationId, ex);
248                        }
249                        throw ex;
250                    }
251                    
252                    // Create Result
253                    MediaServicesAccountCreateResponse result = null;
254                    // Deserialize Response
255                    if (statusCode == HttpStatusCode.Created)
256                    {
257                        cancellationToken.ThrowIfCancellationRequested();
258                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
259                        result = new MediaServicesAccountCreateResponse();
260                        JToken responseDoc = null;
261                        if (string.IsNullOrEmpty(responseContent) == false)
262                        {
263                            responseDoc = JToken.Parse(responseContent);
264                        }
265                        
266                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
267                        {
268                            MediaServicesCreatedAccount accountInstance = new MediaServicesCreatedAccount();
269                            result.Account = accountInstance;
270                            
271                            JToken accountIdValue = responseDoc["AccountId"];
272                            if (accountIdValue != null && accountIdValue.Type != JTokenType.Null)
273                            {
274                                string accountIdInstance = ((string)accountIdValue);
275                                accountInstance.AccountId = accountIdInstance;
276                            }
277                            
278                            JToken accountNameValue = responseDoc["AccountName"];
279                            if (accountNameValue != null && accountNameValue.Type != JTokenType.Null)
280                            {
281                                string accountNameInstance = ((string)accountNameValue);
282                                accountInstance.AccountName = accountNameInstance;
283                            }
284                            
285                            JToken subscriptionValue = responseDoc["Subscription"];
286                            if (subscriptionValue != null && subscriptionValue.Type != JTokenType.Null)
287                            {
288                                string subscriptionInstance = ((string)subscriptionValue);
289                                accountInstance.SubscriptionId = subscriptionInstance;
290                            }
291                        }
292                        
293                    }
294                    result.StatusCode = statusCode;
295                    if (httpResponse.Headers.Contains("x-ms-request-id"))
296                    {
297                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
298                    }
299                    
300                    if (shouldTrace)
301                    {
302                        TracingAdapter.Exit(invocationId, result);
303                    }
304                    return result;
305                }
306                finally
307                {
308                    if (httpResponse != null)
309                    {
310                        httpResponse.Dispose();
311                    }
312                }
313            }
314            finally
315            {
316                if (httpRequest != null)
317                {
318                    httpRequest.Dispose();
319                }
320            }
321        }
322        
323        /// <summary>
324        /// The Delete Media Services Account operation deletes an existing
325        /// media services account in Windows Azure.  (see
326        /// http://msdn.microsoft.com/en-us/library/windowsazure/dn194273.aspx
327        /// for more information)
328        /// </summary>
329        /// <param name='accountName'>
330        /// Required. The name of the media services account.
331        /// </param>
332        /// <param name='cancellationToken'>
333        /// Cancellation token.
334        /// </param>
335        /// <returns>
336        /// A standard service response including an HTTP status code and
337        /// request ID.
338        /// </returns>
339        public async Task<AzureOperationResponse> DeleteAsync(string accountName, CancellationToken cancellationToken)
340        {
341            // Validate
342            if (accountName == null)
343            {
344                throw new ArgumentNullException("accountName");
345            }
346            
347            // Tracing
348            bool shouldTrace = TracingAdapter.IsEnabled;
349            string invocationId = null;
350            if (shouldTrace)
351            {
352                invocationId = TracingAdapter.NextInvocationId.ToString();
353                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
354                tracingParameters.Add("accountName", accountName);
355                TracingAdapter.Enter(invocationId, this, "DeleteAsync", tracingParameters);
356            }
357            
358            // Construct URL
359            string url = "";
360            url = url + "/";
361            if (this.Client.Credentials.SubscriptionId != null)
362            {
363                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
364            }
365            url = url + "/services/mediaservices/Accounts/";
366            url = url + Uri.EscapeDataString(accountName);
367            string baseUrl = this.Client.BaseUri.AbsoluteUri;
368            // Trim '/' character from the end of baseUrl and beginning of url.
369            if (baseUrl[baseUrl.Length - 1] == '/')
370            {
371                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
372            }
373            if (url[0] == '/')
374            {
375                url = url.Substring(1);
376            }
377            url = baseUrl + "/" + url;
378            url = url.Replace(" ", "%20");
379            
380            // Create HTTP transport objects
381            HttpRequestMessage httpRequest = null;
382            try
383            {
384                httpRequest = new HttpRequestMessage();
385                httpRequest.Method = HttpMethod.Delete;
386                httpRequest.RequestUri = new Uri(url);
387                
388                // Set Headers
389                httpRequest.Headers.Add("x-ms-version", "2011-10-01");
390                
391                // Set Credentials
392                cancellationToken.ThrowIfCancellationRequested();
393                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
394                
395                // Send Request
396                HttpResponseMessage httpResponse = null;
397                try
398                {
399                    if (shouldTrace)
400                    {
401                        TracingAdapter.SendRequest(invocationId, httpRequest);
402                    }
403                    cancellationToken.ThrowIfCancellationRequested();
404                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
405                    if (shouldTrace)
406                    {
407                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
408                    }
409                    HttpStatusCode statusCode = httpResponse.StatusCode;
410                    if (statusCode != HttpStatusCode.NoContent)
411                    {
412                        cancellationToken.ThrowIfCancellationRequested();
413                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
414                        if (shouldTrace)
415                        {
416                            TracingAdapter.Error(invocationId, ex);
417                        }
418                        throw ex;
419                    }
420                    
421                    // Create Result
422                    AzureOperationResponse result = null;
423                    // Deserialize Response
424                    result = new AzureOperationResponse();
425                    result.StatusCode = statusCode;
426                    if (httpResponse.Headers.Contains("x-ms-request-id"))
427                    {
428                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
429                    }
430                    
431                    if (shouldTrace)
432                    {
433                        TracingAdapter.Exit(invocationId, result);
434                    }
435                    return result;
436                }
437                finally
438                {
439                    if (httpResponse != null)
440                    {
441                        httpResponse.Dispose();
442                    }
443                }
444            }
445            finally
446            {
447                if (httpRequest != null)
448                {
449                    httpRequest.Dispose();
450                }
451            }
452        }
453        
454        /// <summary>
455        /// The Get Media Services Account operation gets detailed information
456        /// about a media services account in Windows Azure.  (see
457        /// http://msdn.microsoft.com/en-us/library/windowsazure/dn166974.aspx
458        /// for more information)
459        /// </summary>
460        /// <param name='accountName'>
461        /// Required. The name of the Media Services account.
462        /// </param>
463        /// <param name='cancellationToken'>
464        /// Cancellation token.
465        /// </param>
466        /// <returns>
467        /// The Get Media Services Account operation response.
468        /// </returns>
469        public async Task<MediaServicesAccountGetResponse> GetAsync(string accountName, CancellationToken cancellationToken)
470        {
471            // Validate
472            if (accountName == null)
473            {
474                throw new ArgumentNullException("accountName");
475            }
476            
477            // Tracing
478            bool shouldTrace = TracingAdapter.IsEnabled;
479            string invocationId = null;
480            if (shouldTrace)
481            {
482                invocationId = TracingAdapter.NextInvocationId.ToString();
483                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
484                tracingParameters.Add("accountName", accountName);
485                TracingAdapter.Enter(invocationId, this, "GetAsync", tracingParameters);
486            }
487            
488            // Construct URL
489            string url = "";
490            url = url + "/";
491            if (this.Client.Credentials.SubscriptionId != null)
492            {
493                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
494            }
495            url = url + "/services/mediaservices/Accounts/";
496            url = url + Uri.EscapeDataString(accountName);
497            string baseUrl = this.Client.BaseUri.AbsoluteUri;
498            // Trim '/' character from the end of baseUrl and beginning of url.
499            if (baseUrl[baseUrl.Length - 1] == '/')
500            {
501                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
502            }
503            if (url[0] == '/')
504            {
505                url = url.Substring(1);
506            }
507            url = baseUrl + "/" + url;
508            url = url.Replace(" ", "%20");
509            
510            // Create HTTP transport objects
511            HttpRequestMessage httpRequest = null;
512            try
513            {
514                httpRequest = new HttpRequestMessage();
515                httpRequest.Method = HttpMethod.Get;
516                httpRequest.RequestUri = new Uri(url);
517                
518                // Set Headers
519                httpRequest.Headers.Add("x-ms-version", "2011-10-01");
520                
521                // Set Credentials
522                cancellationToken.ThrowIfCancellationRequested();
523                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
524                
525                // Send Request
526                HttpResponseMessage httpResponse = null;
527                try
528                {
529                    if (shouldTrace)
530                    {
531                        TracingAdapter.SendRequest(invocationId, httpRequest);
532                    }
533                    cancellationToken.ThrowIfCancellationRequested();
534                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
535                    if (shouldTrace)
536                    {
537                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
538                    }
539                    HttpStatusCode statusCode = httpResponse.StatusCode;
540                    if (statusCode != HttpStatusCode.OK)
541                    {
542                        cancellationToken.ThrowIfCancellationRequested();
543                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
544                        if (shouldTrace)
545                        {
546                            TracingAdapter.Error(invocationId, ex);
547                        }
548                        throw ex;
549                    }
550                    
551                    // Create Result
552                    MediaServicesAccountGetResponse result = null;
553                    // Deserialize Response
554                    if (statusCode == HttpStatusCode.OK)
555                    {
556                        cancellationToken.ThrowIfCancellationRequested();
557                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
558                        result = new MediaServicesAccountGetResponse();
559                        JToken responseDoc = null;
560                        if (string.IsNullOrEmpty(responseContent) == false)
561                        {
562                            responseDoc = JToken.Parse(responseContent);
563                        }
564                        
565                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
566                        {
567                            MediaServicesAccount accountInstance = new MediaServicesAccount();
568                            result.Account = accountInstance;
569                            
570                            JToken accountNameValue = responseDoc["AccountName"];
571                            if (accountNameValue != null && accountNameValue.Type != JTokenType.Null)
572                            {
573                                string accountNameInstance = ((string)accountNameValue);
574                                accountInstance.AccountName = accountNameInstance;
575                            }
576                            
577                            JToken accountKeyValue = responseDoc["AccountKey"];
578                            if (accountKeyValue != null && accountKeyValue.Type != JTokenType.Null)
579                            {
580                                string accountKeyInstance = ((string)accountKeyValue);
581                                accountInstance.AccountKey = accountKeyInstance;
582                            }
583                            
584                            JToken accountKeysValue = responseDoc["AccountKeys"];
585                            if (accountKeysValue != null && accountKeysValue.Type != JTokenType.Null)
586                            {
587                                MediaServicesAccount.AccountKeys accountKeysInstance = new MediaServicesAccount.AccountKeys();
588                                accountInstance.StorageAccountKeys = accountKeysInstance;
589                                
590                                JToken primaryValue = accountKeysValue["Primary"];
591                                if (primaryValue != null && primaryValue.Type != JTokenType.Null)
592                                {
593                                    string primaryInstance = ((string)primaryValue);
594                                    accountKeysInstance.Primary = primaryInstance;
595                                }
596                                
597                                JToken secondaryValue = accountKeysValue["Secondary"];
598                                if (secondaryValue != null && secondaryValue.Type != JTokenType.Null)
599                                {
600                                    string secondaryInstance = ((string)secondaryValue);
601                                    accountKeysInstance.Secondary = secondaryInstance;
602                                }
603                            }
604                            
605                            JToken accountRegionValue = responseDoc["AccountRegion"];
606                            if (accountRegionValue != null && accountRegionValue.Type != JTokenType.Null)
607                            {
608                                string accountRegionInstance = ((string)accountRegionValue);
609                                accountInstance.AccountRegion = accountRegionInstance;
610                            }
611                            
612                            JToken storageAccountNameValue = responseDoc["StorageAccountName"];
613                            if (storageAccountNameValue != null && storageAccountNameValue.Type != JTokenType.Null)
614                            {
615                                string storageAccountNameInstance = ((string)storageAccountNameValue);
616                                accountInstance.StorageAccountName = storageAccountNameInstance;
617                            }
618                        }
619                        
620                    }
621                    result.StatusCode = statusCode;
622                    if (httpResponse.Headers.Contains("x-ms-request-id"))
623                    {
624                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
625                    }
626                    
627                    if (shouldTrace)
628                    {
629                        TracingAdapter.Exit(invocationId, result);
630                    }
631                    return result;
632                }
633                finally
634                {
635                    if (httpResponse != null)
636                    {
637                        httpResponse.Dispose();
638                    }
639                }
640            }
641            finally
642            {
643                if (httpRequest != null)
644                {
645                    httpRequest.Dispose();
646                }
647            }
648        }
649        
650        /// <summary>
651        /// The List Media Services Account operation gets information about
652        /// all existing media services accounts associated with the current
653        /// subscription in Windows Azure.  (see
654        /// http://msdn.microsoft.com/en-us/library/windowsazure/dn166989.aspx
655        /// for more information)
656        /// </summary>
657        /// <param name='cancellationToken'>
658        /// Cancellation token.
659        /// </param>
660        /// <returns>
661        /// The List Media Accounts operation response.
662        /// </returns>
663        public async Task<MediaServicesAccountListResponse> ListAsync(CancellationToken cancellationToken)
664        {
665            // Validate
666            
667            // Tracing
668            bool shouldTrace = TracingAdapter.IsEnabled;
669            string invocationId = null;
670            if (shouldTrace)
671            {
672                invocationId = TracingAdapter.NextInvocationId.ToString();
673                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
674                TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters);
675            }
676            
677            // Construct URL
678            string url = "";
679            url = url + "/";
680            if (this.Client.Credentials.SubscriptionId != null)
681            {
682                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
683            }
684            url = url + "/services/mediaservices/Accounts";
685            string baseUrl = this.Client.BaseUri.AbsoluteUri;
686            // Trim '/' character from the end of baseUrl and beginning of url.
687            if (baseUrl[baseUrl.Length - 1] == '/')
688            {
689                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
690            }
691            if (url[0] == '/')
692            {
693                url = url.Substring(1);
694            }
695            url = baseUrl + "/" + url;
696            url = url.Replace(" ", "%20");
697            
698            // Create HTTP transport objects
699            HttpRequestMessage httpRequest = null;
700            try
701            {
702                httpRequest = new HttpRequestMessage();
703                httpRequest.Method = HttpMethod.Get;
704                httpRequest.RequestUri = new Uri(url);
705                
706                // Set Headers
707                httpRequest.Headers.Add("x-ms-version", "2011-10-01");
708                
709                // Set Credentials
710                cancellationToken.ThrowIfCancellationRequested();
711                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
712                
713                // Send Request
714                HttpResponseMessage httpResponse = null;
715                try
716                {
717                    if (shouldTrace)
718                    {
719                        TracingAdapter.SendRequest(invocationId, httpRequest);
720                    }
721                    cancellationToken.ThrowIfCancellationRequested();
722                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
723                    if (shouldTrace)
724                    {
725                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
726                    }
727                    HttpStatusCode statusCode = httpResponse.StatusCode;
728                    if (statusCode != HttpStatusCode.OK)
729                    {
730                        cancellationToken.ThrowIfCancellationRequested();
731                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
732                        if (shouldTrace)
733                        {
734                            TracingAdapter.Error(invocationId, ex);
735                        }
736                        throw ex;
737                    }
738                    
739                    // Create Result
740                    MediaServicesAccountListResponse result = null;
741                    // Deserialize Response
742                    if (statusCode == HttpStatusCode.OK)
743                    {
744                        cancellationToken.ThrowIfCancellationRequested();
745                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
746                        result = new MediaServicesAccountListResponse();
747                        XDocument responseDoc = XDocument.Parse(responseContent);
748                        
749                        XElement serviceResourcesSequenceElement = responseDoc.Element(XName.Get("ServiceResources", "http://schemas.microsoft.com/windowsazure"));
750                        if (serviceResourcesSequenceElement != null)
751                        {
752                            foreach (XElement serviceResourcesElement in serviceResourcesSequenceElement.Elements(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure")))
753                            {
754                                MediaServicesAccountListResponse.MediaServiceAccount serviceResourceInstance = new MediaServicesAccountListResponse.MediaServiceAccount();
755                                result.Accounts.Add(serviceResourceInstance);
756                                
757                                XElement nameElement = serviceResourcesElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
758                                if (nameElement != null)
759                                {
760                                    string nameInstance = nameElement.Value;
761                                    serviceResourceInstance.Name = nameInstance;
762                                }
763                                
764                                XElement typeElement = serviceResourcesElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure"));
765                                if (typeElement != null)
766                                {
767                                    string typeInstance = typeElement.Value;
768                                    serviceResourceInstance.Type = typeInstance;
769                                }
770                                
771                                XElement stateElement = serviceResourcesElement.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure"));
772                                if (stateElement != null)
773                                {
774                                    string stateInstance = stateElement.Value;
775                                    serviceResourceInstance.State = stateInstance;
776                                }
777                                
778                                XElement selfLinkElement = serviceResourcesElement.Element(XName.Get("SelfLink", "http://schemas.microsoft.com/windowsazure"));
779                                if (selfLinkElement != null)
780                                {
781                                    Uri selfLinkInstance = TypeConversion.TryParseUri(selfLinkElement.Value);
782                                    serviceResourceInstance.Uri = selfLinkInstance;
783                                }
784                                
785                                XElement parentLinkElement = serviceResourcesElement.Element(XName.Get("ParentLink", "http://schemas.microsoft.com/windowsazure"));
786                                if (parentLinkElement != null)
787                                {
788                                    Uri parentLinkInstance = TypeConversion.TryParseUri(parentLinkElement.Value);
789                                    serviceResourceInstance.ParentUri = parentLinkInstance;
790                                }
791                                
792                                XElement accountIdElement = serviceResourcesElement.Element(XName.Get("AccountId", "http://schemas.microsoft.com/windowsazure"));
793                                if (accountIdElement != null)
794                                {
795                                    string accountIdInstance = accountIdElement.Value;
796                                    serviceResourceInstance.AccountId = accountIdInstance;
797                                }
798                            }
799                        }
800                        
801                    }
802                    result.StatusCode = statusCode;
803                    if (httpResponse.Headers.Contains("x-ms-request-id"))
804                    {
805                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
806                    }
807                    
808                    if (shouldTrace)
809                    {
810                        TracingAdapter.Exit(invocationId, result);
811                    }
812                    return result;
813                }
814                finally
815                {
816                    if (httpResponse != null)
817                    {
818                        httpResponse.Dispose();
819                    }
820                }
821            }
822            finally
823            {
824                if (httpRequest != null)
825                {
826                    httpRequest.Dispose();
827                }
828            }
829        }
830        
831        /// <summary>
832        /// The Regenerate Media Services Account Key operation regenerates an
833        /// account key for the given Media Services account in Windows Azure.
834        /// (see
835        /// http://msdn.microsoft.com/en-us/library/windowsazure/dn167010.aspx
836        /// for more information)
837        /// </summary>
838        /// <param name='accountName'>
839        /// Required. The name of the Media Services Account.
840        /// </param>
841        /// <param name='keyType'>
842        /// Required. The type of key to regenerate (primary or secondary)
843        /// </param>
844        /// <param name='cancellationToken'>
845        /// Cancellation token.
846        /// </param>
847        /// <returns>
848        /// A standard service response including an HTTP status code and
849        /// request ID.
850        /// </returns>
851        public async Task<AzureOperationResponse> RegenerateKeyAsync(string accountName, MediaServicesKeyType keyType, CancellationToken cancellationToken)
852        {
853            // Validate
854            if (accountName == null)
855            {
856                throw new ArgumentNullException("accountName");
857            }
858            
859            // Tracing
860            bool shouldTrace = TracingAdapter.IsEnabled;
861            string invocationId = null;
862            if (shouldTrace)
863            {
864                invocationId = TracingAdapter.NextInvocationId.ToString();
865                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
866                tracingParameters.Add("accountName", accountName);
867                tracingParameters.Add("keyType", keyType);
868                TracingAdapter.Enter(invocationId, this, "RegenerateKeyAsync", tracingParameters);
869            }
870            
871            // Construct URL
872            string url = "";
873            url = url + "/";
874            if (this.Client.Credentials.SubscriptionId != null)
875            {
876                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
877            }
878            url = url + "/services/mediaservices/Accounts/";
879            url = url + Uri.EscapeDataString(accountName);
880            url = url + "/AccountKeys/";
881            url = url + Uri.EscapeDataString(keyType.ToString());
882            url = url + "/Regenerate";
883            string baseUrl = this.Client.BaseUri.AbsoluteUri;
884            // Trim '/' character from the end of baseUrl and beginning of url.
885            if (baseUrl[baseUrl.Length - 1] == '/')
886            {
887                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
888            }
889            if (url[0] == '/')
890            {
891                url = url.Substring(1);
892            }
893            url = baseUrl + "/" + url;
894            url = url.Replace(" ", "%20");
895            
896            // Create HTTP transport objects
897            HttpRequestMessage httpRequest = null;
898            try
899            {
900                httpRequest = new HttpRequestMessage();
901                httpRequest.Method = HttpMethod.Post;
902                httpRequest.RequestUri = new Uri(url);
903                
904                // Set Headers
905                httpRequest.Headers.Add("x-ms-version", "2011-10-01");
906                
907                // Set Credentials
908                cancellationToken.ThrowIfCancellationRequested();
909                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
910                
911                // Send Request
912                HttpResponseMessage httpResponse = null;
913                try
914                {
915                    if (shouldTrace)
916                    {
917                        TracingAdapter.SendRequest(invocationId, httpRequest);
918                    }
919                    cancellationToken.ThrowIfCancellationRequested();
920                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
921                    if (shouldTrace)
922                    {
923                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
924                    }
925                    HttpStatusCode statusCode = httpResponse.StatusCode;
926                    if (statusCode != HttpStatusCode.NoContent)
927                    {
928                        cancellationToken.ThrowIfCancellationRequested();
929                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
930                        if (shouldTrace)
931                        {
932                            TracingAdapter.Error(invocationId, ex);
933                        }
934                        throw ex;
935                    }
936                    
937                    // Create Result
938                    AzureOperationResponse result = null;
939                    // Deserialize Response
940                    result = new AzureOperationResponse();
941                    result.StatusCode = statusCode;
942                    if (httpResponse.Headers.Contains("x-ms-request-id"))
943                    {
944                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
945                    }
946                    
947                    if (shouldTrace)
948                    {
949                        TracingAdapter.Exit(invocationId, result);
950                    }
951                    return result;
952                }
953                finally
954                {
955                    if (httpResponse != null)
956                    {
957                        httpResponse.Dispose();
958                    }
959                }
960            }
961            finally
962            {
963                if (httpRequest != null)
964                {
965                    httpRequest.Dispose();
966                }
967            }
968        }
969    }
970}