/// <summary>
        /// List all Product APIs.
        /// </summary>
        /// <param name='nextLink'>
        /// Required. NextLink from the previous successful call to List
        /// operation.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// List Api operations response details.
        /// </returns>
        public async Task <ApiListResponse> ListNextAsync(string nextLink, CancellationToken cancellationToken)
        {
            // Validate
            if (nextLink == null)
            {
                throw new ArgumentNullException("nextLink");
            }

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

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

            // Construct URL
            string url = "";

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

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

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            ApiPaged resultInstance = new ApiPaged();
                            result.Result = resultInstance;

                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    ApiContract apiContractInstance = new ApiContract();
                                    resultInstance.Values.Add(apiContractInstance);

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

                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        string nameInstance = ((string)nameValue);
                                        apiContractInstance.Name = nameInstance;
                                    }

                                    JToken descriptionValue = valueValue["description"];
                                    if (descriptionValue != null && descriptionValue.Type != JTokenType.Null)
                                    {
                                        string descriptionInstance = ((string)descriptionValue);
                                        apiContractInstance.Description = descriptionInstance;
                                    }

                                    JToken serviceUrlValue = valueValue["serviceUrl"];
                                    if (serviceUrlValue != null && serviceUrlValue.Type != JTokenType.Null)
                                    {
                                        string serviceUrlInstance = ((string)serviceUrlValue);
                                        apiContractInstance.ServiceUrl = serviceUrlInstance;
                                    }

                                    JToken pathValue = valueValue["path"];
                                    if (pathValue != null && pathValue.Type != JTokenType.Null)
                                    {
                                        string pathInstance = ((string)pathValue);
                                        apiContractInstance.Path = pathInstance;
                                    }

                                    JToken protocolsArray = valueValue["protocols"];
                                    if (protocolsArray != null && protocolsArray.Type != JTokenType.Null)
                                    {
                                        foreach (JToken protocolsValue in ((JArray)protocolsArray))
                                        {
                                            apiContractInstance.Protocols.Add(((ApiProtocolContract)Enum.Parse(typeof(ApiProtocolContract), ((string)protocolsValue), true)));
                                        }
                                    }

                                    JToken authenticationSettingsValue = valueValue["authenticationSettings"];
                                    if (authenticationSettingsValue != null && authenticationSettingsValue.Type != JTokenType.Null)
                                    {
                                        AuthenticationSettingsContract authenticationSettingsInstance = new AuthenticationSettingsContract();
                                        apiContractInstance.AuthenticationSettings = authenticationSettingsInstance;

                                        JToken oAuth2Value = authenticationSettingsValue["oAuth2"];
                                        if (oAuth2Value != null && oAuth2Value.Type != JTokenType.Null)
                                        {
                                            OAuth2AuthenticationSettingsContract oAuth2Instance = new OAuth2AuthenticationSettingsContract();
                                            authenticationSettingsInstance.OAuth2 = oAuth2Instance;

                                            JToken authorizationServerIdValue = oAuth2Value["authorizationServerId"];
                                            if (authorizationServerIdValue != null && authorizationServerIdValue.Type != JTokenType.Null)
                                            {
                                                string authorizationServerIdInstance = ((string)authorizationServerIdValue);
                                                oAuth2Instance.AuthorizationServerId = authorizationServerIdInstance;
                                            }

                                            JToken scopeValue = oAuth2Value["scope"];
                                            if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                            {
                                                string scopeInstance = ((string)scopeValue);
                                                oAuth2Instance.Scope = scopeInstance;
                                            }
                                        }
                                    }

                                    JToken subscriptionKeyParameterNamesValue = valueValue["subscriptionKeyParameterNames"];
                                    if (subscriptionKeyParameterNamesValue != null && subscriptionKeyParameterNamesValue.Type != JTokenType.Null)
                                    {
                                        SubscriptionKeyParameterNamesContract subscriptionKeyParameterNamesInstance = new SubscriptionKeyParameterNamesContract();
                                        apiContractInstance.SubscriptionKeyParameterNames = subscriptionKeyParameterNamesInstance;

                                        JToken headerValue = subscriptionKeyParameterNamesValue["header"];
                                        if (headerValue != null && headerValue.Type != JTokenType.Null)
                                        {
                                            string headerInstance = ((string)headerValue);
                                            subscriptionKeyParameterNamesInstance.Header = headerInstance;
                                        }

                                        JToken queryValue = subscriptionKeyParameterNamesValue["query"];
                                        if (queryValue != null && queryValue.Type != JTokenType.Null)
                                        {
                                            string queryInstance = ((string)queryValue);
                                            subscriptionKeyParameterNamesInstance.Query = queryInstance;
                                        }
                                    }

                                    JToken typeValue = valueValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        ApiTypeContract typeInstance = ((ApiTypeContract)Enum.Parse(typeof(ApiTypeContract), ((string)typeValue), true));
                                        apiContractInstance.Type = typeInstance;
                                    }
                                }
                            }

                            JToken countValue = responseDoc["count"];
                            if (countValue != null && countValue.Type != JTokenType.Null)
                            {
                                long countInstance = ((long)countValue);
                                resultInstance.TotalCount = countInstance;
                            }

                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                resultInstance.NextLink = nextLinkInstance;
                            }
                        }
                    }
                    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>
        /// List all Product APIs.
        /// </summary>
        /// <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='pid'>
        /// Required. Identifier of the product.
        /// </param>
        /// <param name='query'>
        /// Optional.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// List Api operations response details.
        /// </returns>
        public async Task <ApiListResponse> ListAsync(string resourceGroupName, string serviceName, string pid, QueryParameters query, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (serviceName == null)
            {
                throw new ArgumentNullException("serviceName");
            }
            if (pid == null)
            {
                throw new ArgumentNullException("pid");
            }

            // 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("pid", pid);
                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 + "/products/";
            url = url + Uri.EscapeDataString(pid);
            url = url + "/apis";
            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
                    ApiListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

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

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            ApiPaged resultInstance = new ApiPaged();
                            result.Result = resultInstance;

                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    ApiContract apiContractInstance = new ApiContract();
                                    resultInstance.Values.Add(apiContractInstance);

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

                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        string nameInstance = ((string)nameValue);
                                        apiContractInstance.Name = nameInstance;
                                    }

                                    JToken descriptionValue = valueValue["description"];
                                    if (descriptionValue != null && descriptionValue.Type != JTokenType.Null)
                                    {
                                        string descriptionInstance = ((string)descriptionValue);
                                        apiContractInstance.Description = descriptionInstance;
                                    }

                                    JToken serviceUrlValue = valueValue["serviceUrl"];
                                    if (serviceUrlValue != null && serviceUrlValue.Type != JTokenType.Null)
                                    {
                                        string serviceUrlInstance = ((string)serviceUrlValue);
                                        apiContractInstance.ServiceUrl = serviceUrlInstance;
                                    }

                                    JToken pathValue = valueValue["path"];
                                    if (pathValue != null && pathValue.Type != JTokenType.Null)
                                    {
                                        string pathInstance = ((string)pathValue);
                                        apiContractInstance.Path = pathInstance;
                                    }

                                    JToken protocolsArray = valueValue["protocols"];
                                    if (protocolsArray != null && protocolsArray.Type != JTokenType.Null)
                                    {
                                        foreach (JToken protocolsValue in ((JArray)protocolsArray))
                                        {
                                            apiContractInstance.Protocols.Add(((ApiProtocolContract)Enum.Parse(typeof(ApiProtocolContract), ((string)protocolsValue), true)));
                                        }
                                    }

                                    JToken authenticationSettingsValue = valueValue["authenticationSettings"];
                                    if (authenticationSettingsValue != null && authenticationSettingsValue.Type != JTokenType.Null)
                                    {
                                        AuthenticationSettingsContract authenticationSettingsInstance = new AuthenticationSettingsContract();
                                        apiContractInstance.AuthenticationSettings = authenticationSettingsInstance;

                                        JToken oAuth2Value = authenticationSettingsValue["oAuth2"];
                                        if (oAuth2Value != null && oAuth2Value.Type != JTokenType.Null)
                                        {
                                            OAuth2AuthenticationSettingsContract oAuth2Instance = new OAuth2AuthenticationSettingsContract();
                                            authenticationSettingsInstance.OAuth2 = oAuth2Instance;

                                            JToken authorizationServerIdValue = oAuth2Value["authorizationServerId"];
                                            if (authorizationServerIdValue != null && authorizationServerIdValue.Type != JTokenType.Null)
                                            {
                                                string authorizationServerIdInstance = ((string)authorizationServerIdValue);
                                                oAuth2Instance.AuthorizationServerId = authorizationServerIdInstance;
                                            }

                                            JToken scopeValue = oAuth2Value["scope"];
                                            if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                            {
                                                string scopeInstance = ((string)scopeValue);
                                                oAuth2Instance.Scope = scopeInstance;
                                            }
                                        }
                                    }

                                    JToken subscriptionKeyParameterNamesValue = valueValue["subscriptionKeyParameterNames"];
                                    if (subscriptionKeyParameterNamesValue != null && subscriptionKeyParameterNamesValue.Type != JTokenType.Null)
                                    {
                                        SubscriptionKeyParameterNamesContract subscriptionKeyParameterNamesInstance = new SubscriptionKeyParameterNamesContract();
                                        apiContractInstance.SubscriptionKeyParameterNames = subscriptionKeyParameterNamesInstance;

                                        JToken headerValue = subscriptionKeyParameterNamesValue["header"];
                                        if (headerValue != null && headerValue.Type != JTokenType.Null)
                                        {
                                            string headerInstance = ((string)headerValue);
                                            subscriptionKeyParameterNamesInstance.Header = headerInstance;
                                        }

                                        JToken queryValue = subscriptionKeyParameterNamesValue["query"];
                                        if (queryValue != null && queryValue.Type != JTokenType.Null)
                                        {
                                            string queryInstance = ((string)queryValue);
                                            subscriptionKeyParameterNamesInstance.Query = queryInstance;
                                        }
                                    }

                                    JToken typeValue = valueValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        ApiTypeContract typeInstance = ((ApiTypeContract)Enum.Parse(typeof(ApiTypeContract), ((string)typeValue), true));
                                        apiContractInstance.Type = typeInstance;
                                    }
                                }
                            }

                            JToken countValue = responseDoc["count"];
                            if (countValue != null && countValue.Type != JTokenType.Null)
                            {
                                long countInstance = ((long)countValue);
                                resultInstance.TotalCount = countInstance;
                            }

                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                resultInstance.NextLink = nextLinkInstance;
                            }
                        }
                    }
                    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();
                }
            }
        }