<commit_before>﻿using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Net;

namespace LoremIpsum
{
    public static class LoremIpsumUtil
    {
        public static readonly string LIPSUM_JSON_URL = "http://lipsum.com/feed/json?amount={0}&start={1}&what={2}";

        /// <summary>
        /// Gets a new Lorem Ipsum result object from the JSON endpoint at http://lipsum.com.
        /// </summary>
        /// <param name="lipsumType">The type of result to get from the api. See <see cref="LipsumType"/></param>
        /// <param name="amount">An integer value representing how many of the given type to receive from the service.</param>
        /// <param name="startWithLoremIpsum">true to start with the "Lorem Ipsum" text or false to get a random result.</param>
        /// <param name="proxyCredentials">Use <see cref="System.Net.NetworkCredential"/>, <see cref="System.Net.CredentialCache.DefaultCredentials"/>, or <see cref="System.Net.CredentialCache.DefaultNetworkCredentials"/> to send to your proxy server.</param>
        /// <returns>An instance of the <see cref="LoremIpsum"/> class representing the data that was returned from the api.</returns>
        public static LoremIpsum GetNewLipsum(LipsumType lipsumType, int amount, bool startWithLoremIpsum = true, ICredentials proxyCredentials = null)
        {
            string requestUrl = string.Format(LIPSUM_JSON_URL, amount, startWithLoremIpsum ? "yes" : "no", lipsumType.GetDescription());

            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;

            if (proxyCredentials != null)
            {
                request.Proxy.Credentials = proxyCredentials;
            }

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                    throw new Exception(String.Format(
                    "Server error (HTTP {0}: {1}).",
                    response.StatusCode,
                    response.StatusDescription));

                StreamReader sr = new StreamReader(response.GetResponseStream());

                return JsonConvert.DeserializeObject<LoremIpsum>(sr.ReadToEnd());
            }
        }

        /// <summary>
        /// The type of result to retrieve from the JSON endpoint.
        /// </summary>
        public enum LipsumType
        {
            [Description("words")]
            Words,
            [Description("paras")]
            Paragraphs,
            [Description("bytes")]
            Bytes,
            [Description("lists")]
            Lists
        }

        /// <summary>
        /// Retrieve the description value from this enum.
        /// </summary><commit_msg>Added an async option and modified the Json serialization to use the method that is not deprecated.<commit_after>﻿using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace LoremIpsum
{
    public static class LoremIpsumUtil
    {
        public static readonly string LIPSUM_JSON_URL = "http://lipsum.com/feed/json?amount={0}&start={1}&what={2}";

        /// <summary>
        /// Gets a new Lorem Ipsum result object from the JSON endpoint at http://lipsum.com.
        /// </summary>
        /// <param name="lipsumType">The type of result to get from the api. See <see cref="LipsumType"/></param>
        /// <param name="amount">An integer value representing how many of the given type to receive from the service.</param>
        /// <param name="startWithLoremIpsum">true to start with the "Lorem Ipsum" text or false to get a random result.</param>
        /// <param name="proxyCredentials">Use <see cref="System.Net.NetworkCredential"/>, <see cref="System.Net.CredentialCache.DefaultCredentials"/>, or <see cref="System.Net.CredentialCache.DefaultNetworkCredentials"/> to send to your proxy server.</param>
        /// <returns>An instance of the <see cref="LoremIpsum"/> class representing the data that was returned from the api.</returns>
        public static LoremIpsum GetNewLipsum(LipsumType lipsumType, int amount, bool startWithLoremIpsum = true, ICredentials proxyCredentials = null)
        {
            string requestUrl = string.Format(LIPSUM_JSON_URL, amount, startWithLoremIpsum ? "yes" : "no", lipsumType.GetDescription());

            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;

            if (proxyCredentials != null)
            {
                request.Proxy.Credentials = proxyCredentials;
            }

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                    throw new Exception(String.Format(
                    "Server error (HTTP {0}: {1}).",
                    response.StatusCode,
                    response.StatusDescription));

                StreamReader sr = new StreamReader(response.GetResponseStream());
                JsonTextReader tr = new JsonTextReader(sr);
                JsonSerializer ser = new JsonSerializer();

                return ser.Deserialize<LoremIpsum>(tr);
            }
        }

        public async static Task<LoremIpsum> GetNewLipsumAsync(LipsumType lipsumType, int amount, bool startWithLoremIpsum = true, ICredentials proxyCredentials = null)
        {
            string requestUrl = string.Format(LIPSUM_JSON_URL, amount, startWithLoremIpsum ? "yes" : "no", lipsumType.GetDescription());

            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;

            if (proxyCredentials != null)
            {
                request.Proxy.Credentials = proxyCredentials;
            }

            var response = await request.GetResponseAsync();

            StreamReader sr = new StreamReader(response.GetResponseStream());
            JsonTextReader tr = new JsonTextReader(sr);
            JsonSerializer ser = new JsonSerializer();

            return ser.Deserialize<LoremIpsum>(tr);
        }

        /// <summary>
        /// The type of result to retrieve from the JSON endpoint.
        /// </summary>
        public enum LipsumType
        {
            [Description("words")]
            Words,
            [Description("paras")]
            Paragraphs,
            [Description("bytes")]
            Bytes,
            [Description("lists")]
            Lists
        }

        /// <summary>
        /// Retrieve the description value from this enum.
        /// </summary>
{'fraction_non_alphanumeric': 0.10360832459949094, 'fraction_numerical': 0.0014972301242701003, 'mean_word_length': 2.308568598315998, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, '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': '3198146', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1679, 'n_words': 481}