<commit_before>﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;

namespace AskMonaWrapper
{
    public partial class AskMonaApi
    {
        internal async Task<T> CallAsync<T>(string url, Dictionary<string, string> prms)
        {
            var sb = new StringBuilder();
            foreach (var prm in prms)
            {
                if (prm.Value != null)
                    sb.Append(String.Format("{0}={1}&", prm.Key, System.Net.WebUtility.UrlEncode(prm.Value)));
            }

            try
            {
                var ub = new UriBuilder(url);
                ub.Query = sb.ToString().TrimEnd('&');
                var wc = new WebClientWithTimeout();
                var res = await wc.DownloadDataTaskAsync(ub.Uri);
                var serializer = new DataContractJsonSerializer(typeof(T));
                return (T)serializer.ReadObject(new MemoryStream(res));
            }
            catch { }

            return default(T);
        }

        internal async Task<T> CallAuthAsync<T>(string url, Dictionary<string, string> prms)
        {
            var authKey = await GenerateAuthorizationKey();
            if (authKey == null)
                return default(T);

            prms.Add("app_id", mApplicationId);
            prms.Add("u_id", mAccount.UserId.ToString());
            prms.Add("nonce", authKey.Nonce);
            prms.Add("time", authKey.Time);
            prms.Add("auth_key", authKey.Key);

            return await CallAsync<T>(url, prms);
        }

        internal static string GenerateNonce(int length)
        {
            var random = new Random();
            var nonceString = new StringBuilder();
            for (int i = 0; i < length; i++)
                nonceString.Append(mValidChars[random.Next(0, mValidChars.Length - 1)]);

            return nonceString.ToString();
        }

        internal async Task<AuthorizationKey> GenerateAuthorizationKey()
        {
            if (String.IsNullOrEmpty(mAccount.SecretKey) && 
                !String.IsNullOrEmpty(mAccount.Address) && !String.IsNullOrEmpty(mAccount.Password))
            {
                var secretKey = await FetchSecretKey(mAccount.Address, mAccount.Password);
                if (secretKey == null)
                    return null;

                mAccount.UserId = secretKey.UserId;
                mAccount.SecretKey = secretKey.Key;
            }

            var nonce = "";<commit_msg>実装の隠蔽<commit_after>﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;

namespace AskMonaWrapper
{
    public partial class AskMonaApi
    {
        private async Task<T> CallAsync<T>(string url, Dictionary<string, string> prms)
        {
            var sb = new StringBuilder();
            foreach (var prm in prms)
            {
                if (prm.Value != null)
                    sb.Append(String.Format("{0}={1}&", prm.Key, System.Net.WebUtility.UrlEncode(prm.Value)));
            }

            try
            {
                var ub = new UriBuilder(url);
                ub.Query = sb.ToString().TrimEnd('&');
                var wc = new WebClientWithTimeout();
                var res = await wc.DownloadDataTaskAsync(ub.Uri);
                var serializer = new DataContractJsonSerializer(typeof(T));
                return (T)serializer.ReadObject(new MemoryStream(res));
            }
            catch { }

            return default(T);
        }

        private async Task<T> CallAuthAsync<T>(string url, Dictionary<string, string> prms)
        {
            var authKey = await GenerateAuthorizationKey();
            if (authKey == null)
                return default(T);

            prms.Add("app_id", mApplicationId);
            prms.Add("u_id", mAccount.UserId.ToString());
            prms.Add("nonce", authKey.Nonce);
            prms.Add("time", authKey.Time);
            prms.Add("auth_key", authKey.Key);

            return await CallAsync<T>(url, prms);
        }

        private static string GenerateNonce(int length)
        {
            var random = new Random();
            var nonceString = new StringBuilder();
            for (int i = 0; i < length; i++)
                nonceString.Append(mValidChars[random.Next(0, mValidChars.Length - 1)]);

            return nonceString.ToString();
        }

        private async Task<AuthorizationKey> GenerateAuthorizationKey()
        {
            if (String.IsNullOrEmpty(mAccount.SecretKey) && 
                !String.IsNullOrEmpty(mAccount.Address) && !String.IsNullOrEmpty(mAccount.Password))
            {
                var secretKey = await FetchSecretKey(mAccount.Address, mAccount.Password);
                if (secretKey == null)
                    return null;

                mAccount.UserId = secretKey.UserId;
                mAccount.SecretKey = secretKey.Key;
            }

            var nonce = "";
{'fraction_non_alphanumeric': 0.1085240726124704, 'fraction_numerical': 0.001973164956590371, 'mean_word_length': 1.7959183673469388, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3813300', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1350, 'n_words': 292}