<commit_before><commit_msg>:tractor: :sparkles: added HttpRequestMessageExtensions, resolves #18, issue #20<commit_after>﻿#if NETSTANDARD

using System;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Songhay.Extensions
{
    /// <summary>
    /// Extensions of <see cref="HttpRequestMessage"/>
    /// </summary>
    public static class HttpRequestMessageExtensions
    {
        /// <summary>
        /// Gets a <see cref="string"/> from the derived <see cref="HttpResponseMessage"/>.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static async Task<string> GetServerResponseAsync(this HttpRequestMessage request)
        {
            if (request == null) throw new NullReferenceException($"The expected {nameof(HttpRequestMessage)} is not here.");

            var response = await GetHttpClient().SendAsync(request);

            var content = await response.Content.ReadAsStringAsync();
            return content;

        }

        /// <summary>
        /// Calls <see cref="HttpClient.SendAsync(HttpRequestMessage)" />
        /// </summary>
        /// <param name="request"></param>
        /// <param name="requestMessageAction">The request message action.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">uri - The expected URI is not here.
        /// or
        /// mediaType - The expected request body media type is not here.</exception>
        public static async Task<HttpResponseMessage> SendAsync(this HttpRequestMessage request, Action<HttpRequestMessage> requestMessageAction)
        {
            if (request == null) throw new NullReferenceException($"The expected {nameof(HttpRequestMessage)} is not here.");

            requestMessageAction?.Invoke(request);

            var response = await GetHttpClient().SendAsync(request);
            return response;
        }

        /// <summary>
        /// Calls <see cref="HttpClient.SendAsync(HttpRequestMessage)" />
        /// with the specified request body.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="requestBody">The request body.</param>
        /// <param name="encoding">The encoding.</param>
        /// <param name="mediaType">Type of the media.</param>
        /// <param name="requestMessageAction">The request message action.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">requestBody - The expected request body is not here.</exception>
        public static async Task<HttpResponseMessage> SendBodyAsync(this HttpRequestMessage request, string requestBody, Encoding encoding, string mediaType, Action<HttpRequestMessage> requestMessageAction)
        {
            if (request == null) throw new NullReferenceException($"The expected {nameof(HttpRequestMessage)} is not here.");
            if (string.IsNullOrEmpty(requestBody)) throw new ArgumentNullException(nameof(requestBody), "The expected request body is not here.");
            if (string.IsNullOrEmpty(mediaType)) throw new ArgumentNullException(nameof(mediaType), "The expected request body media type is not here.");

            request.Content = new StringContent(requestBody, encoding, mediaType);

            requestMessageAction?.Invoke(request);

            var response = await GetHttpClient().SendAsync(request);
            return response;
        }

        private static HttpClient GetHttpClient() => HttpClientLazy.Value;

        private static readonly Lazy<HttpClient> HttpClientLazy =
            new Lazy<HttpClient>(() => new HttpClient(), LazyThreadSafetyMode.PublicationOnly);
    }
}

#endif
{'fraction_non_alphanumeric': 0.1205141938939475, 'fraction_numerical': 0.0010712372790573112, 'mean_word_length': 2.8544891640866874, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 51, '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': '3310410', 'n_tokens_mistral': 904, 'n_tokens_neox': 889, 'n_words': 251}