/// <summary>
        /// Parses a URL and turns it into authority and discovery endpoint URL.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException">
        /// Malformed URL
        /// </exception>
        public static ConsentDiscoveryEndpoint ParseUrl(string input)
        {
            var success = Uri.TryCreate(input, UriKind.Absolute, out var uri);

            if (success == false)
            {
                throw new InvalidOperationException("Malformed URL");
            }

            if (!ConsentDiscoveryEndpoint.IsValidScheme(uri))
            {
                throw new InvalidOperationException("Malformed URL");
            }

            var url = input.RemoveTrailingSlash();

            if (url.EndsWith(Constants.Discovery.DiscoveryEndpoint, StringComparison.OrdinalIgnoreCase))
            {
                return(new ConsentDiscoveryEndpoint(url.Substring(0, url.Length - Constants.Discovery.DiscoveryEndpoint.Length - 1), url));
            }
            else
            {
                return(new ConsentDiscoveryEndpoint(url, url.EnsureTrailingSlash() + Constants.Discovery.DiscoveryEndpoint));
            }
        }
        /// <summary>
        /// Sends a discovery document request
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public static async Task <ConsentDiscoveryDocumentResponse> GetDiscoveryDocumentAsync(this HttpMessageInvoker client,
                                                                                              ConsentDiscoveryDocumentRequest request,
                                                                                              CancellationToken cancellationToken = default)
        {
            string address;

            if (request.Address.IsPresent())
            {
                address = request.Address;
            }
            else if (client is HttpClient)
            {
                address = ((HttpClient)client).BaseAddress.AbsoluteUri;
            }
            else
            {
                throw new ArgumentException("An address is required.");
            }

            var parsed    = ConsentDiscoveryEndpoint.ParseUrl(address);
            var authority = parsed.Authority;
            var url       = parsed.Url;

            if (request.Policy.Authority.IsMissing())
            {
                request.Policy.Authority = authority;
            }

            string jwkUrl = "";

            if (!ConsentDiscoveryEndpoint.IsSecureScheme(new Uri(url), request.Policy))
            {
                return(ConsentProtocolResponse.FromException <ConsentDiscoveryDocumentResponse>(new InvalidOperationException("HTTPS required"), $"Error connecting to {url}. HTTPS required."));
            }

            try
            {
                var clone = request.Clone();

                clone.Method = HttpMethod.Get;
                clone.Prepare();

                clone.RequestUri = new Uri(url);

                var response = await client.SendAsync(clone, cancellationToken).ConfigureAwait();

                string responseContent = null;

                if (response.Content != null)
                {
                    responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait();
                }

                if (!response.IsSuccessStatusCode)
                {
                    return(await ConsentProtocolResponse.FromHttpResponseAsync <ConsentDiscoveryDocumentResponse>(response, $"Error connecting to {url}: {response.ReasonPhrase}").ConfigureAwait());
                }

                var disco = await ConsentProtocolResponse.FromHttpResponseAsync <ConsentDiscoveryDocumentResponse>(response, request.Policy).ConfigureAwait();

                return(disco);
            }
            catch (Exception ex)
            {
                return(ConsentProtocolResponse.FromException <ConsentDiscoveryDocumentResponse>(ex, $"Error connecting to {url}. {ex.Message}."));
            }
        }