/// <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'>
        /// 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 System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.MediaServices.Models.MediaServicesAccountGetResponse> GetAsync(string accountName, CancellationToken cancellationToken)
        {
            // Validate
            if (accountName == null)
            {
                throw new ArgumentNullException("accountName");
            }

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = Tracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("accountName", accountName);
                Tracing.Enter(invocationId, this, "GetAsync", tracingParameters);
            }

            // Construct URL
            string url = new Uri(this.Client.BaseUri, "/").ToString() + this.Client.Credentials.SubscriptionId + "/services/mediaservices/Accounts/" + accountName;

            // 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)
                    {
                        Tracing.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        Tracing.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), CloudExceptionType.Json);
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    MediaServicesAccountGetResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new MediaServicesAccountGetResponse();
                    JToken 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)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
Esempio n. 2
0
        /// <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();
                }
            }
        }