/// <summary>
 /// Convert an enum of type IdentityProviderTypeContract to a string.
 /// </summary>
 /// <param name='value'>
 /// The value to convert to a string.
 /// </param>
 /// <returns>
 /// The enum value as a string.
 /// </returns>
 internal static string IdentityProviderTypeContractToString(IdentityProviderTypeContract value)
 {
     if (value == IdentityProviderTypeContract.Facebook)
     {
         return("facebook");
     }
     if (value == IdentityProviderTypeContract.Google)
     {
         return("google");
     }
     if (value == IdentityProviderTypeContract.Microsoft)
     {
         return("microsoft");
     }
     if (value == IdentityProviderTypeContract.Twitter)
     {
         return("twitter");
     }
     if (value == IdentityProviderTypeContract.Aad)
     {
         return("aad");
     }
     throw new ArgumentOutOfRangeException("value");
 }
        /// <param name='resourceGroupName'>
        /// Required. The name of the resource group.
        /// </param>
        /// <param name='serviceName'>
        /// Required. The name of the Api Management service.
        /// </param>
        /// <param name='query'>
        /// Optional.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// List IdentityProvider operation response details.
        /// </returns>
        public async Task <IdentityProviderListResponse> ListAsync(string resourceGroupName, string serviceName, QueryParameters query, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (serviceName == null)
            {
                throw new ArgumentNullException("serviceName");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("resourceGroupName", resourceGroupName);
                tracingParameters.Add("serviceName", serviceName);
                tracingParameters.Add("query", query);
                TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/resourceGroups/";
            url = url + Uri.EscapeDataString(resourceGroupName);
            url = url + "/providers/";
            url = url + "Microsoft.ApiManagement";
            url = url + "/service/";
            url = url + Uri.EscapeDataString(serviceName);
            url = url + "/identityProviders";
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2016-10-10");
            List <string> odataFilter = new List <string>();

            if (query != null && query.Filter != null)
            {
                odataFilter.Add(Uri.EscapeDataString(query.Filter));
            }
            if (odataFilter.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter));
            }
            if (query != null && query.Top != null)
            {
                queryParameters.Add("$top=" + Uri.EscapeDataString(query.Top.Value.ToString()));
            }
            if (query != null && query.Skip != null)
            {
                queryParameters.Add("$skip=" + Uri.EscapeDataString(query.Skip.Value.ToString()));
            }
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            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
                    IdentityProviderListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

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

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken resultArray = responseDoc;
                            if (resultArray != null && resultArray.Type != JTokenType.Null)
                            {
                                foreach (JToken resultValue in ((JArray)resultArray))
                                {
                                    IdentityProviderContract identityProviderContractInstance = new IdentityProviderContract();
                                    result.Result.Add(identityProviderContractInstance);

                                    JToken typeValue = resultValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        IdentityProviderTypeContract typeInstance = ApiManagementClient.ParseIdentityProviderTypeContract(((string)typeValue));
                                        identityProviderContractInstance.Type = typeInstance;
                                    }

                                    JToken clientIdValue = resultValue["clientId"];
                                    if (clientIdValue != null && clientIdValue.Type != JTokenType.Null)
                                    {
                                        string clientIdInstance = ((string)clientIdValue);
                                        identityProviderContractInstance.ClientId = clientIdInstance;
                                    }

                                    JToken clientSecretValue = resultValue["clientSecret"];
                                    if (clientSecretValue != null && clientSecretValue.Type != JTokenType.Null)
                                    {
                                        string clientSecretInstance = ((string)clientSecretValue);
                                        identityProviderContractInstance.ClientSecret = clientSecretInstance;
                                    }

                                    JToken allowedTenantsArray = resultValue["allowedTenants"];
                                    if (allowedTenantsArray != null && allowedTenantsArray.Type != JTokenType.Null)
                                    {
                                        foreach (JToken allowedTenantsValue in ((JArray)allowedTenantsArray))
                                        {
                                            identityProviderContractInstance.AllowedTenants.Add(((string)allowedTenantsValue));
                                        }
                                    }
                                }
                            }
                        }
                    }
                    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();
                }
            }
        }