/src/MediaServicesManagement/Generated/AccountOperations.cs
C# | 970 lines | 730 code | 80 blank | 160 comment | 192 complexity | fbf1656eedd062137b87bf64d08b3545 MD5 | raw file
- //
- // Copyright (c) Microsoft and contributors. All rights reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- //
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- // Warning: This code was generated by a tool.
- //
- // Changes to this file may cause incorrect behavior and will be lost if the
- // code is regenerated.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Xml.Linq;
- using Hyak.Common;
- using Hyak.Common.Internals;
- using Microsoft.Azure;
- using Microsoft.WindowsAzure.Management.MediaServices;
- using Microsoft.WindowsAzure.Management.MediaServices.Models;
- using Newtonsoft.Json.Linq;
- namespace Microsoft.WindowsAzure.Management.MediaServices
- {
- internal partial class AccountOperations : IServiceOperations<MediaServicesManagementClient>, IAccountOperations
- {
- /// <summary>
- /// Initializes a new instance of the AccountOperations class.
- /// </summary>
- /// <param name='client'>
- /// Reference to the service client.
- /// </param>
- internal AccountOperations(MediaServicesManagementClient client)
- {
- this._client = client;
- }
-
- private MediaServicesManagementClient _client;
-
- /// <summary>
- /// Gets a reference to the
- /// Microsoft.WindowsAzure.Management.MediaServices.MediaServicesManagementClient.
- /// </summary>
- public MediaServicesManagementClient Client
- {
- get { return this._client; }
- }
-
- /// <summary>
- /// The Create Media Services Account operation creates a new media
- /// services account in Windows Azure. (see
- /// http://msdn.microsoft.com/en-us/library/windowsazure/dn194267.aspx
- /// for more information)
- /// </summary>
- /// <param name='parameters'>
- /// Required. Parameters supplied to the Create Media Services Account
- /// operation.
- /// </param>
- /// <param name='cancellationToken'>
- /// Cancellation token.
- /// </param>
- /// <returns>
- /// The Create Media Services Account operation response.
- /// </returns>
- public async Task<MediaServicesAccountCreateResponse> CreateAsync(MediaServicesAccountCreateParameters parameters, CancellationToken cancellationToken)
- {
- // Validate
- if (parameters == null)
- {
- throw new ArgumentNullException("parameters");
- }
- if (parameters.AccountName == null)
- {
- throw new ArgumentNullException("parameters.AccountName");
- }
- if (parameters.AccountName.Length < 3)
- {
- throw new ArgumentOutOfRangeException("parameters.AccountName");
- }
- if (parameters.AccountName.Length > 24)
- {
- throw new ArgumentOutOfRangeException("parameters.AccountName");
- }
- if (parameters.BlobStorageEndpointUri == null)
- {
- throw new ArgumentNullException("parameters.BlobStorageEndpointUri");
- }
- if (parameters.Region == null)
- {
- throw new ArgumentNullException("parameters.Region");
- }
- if (parameters.Region.Length < 3)
- {
- throw new ArgumentOutOfRangeException("parameters.Region");
- }
- if (parameters.Region.Length > 256)
- {
- throw new ArgumentOutOfRangeException("parameters.Region");
- }
- if (parameters.StorageAccountKey == null)
- {
- throw new ArgumentNullException("parameters.StorageAccountKey");
- }
- if (parameters.StorageAccountKey.Length < 14)
- {
- throw new ArgumentOutOfRangeException("parameters.StorageAccountKey");
- }
- if (parameters.StorageAccountKey.Length > 256)
- {
- throw new ArgumentOutOfRangeException("parameters.StorageAccountKey");
- }
- if (parameters.StorageAccountName == null)
- {
- throw new ArgumentNullException("parameters.StorageAccountName");
- }
- if (parameters.StorageAccountName.Length < 3)
- {
- throw new ArgumentOutOfRangeException("parameters.StorageAccountName");
- }
- if (parameters.StorageAccountName.Length > 24)
- {
- throw new ArgumentOutOfRangeException("parameters.StorageAccountName");
- }
- foreach (char storageAccountNameChar in parameters.StorageAccountName)
- {
- if (char.IsLower(storageAccountNameChar) == false && char.IsDigit(storageAccountNameChar) == false)
- {
- throw new ArgumentOutOfRangeException("parameters.StorageAccountName");
- }
- }
-
- // Tracing
- bool shouldTrace = TracingAdapter.IsEnabled;
- string invocationId = null;
- if (shouldTrace)
- {
- invocationId = TracingAdapter.NextInvocationId.ToString();
- Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
- tracingParameters.Add("parameters", parameters);
- TracingAdapter.Enter(invocationId, this, "CreateAsync", tracingParameters);
- }
-
- // Construct URL
- string url = "";
- url = url + "/";
- if (this.Client.Credentials.SubscriptionId != null)
- {
- url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
- }
- url = url + "/services/mediaservices/Accounts";
- string baseUrl = this.Client.BaseUri.AbsoluteUri;
- // Trim '/' character from the end of baseUrl and beginning of url.
- if (baseUrl[baseUrl.Length - 1] == '/')
- {
- baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
- }
- if (url[0] == '/')
- {
- url = url.Substring(1);
- }
- url = baseUrl + "/" + url;
- url = url.Replace(" ", "%20");
-
- // Create HTTP transport objects
- HttpRequestMessage httpRequest = null;
- try
- {
- httpRequest = new HttpRequestMessage();
- httpRequest.Method = HttpMethod.Post;
- httpRequest.RequestUri = new Uri(url);
-
- // Set Headers
- httpRequest.Headers.Add("x-ms-version", "2011-10-01");
-
- // Set Credentials
- cancellationToken.ThrowIfCancellationRequested();
- await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
-
- // Serialize Request
- string requestContent = null;
- XDocument requestDoc = new XDocument();
-
- XElement accountCreationRequestElement = new XElement(XName.Get("AccountCreationRequest", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
- requestDoc.Add(accountCreationRequestElement);
-
- XElement accountNameElement = new XElement(XName.Get("AccountName", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
- accountNameElement.Value = parameters.AccountName;
- accountCreationRequestElement.Add(accountNameElement);
-
- XElement blobStorageEndpointUriElement = new XElement(XName.Get("BlobStorageEndpointUri", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
- blobStorageEndpointUriElement.Value = parameters.BlobStorageEndpointUri.AbsoluteUri;
- accountCreationRequestElement.Add(blobStorageEndpointUriElement);
-
- XElement regionElement = new XElement(XName.Get("Region", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
- regionElement.Value = parameters.Region;
- accountCreationRequestElement.Add(regionElement);
-
- XElement storageAccountKeyElement = new XElement(XName.Get("StorageAccountKey", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
- storageAccountKeyElement.Value = parameters.StorageAccountKey;
- accountCreationRequestElement.Add(storageAccountKeyElement);
-
- XElement storageAccountNameElement = new XElement(XName.Get("StorageAccountName", "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models"));
- storageAccountNameElement.Value = parameters.StorageAccountName;
- accountCreationRequestElement.Add(storageAccountNameElement);
-
- requestContent = requestDoc.ToString();
- httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
- httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/xml");
-
- // Send Request
- HttpResponseMessage httpResponse = null;
- try
- {
- if (shouldTrace)
- {
- TracingAdapter.SendRequest(invocationId, httpRequest);
- }
- cancellationToken.ThrowIfCancellationRequested();
- httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
- if (shouldTrace)
- {
- TracingAdapter.ReceiveResponse(invocationId, httpResponse);
- }
- HttpStatusCode statusCode = httpResponse.StatusCode;
- if (statusCode != HttpStatusCode.Created)
- {
- cancellationToken.ThrowIfCancellationRequested();
- CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
- if (shouldTrace)
- {
- TracingAdapter.Error(invocationId, ex);
- }
- throw ex;
- }
-
- // Create Result
- MediaServicesAccountCreateResponse result = null;
- // Deserialize Response
- if (statusCode == HttpStatusCode.Created)
- {
- cancellationToken.ThrowIfCancellationRequested();
- string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
- result = new MediaServicesAccountCreateResponse();
- JToken responseDoc = null;
- if (string.IsNullOrEmpty(responseContent) == false)
- {
- responseDoc = JToken.Parse(responseContent);
- }
-
- if (responseDoc != null && responseDoc.Type != JTokenType.Null)
- {
- MediaServicesCreatedAccount accountInstance = new MediaServicesCreatedAccount();
- result.Account = accountInstance;
-
- JToken accountIdValue = responseDoc["AccountId"];
- if (accountIdValue != null && accountIdValue.Type != JTokenType.Null)
- {
- string accountIdInstance = ((string)accountIdValue);
- accountInstance.AccountId = accountIdInstance;
- }
-
- JToken accountNameValue = responseDoc["AccountName"];
- if (accountNameValue != null && accountNameValue.Type != JTokenType.Null)
- {
- string accountNameInstance = ((string)accountNameValue);
- accountInstance.AccountName = accountNameInstance;
- }
-
- JToken subscriptionValue = responseDoc["Subscription"];
- if (subscriptionValue != null && subscriptionValue.Type != JTokenType.Null)
- {
- string subscriptionInstance = ((string)subscriptionValue);
- accountInstance.SubscriptionId = subscriptionInstance;
- }
- }
-
- }
- result.StatusCode = statusCode;
- if (httpResponse.Headers.Contains("x-ms-request-id"))
- {
- result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
- }
-
- if (shouldTrace)
- {
- TracingAdapter.Exit(invocationId, result);
- }
- return result;
- }
- finally
- {
- if (httpResponse != null)
- {
- httpResponse.Dispose();
- }
- }
- }
- finally
- {
- if (httpRequest != null)
- {
- httpRequest.Dispose();
- }
- }
- }
-
- /// <summary>
- /// The Delete Media Services Account operation deletes an existing
- /// media services account in Windows Azure. (see
- /// http://msdn.microsoft.com/en-us/library/windowsazure/dn194273.aspx
- /// for more information)
- /// </summary>
- /// <param name='accountName'>
- /// Required. The name of the media services account.
- /// </param>
- /// <param name='cancellationToken'>
- /// Cancellation token.
- /// </param>
- /// <returns>
- /// A standard service response including an HTTP status code and
- /// request ID.
- /// </returns>
- public async Task<AzureOperationResponse> DeleteAsync(string accountName, CancellationToken cancellationToken)
- {
- // Validate
- if (accountName == null)
- {
- throw new ArgumentNullException("accountName");
- }
-
- // Tracing
- bool shouldTrace = TracingAdapter.IsEnabled;
- string invocationId = null;
- if (shouldTrace)
- {
- invocationId = TracingAdapter.NextInvocationId.ToString();
- Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
- tracingParameters.Add("accountName", accountName);
- TracingAdapter.Enter(invocationId, this, "DeleteAsync", tracingParameters);
- }
-
- // Construct URL
- string url = "";
- url = url + "/";
- if (this.Client.Credentials.SubscriptionId != null)
- {
- url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
- }
- url = url + "/services/mediaservices/Accounts/";
- url = url + Uri.EscapeDataString(accountName);
- string baseUrl = this.Client.BaseUri.AbsoluteUri;
- // Trim '/' character from the end of baseUrl and beginning of url.
- if (baseUrl[baseUrl.Length - 1] == '/')
- {
- baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
- }
- if (url[0] == '/')
- {
- url = url.Substring(1);
- }
- url = baseUrl + "/" + url;
- url = url.Replace(" ", "%20");
-
- // Create HTTP transport objects
- HttpRequestMessage httpRequest = null;
- try
- {
- httpRequest = new HttpRequestMessage();
- httpRequest.Method = HttpMethod.Delete;
- httpRequest.RequestUri = new Uri(url);
-
- // Set Headers
- httpRequest.Headers.Add("x-ms-version", "2011-10-01");
-
- // Set Credentials
- cancellationToken.ThrowIfCancellationRequested();
- await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
-
- // Send Request
- HttpResponseMessage httpResponse = null;
- try
- {
- if (shouldTrace)
- {
- TracingAdapter.SendRequest(invocationId, httpRequest);
- }
- cancellationToken.ThrowIfCancellationRequested();
- httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
- if (shouldTrace)
- {
- TracingAdapter.ReceiveResponse(invocationId, httpResponse);
- }
- HttpStatusCode statusCode = httpResponse.StatusCode;
- if (statusCode != HttpStatusCode.NoContent)
- {
- cancellationToken.ThrowIfCancellationRequested();
- CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
- if (shouldTrace)
- {
- TracingAdapter.Error(invocationId, ex);
- }
- throw ex;
- }
-
- // Create Result
- AzureOperationResponse result = null;
- // Deserialize Response
- result = new AzureOperationResponse();
- result.StatusCode = statusCode;
- if (httpResponse.Headers.Contains("x-ms-request-id"))
- {
- result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
- }
-
- if (shouldTrace)
- {
- TracingAdapter.Exit(invocationId, result);
- }
- return result;
- }
- finally
- {
- if (httpResponse != null)
- {
- httpResponse.Dispose();
- }
- }
- }
- finally
- {
- if (httpRequest != null)
- {
- httpRequest.Dispose();
- }
- }
- }
-
- /// <summary>
- /// The Get Media Services Account operation gets detailed information
- /// about a media services account in Windows Azure. (see
- /// http://msdn.microsoft.com/en-us/library/windowsazure/dn166974.aspx
- /// for more information)
- /// </summary>
- /// <param name='accountName'>
- /// Required. The name of the Media Services account.
- /// </param>
- /// <param name='cancellationToken'>
- /// Cancellation token.
- /// </param>
- /// <returns>
- /// The Get Media Services Account operation response.
- /// </returns>
- public async Task<MediaServicesAccountGetResponse> GetAsync(string accountName, CancellationToken cancellationToken)
- {
- // Validate
- if (accountName == null)
- {
- throw new ArgumentNullException("accountName");
- }
-
- // Tracing
- bool shouldTrace = TracingAdapter.IsEnabled;
- string invocationId = null;
- if (shouldTrace)
- {
- invocationId = TracingAdapter.NextInvocationId.ToString();
- Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
- tracingParameters.Add("accountName", accountName);
- TracingAdapter.Enter(invocationId, this, "GetAsync", tracingParameters);
- }
-
- // Construct URL
- string url = "";
- url = url + "/";
- if (this.Client.Credentials.SubscriptionId != null)
- {
- url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
- }
- url = url + "/services/mediaservices/Accounts/";
- url = url + Uri.EscapeDataString(accountName);
- string baseUrl = this.Client.BaseUri.AbsoluteUri;
- // Trim '/' character from the end of baseUrl and beginning of url.
- if (baseUrl[baseUrl.Length - 1] == '/')
- {
- baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
- }
- if (url[0] == '/')
- {
- url = url.Substring(1);
- }
- url = baseUrl + "/" + url;
- url = url.Replace(" ", "%20");
-
- // Create HTTP transport objects
- HttpRequestMessage httpRequest = null;
- try
- {
- httpRequest = new HttpRequestMessage();
- httpRequest.Method = HttpMethod.Get;
- httpRequest.RequestUri = new Uri(url);
-
- // Set Headers
- httpRequest.Headers.Add("x-ms-version", "2011-10-01");
-
- // Set Credentials
- cancellationToken.ThrowIfCancellationRequested();
- await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
-
- // Send Request
- HttpResponseMessage httpResponse = null;
- try
- {
- if (shouldTrace)
- {
- TracingAdapter.SendRequest(invocationId, httpRequest);
- }
- cancellationToken.ThrowIfCancellationRequested();
- httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
- if (shouldTrace)
- {
- TracingAdapter.ReceiveResponse(invocationId, httpResponse);
- }
- HttpStatusCode statusCode = httpResponse.StatusCode;
- if (statusCode != HttpStatusCode.OK)
- {
- cancellationToken.ThrowIfCancellationRequested();
- CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
- if (shouldTrace)
- {
- TracingAdapter.Error(invocationId, ex);
- }
- throw ex;
- }
-
- // Create Result
- MediaServicesAccountGetResponse result = null;
- // Deserialize Response
- if (statusCode == HttpStatusCode.OK)
- {
- cancellationToken.ThrowIfCancellationRequested();
- string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
- result = new MediaServicesAccountGetResponse();
- JToken responseDoc = null;
- if (string.IsNullOrEmpty(responseContent) == false)
- {
- responseDoc = JToken.Parse(responseContent);
- }
-
- if (responseDoc != null && responseDoc.Type != JTokenType.Null)
- {
- MediaServicesAccount accountInstance = new MediaServicesAccount();
- result.Account = accountInstance;
-
- JToken accountNameValue = responseDoc["AccountName"];
- if (accountNameValue != null && accountNameValue.Type != JTokenType.Null)
- {
- string accountNameInstance = ((string)accountNameValue);
- accountInstance.AccountName = accountNameInstance;
- }
-
- JToken accountKeyValue = responseDoc["AccountKey"];
- if (accountKeyValue != null && accountKeyValue.Type != JTokenType.Null)
- {
- string accountKeyInstance = ((string)accountKeyValue);
- accountInstance.AccountKey = accountKeyInstance;
- }
-
- JToken accountKeysValue = responseDoc["AccountKeys"];
- if (accountKeysValue != null && accountKeysValue.Type != JTokenType.Null)
- {
- MediaServicesAccount.AccountKeys accountKeysInstance = new MediaServicesAccount.AccountKeys();
- accountInstance.StorageAccountKeys = accountKeysInstance;
-
- JToken primaryValue = accountKeysValue["Primary"];
- if (primaryValue != null && primaryValue.Type != JTokenType.Null)
- {
- string primaryInstance = ((string)primaryValue);
- accountKeysInstance.Primary = primaryInstance;
- }
-
- JToken secondaryValue = accountKeysValue["Secondary"];
- if (secondaryValue != null && secondaryValue.Type != JTokenType.Null)
- {
- string secondaryInstance = ((string)secondaryValue);
- accountKeysInstance.Secondary = secondaryInstance;
- }
- }
-
- JToken accountRegionValue = responseDoc["AccountRegion"];
- if (accountRegionValue != null && accountRegionValue.Type != JTokenType.Null)
- {
- string accountRegionInstance = ((string)accountRegionValue);
- accountInstance.AccountRegion = accountRegionInstance;
- }
-
- JToken storageAccountNameValue = responseDoc["StorageAccountName"];
- if (storageAccountNameValue != null && storageAccountNameValue.Type != JTokenType.Null)
- {
- string storageAccountNameInstance = ((string)storageAccountNameValue);
- accountInstance.StorageAccountName = storageAccountNameInstance;
- }
- }
-
- }
- result.StatusCode = statusCode;
- if (httpResponse.Headers.Contains("x-ms-request-id"))
- {
- result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
- }
-
- if (shouldTrace)
- {
- TracingAdapter.Exit(invocationId, result);
- }
- return result;
- }
- finally
- {
- if (httpResponse != null)
- {
- httpResponse.Dispose();
- }
- }
- }
- finally
- {
- if (httpRequest != null)
- {
- httpRequest.Dispose();
- }
- }
- }
-
- /// <summary>
- /// The List Media Services Account operation gets information about
- /// all existing media services accounts associated with the current
- /// subscription in Windows Azure. (see
- /// http://msdn.microsoft.com/en-us/library/windowsazure/dn166989.aspx
- /// for more information)
- /// </summary>
- /// <param name='cancellationToken'>
- /// Cancellation token.
- /// </param>
- /// <returns>
- /// The List Media Accounts operation response.
- /// </returns>
- public async Task<MediaServicesAccountListResponse> ListAsync(CancellationToken cancellationToken)
- {
- // Validate
-
- // Tracing
- bool shouldTrace = TracingAdapter.IsEnabled;
- string invocationId = null;
- if (shouldTrace)
- {
- invocationId = TracingAdapter.NextInvocationId.ToString();
- Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
- TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters);
- }
-
- // Construct URL
- string url = "";
- url = url + "/";
- if (this.Client.Credentials.SubscriptionId != null)
- {
- url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
- }
- url = url + "/services/mediaservices/Accounts";
- string baseUrl = this.Client.BaseUri.AbsoluteUri;
- // Trim '/' character from the end of baseUrl and beginning of url.
- if (baseUrl[baseUrl.Length - 1] == '/')
- {
- baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
- }
- if (url[0] == '/')
- {
- url = url.Substring(1);
- }
- url = baseUrl + "/" + url;
- url = url.Replace(" ", "%20");
-
- // Create HTTP transport objects
- HttpRequestMessage httpRequest = null;
- try
- {
- httpRequest = new HttpRequestMessage();
- httpRequest.Method = HttpMethod.Get;
- httpRequest.RequestUri = new Uri(url);
-
- // Set Headers
- httpRequest.Headers.Add("x-ms-version", "2011-10-01");
-
- // Set Credentials
- cancellationToken.ThrowIfCancellationRequested();
- await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
-
- // Send Request
- HttpResponseMessage httpResponse = null;
- try
- {
- if (shouldTrace)
- {
- TracingAdapter.SendRequest(invocationId, httpRequest);
- }
- cancellationToken.ThrowIfCancellationRequested();
- httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
- if (shouldTrace)
- {
- TracingAdapter.ReceiveResponse(invocationId, httpResponse);
- }
- HttpStatusCode statusCode = httpResponse.StatusCode;
- if (statusCode != HttpStatusCode.OK)
- {
- cancellationToken.ThrowIfCancellationRequested();
- CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
- if (shouldTrace)
- {
- TracingAdapter.Error(invocationId, ex);
- }
- throw ex;
- }
-
- // Create Result
- MediaServicesAccountListResponse result = null;
- // Deserialize Response
- if (statusCode == HttpStatusCode.OK)
- {
- cancellationToken.ThrowIfCancellationRequested();
- string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
- result = new MediaServicesAccountListResponse();
- XDocument responseDoc = XDocument.Parse(responseContent);
-
- XElement serviceResourcesSequenceElement = responseDoc.Element(XName.Get("ServiceResources", "http://schemas.microsoft.com/windowsazure"));
- if (serviceResourcesSequenceElement != null)
- {
- foreach (XElement serviceResourcesElement in serviceResourcesSequenceElement.Elements(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure")))
- {
- MediaServicesAccountListResponse.MediaServiceAccount serviceResourceInstance = new MediaServicesAccountListResponse.MediaServiceAccount();
- result.Accounts.Add(serviceResourceInstance);
-
- XElement nameElement = serviceResourcesElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
- if (nameElement != null)
- {
- string nameInstance = nameElement.Value;
- serviceResourceInstance.Name = nameInstance;
- }
-
- XElement typeElement = serviceResourcesElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure"));
- if (typeElement != null)
- {
- string typeInstance = typeElement.Value;
- serviceResourceInstance.Type = typeInstance;
- }
-
- XElement stateElement = serviceResourcesElement.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure"));
- if (stateElement != null)
- {
- string stateInstance = stateElement.Value;
- serviceResourceInstance.State = stateInstance;
- }
-
- XElement selfLinkElement = serviceResourcesElement.Element(XName.Get("SelfLink", "http://schemas.microsoft.com/windowsazure"));
- if (selfLinkElement != null)
- {
- Uri selfLinkInstance = TypeConversion.TryParseUri(selfLinkElement.Value);
- serviceResourceInstance.Uri = selfLinkInstance;
- }
-
- XElement parentLinkElement = serviceResourcesElement.Element(XName.Get("ParentLink", "http://schemas.microsoft.com/windowsazure"));
- if (parentLinkElement != null)
- {
- Uri parentLinkInstance = TypeConversion.TryParseUri(parentLinkElement.Value);
- serviceResourceInstance.ParentUri = parentLinkInstance;
- }
-
- XElement accountIdElement = serviceResourcesElement.Element(XName.Get("AccountId", "http://schemas.microsoft.com/windowsazure"));
- if (accountIdElement != null)
- {
- string accountIdInstance = accountIdElement.Value;
- serviceResourceInstance.AccountId = accountIdInstance;
- }
- }
- }
-
- }
- result.StatusCode = statusCode;
- if (httpResponse.Headers.Contains("x-ms-request-id"))
- {
- result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
- }
-
- if (shouldTrace)
- {
- TracingAdapter.Exit(invocationId, result);
- }
- return result;
- }
- finally
- {
- if (httpResponse != null)
- {
- httpResponse.Dispose();
- }
- }
- }
- finally
- {
- if (httpRequest != null)
- {
- httpRequest.Dispose();
- }
- }
- }
-
- /// <summary>
- /// The Regenerate Media Services Account Key operation regenerates an
- /// account key for the given Media Services account in Windows Azure.
- /// (see
- /// http://msdn.microsoft.com/en-us/library/windowsazure/dn167010.aspx
- /// for more information)
- /// </summary>
- /// <param name='accountName'>
- /// Required. The name of the Media Services Account.
- /// </param>
- /// <param name='keyType'>
- /// Required. The type of key to regenerate (primary or secondary)
- /// </param>
- /// <param name='cancellationToken'>
- /// Cancellation token.
- /// </param>
- /// <returns>
- /// A standard service response including an HTTP status code and
- /// request ID.
- /// </returns>
- public async Task<AzureOperationResponse> RegenerateKeyAsync(string accountName, MediaServicesKeyType keyType, CancellationToken cancellationToken)
- {
- // Validate
- if (accountName == null)
- {
- throw new ArgumentNullException("accountName");
- }
-
- // Tracing
- bool shouldTrace = TracingAdapter.IsEnabled;
- string invocationId = null;
- if (shouldTrace)
- {
- invocationId = TracingAdapter.NextInvocationId.ToString();
- Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
- tracingParameters.Add("accountName", accountName);
- tracingParameters.Add("keyType", keyType);
- TracingAdapter.Enter(invocationId, this, "RegenerateKeyAsync", tracingParameters);
- }
-
- // Construct URL
- string url = "";
- url = url + "/";
- if (this.Client.Credentials.SubscriptionId != null)
- {
- url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
- }
- url = url + "/services/mediaservices/Accounts/";
- url = url + Uri.EscapeDataString(accountName);
- url = url + "/AccountKeys/";
- url = url + Uri.EscapeDataString(keyType.ToString());
- url = url + "/Regenerate";
- string baseUrl = this.Client.BaseUri.AbsoluteUri;
- // Trim '/' character from the end of baseUrl and beginning of url.
- if (baseUrl[baseUrl.Length - 1] == '/')
- {
- baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
- }
- if (url[0] == '/')
- {
- url = url.Substring(1);
- }
- url = baseUrl + "/" + url;
- url = url.Replace(" ", "%20");
-
- // Create HTTP transport objects
- HttpRequestMessage httpRequest = null;
- try
- {
- httpRequest = new HttpRequestMessage();
- httpRequest.Method = HttpMethod.Post;
- httpRequest.RequestUri = new Uri(url);
-
- // Set Headers
- httpRequest.Headers.Add("x-ms-version", "2011-10-01");
-
- // Set Credentials
- cancellationToken.ThrowIfCancellationRequested();
- await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
-
- // Send Request
- HttpResponseMessage httpResponse = null;
- try
- {
- if (shouldTrace)
- {
- TracingAdapter.SendRequest(invocationId, httpRequest);
- }
- cancellationToken.ThrowIfCancellationRequested();
- httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
- if (shouldTrace)
- {
- TracingAdapter.ReceiveResponse(invocationId, httpResponse);
- }
- HttpStatusCode statusCode = httpResponse.StatusCode;
- if (statusCode != HttpStatusCode.NoContent)
- {
- cancellationToken.ThrowIfCancellationRequested();
- CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
- if (shouldTrace)
- {
- TracingAdapter.Error(invocationId, ex);
- }
- throw ex;
- }
-
- // Create Result
- AzureOperationResponse result = null;
- // Deserialize Response
- result = new AzureOperationResponse();
- result.StatusCode = statusCode;
- if (httpResponse.Headers.Contains("x-ms-request-id"))
- {
- result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
- }
-
- if (shouldTrace)
- {
- TracingAdapter.Exit(invocationId, result);
- }
- return result;
- }
- finally
- {
- if (httpResponse != null)
- {
- httpResponse.Dispose();
- }
- }
- }
- finally
- {
- if (httpRequest != null)
- {
- httpRequest.Dispose();
- }
- }
- }
- }
- }