<commit_before>﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace TypedRest
{
    /// <summary>
    /// Entry point to a REST interface. Derive from this class and add your own set of child-<see cref="IEndpoint"/>s as properties.
    /// </summary>
    public class EntryEndpoint : EndpointBase
    {
        /// <summary>
        /// Creates a new REST interface.
        /// </summary>
        /// <param name="uri">The base URI of the REST interface. Missing trailing slash will be appended automatically.</param>
        /// <param name="credentials">The credentials used to authenticate against the REST interface. Can be <c>null</c> for no authentication.</param>
        public EntryEndpoint(Uri uri, ICredentials credentials = null)
            : base(BuildHttpClient(credentials), uri.EnsureTrailingSlash())
        {
        }

        private static HttpClient BuildHttpClient(ICredentials credentials)
        {
            return new HttpClient((credentials == null)
                ? new HttpClientHandler()
                : new HttpClientHandler {PreAuthenticate = true, Credentials = credentials});
        }
<commit_msg>Preemptively set HTTP Basic Auth header when appropriate<commit_after>﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TypedRest
{
    /// <summary>
    /// Entry point to a REST interface. Derive from this class and add your own set of child-<see cref="IEndpoint"/>s as properties.
    /// </summary>
    public class EntryEndpoint : EndpointBase
    {
        /// <summary>
        /// Creates a new REST interface.
        /// </summary>
        /// <param name="uri">The base URI of the REST interface. Missing trailing slash will be appended automatically.</param>
        /// <param name="credentials">The credentials used to authenticate against the REST interface. Can be <c>null</c> for no authentication.</param>
        public EntryEndpoint(Uri uri, ICredentials credentials = null)
            : base(BuildHttpClient(uri, credentials), uri.EnsureTrailingSlash())
        {
        }

        private static HttpClient BuildHttpClient(Uri uri, ICredentials credentials)
        {
            if (credentials == null) return new HttpClient();

            var handler = new HttpClientHandler {PreAuthenticate = true, Credentials = credentials};

            var basicCredentials = credentials.GetCredential(uri, "Basic");
            if (basicCredentials == null) return new HttpClient(handler);

            return new HttpClient(handler)
            {
                DefaultRequestHeaders =
                {
                    // Preemptively set HTTP Basic Auth header when appropriate
                    Authorization = new AuthenticationHeaderValue(
                        "Basic",
                        Convert.ToBase64String(
                            Encoding.ASCII.GetBytes(basicCredentials.UserName + ":" + basicCredentials.Password)))
                }
            };
        }

{'fraction_non_alphanumeric': 0.09388646288209607, 'fraction_numerical': 0.0006238303181534623, 'mean_word_length': 2.3686974789915967, 'pattern_counts': {'":': 1, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3797438', 'n_tokens_mistral': 788, 'n_tokens_neox': 766, 'n_words': 251}