/// <summary>
 /// Gets a list of resource providers.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Internal.Management.Csm.IProviderOperations.
 /// </param>
 /// <param name='parameters'>
 /// Optional. Query parameters. If null is passed returns all
 /// deployments.
 /// </param>
 /// <returns>
 /// List of resource providers.
 /// </returns>
 public static ProviderListResult List(this IProviderOperations operations, ProviderListParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IProviderOperations)s).ListAsync(parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
        /// <summary>
        /// Gets a list of resource providers.
        /// </summary>
        /// <param name='parameters'>
        /// Optional. Query parameters. If null is passed returns all
        /// deployments.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// List of resource providers.
        /// </returns>
        public async Task <ProviderListResult> ListAsync(ProviderListParameters parameters, 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>();
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters);
            }

            // Construct URL
            string url = "/subscriptions/" + (this.Client.Credentials.SubscriptionId == null ? "" : Uri.EscapeDataString(this.Client.Credentials.SubscriptionId)) + "/providers?";

            if (parameters != null && parameters.Top != null)
            {
                url = url + "$top=" + Uri.EscapeDataString(parameters.Top.Value.ToString());
            }
            url = url + "&api-version=2014-04-01-preview";
            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

                // 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
                    ProviderListResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new ProviderListResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    Provider providerInstance = new Provider();
                                    result.Providers.Add(providerInstance);

                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        providerInstance.Id = idInstance;
                                    }

                                    JToken namespaceValue = valueValue["namespace"];
                                    if (namespaceValue != null && namespaceValue.Type != JTokenType.Null)
                                    {
                                        string namespaceInstance = ((string)namespaceValue);
                                        providerInstance.Namespace = namespaceInstance;
                                    }

                                    JToken registrationStateValue = valueValue["registrationState"];
                                    if (registrationStateValue != null && registrationStateValue.Type != JTokenType.Null)
                                    {
                                        string registrationStateInstance = ((string)registrationStateValue);
                                        providerInstance.RegistrationState = registrationStateInstance;
                                    }

                                    JToken resourceTypesArray = valueValue["resourceTypes"];
                                    if (resourceTypesArray != null && resourceTypesArray.Type != JTokenType.Null)
                                    {
                                        foreach (JToken resourceTypesValue in ((JArray)resourceTypesArray))
                                        {
                                            ProviderResourceType providerResourceTypeInstance = new ProviderResourceType();
                                            providerInstance.ResourceTypes.Add(providerResourceTypeInstance);

                                            JToken resourceTypeValue = resourceTypesValue["resourceType"];
                                            if (resourceTypeValue != null && resourceTypeValue.Type != JTokenType.Null)
                                            {
                                                string resourceTypeInstance = ((string)resourceTypeValue);
                                                providerResourceTypeInstance.Name = resourceTypeInstance;
                                            }

                                            JToken locationsArray = resourceTypesValue["locations"];
                                            if (locationsArray != null && locationsArray.Type != JTokenType.Null)
                                            {
                                                foreach (JToken locationsValue in ((JArray)locationsArray))
                                                {
                                                    providerResourceTypeInstance.Locations.Add(((string)locationsValue));
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            JToken odatanextLinkValue = responseDoc["@odata.nextLink"];
                            if (odatanextLinkValue != null && odatanextLinkValue.Type != JTokenType.Null)
                            {
                                string odatanextLinkInstance = ((string)odatanextLinkValue);
                                result.NextLink = odatanextLinkInstance;
                            }
                        }
                    }
                    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>
 /// Gets a list of resource providers.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Internal.Management.Csm.IProviderOperations.
 /// </param>
 /// <param name='parameters'>
 /// Optional. Query parameters. If null is passed returns all
 /// deployments.
 /// </param>
 /// <returns>
 /// List of resource providers.
 /// </returns>
 public static Task <ProviderListResult> ListAsync(this IProviderOperations operations, ProviderListParameters parameters)
 {
     return(operations.ListAsync(parameters, CancellationToken.None));
 }