<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Chatwork.Service
{
    public class ChatWorkAuthenticationHandler : DelegatingHandler
    {
        readonly string apiToken;

        public ChatWorkAuthenticationHandler(string apiToken)
            : this(apiToken, new System.Net.Http.HttpClientHandler())
        { }

        public ChatWorkAuthenticationHandler(string apiToken, HttpMessageHandler innerHandler)
            : base(innerHandler)
        {
            this.apiToken = apiToken;
        }

        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            request.Headers.Add("X-ChatWorkToken", apiToken);
            return base.SendAsync(request, cancellationToken);
        }
    }

    public partial class ChatworkClient : IMe, IMy, IContact, IRoom
    {
        private static readonly string BaseUri = "https://api.chatwork.com/v1/";
        
        readonly HttpClient httpClient;

        public long MaxResponseContentBufferSize
        {
            get
            {
                return httpClient.MaxResponseContentBufferSize;
            }
            set
            {
                httpClient.MaxResponseContentBufferSize = value;
            }
        }
 
        public TimeSpan Timeout
        {
            get
            {
                return httpClient.Timeout;
            }
            set
            {
                httpClient.Timeout = value;
            }
        }

        public IMe Me { get { return this; } }
        public IMy My { get { return this; } }
        public IContact Contract { get { return this; } }
        public IRoom Room { get { return this; } }
        
        public ChatworkClient(string apiToken)
        {
            httpClient = new HttpClient(new ChatWorkAuthenticationHandler(apiToken));
        }

        public ChatworkClient(string apiToken, HttpMessageHandler innerHandler)
        {
            httpClient = new HttpClient(new ChatWorkAuthenticationHandler(apiToken, innerHandler));
        }

        private async Task<TResult> GetAsync<TResult>(string path, params KeyValuePair<string, object>[] parameters)
        {
            //TODO エスケープ処理
            var requestUri = new Uri(BaseUri + path + "?" + string.Join("&", parameters.Where(p => p.Value != null).Select(p => p.Key + "=" + ConvertToString(p.Value))));
            var request = new HttpRequestMessage()
            {
                Method = HttpMethod.Get,
                RequestUri = requestUri
            };
            return await SendAsync<TResult>(request);
        }

        private async Task<TResult> SendAsync<TResult>(HttpMethod method, string path, params KeyValuePair<string, object>[] parameters)
        {
            var request = new HttpRequestMessage()
            {
                Method = method,
                Content = new FormUrlEncodedContent(parameters.Where(p => p.Value != null).Select(p => new KeyValuePair<string, string>(p.Key, ConvertToString(p.Value)))),
                RequestUri = new Uri(BaseUri + path)
            };
            return await SendAsync<TResult>(request);
        }

        private async Task<TResult> SendAsync<TResult>(HttpRequestMessage request)
        {
            var res = await httpClient.SendAsync(request).ConfigureAwait(false);
            if (res.IsSuccessStatusCode)
            {
                return JsonConvert.DeserializeObject<TResult>(await res.Content.ReadAsStringAsync().ConfigureAwait(false));
            }
            throw new Exception(string.Format("Failed with code {0}. Message: {1}", res.StatusCode, await res.Content.ReadAsStringAsync()));
        }

        private async Task SendAsync(HttpMethod httpMethod, string path, params KeyValuePair<string, object>[] parameters)
        {
            var request = new HttpRequestMessage()
            {
                Method = httpMethod,
                Content = new FormUrlEncodedContent(parameters.Where(p => p.Value != null).Select(p => new KeyValuePair<string, string>(p.Key, ConvertToString(p.Value)))),
                RequestUri = new Uri(BaseUri + path)
            };
            var res = await httpClient.SendAsync(request).ConfigureAwait(false);
            if (res.IsSuccessStatusCode)
            {
                return;
            }
            throw new Exception(string.Format("Failed with code {0}. Message: {1}", res.StatusCode, await res.Content.ReadAsStringAsync()));
        }<commit_msg>closed issue #9<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Chatwork.Service
{
    public class ChatWorkAuthenticationHandler : DelegatingHandler
    {
        readonly string apiToken;

        public ChatWorkAuthenticationHandler(string apiToken)
            : this(apiToken, new System.Net.Http.HttpClientHandler())
        { }

        public ChatWorkAuthenticationHandler(string apiToken, HttpMessageHandler innerHandler)
            : base(innerHandler)
        {
            this.apiToken = apiToken;
        }

        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            request.Headers.Add("X-ChatWorkToken", apiToken);
            return base.SendAsync(request, cancellationToken);
        }
    }

    public partial class ChatworkClient : IMe, IMy, IContact, IRoom
    {
        private static readonly string BaseUri = "https://api.chatwork.com/v1/";
        
        readonly HttpClient httpClient;

        public long MaxResponseContentBufferSize
        {
            get
            {
                return httpClient.MaxResponseContentBufferSize;
            }
            set
            {
                httpClient.MaxResponseContentBufferSize = value;
            }
        }
 
        public TimeSpan Timeout
        {
            get
            {
                return httpClient.Timeout;
            }
            set
            {
                httpClient.Timeout = value;
            }
        }

        public IMe Me { get { return this; } }
        public IMy My { get { return this; } }
        public IContact Contract { get { return this; } }
        public IRoom Room { get { return this; } }
        
        public ChatworkClient(string apiToken)
        {
            httpClient = new HttpClient(new ChatWorkAuthenticationHandler(apiToken));
        }

        public ChatworkClient(string apiToken, HttpMessageHandler innerHandler)
        {
            httpClient = new HttpClient(new ChatWorkAuthenticationHandler(apiToken, innerHandler));
        }

        private Task<TResult> GetAsync<TResult>(string path, params KeyValuePair<string, object>[] parameters)
        {
            //TODO エスケープ処理
            var requestUri = new Uri(BaseUri + path + "?" + string.Join("&", parameters.Where(p => p.Value != null).Select(p => p.Key + "=" + ConvertToString(p.Value))));
            var request = new HttpRequestMessage()
            {
                Method = HttpMethod.Get,
                RequestUri = requestUri
            };
            return SendAsync<TResult>(request);
        }

        private Task<TResult> SendAsync<TResult>(HttpMethod method, string path, params KeyValuePair<string, object>[] parameters)
        {
            var request = new HttpRequestMessage()
            {
                Method = method,
                Content = new FormUrlEncodedContent(parameters.Where(p => p.Value != null).Select(p => new KeyValuePair<string, string>(p.Key, ConvertToString(p.Value)))),
                RequestUri = new Uri(BaseUri + path)
            };
            return SendAsync<TResult>(request);
        }

        private async Task<TResult> SendAsync<TResult>(HttpRequestMessage request)
        {
            var res = await httpClient.SendAsync(request).ConfigureAwait(false);
            if (res.IsSuccessStatusCode)
            {
                return JsonConvert.DeserializeObject<TResult>(await res.Content.ReadAsStringAsync().ConfigureAwait(false));
            }
            throw new Exception(string.Format("Failed with code {0}. Message: {1}", res.StatusCode, await res.Content.ReadAsStringAsync()));
        }

        private async Task SendAsync(HttpMethod httpMethod, string path, params KeyValuePair<string, object>[] parameters)
        {
            var request = new HttpRequestMessage()
            {
                Method = httpMethod,
                Content = new FormUrlEncodedContent(parameters.Where(p => p.Value != null).Select(p => new KeyValuePair<string, string>(p.Key, ConvertToString(p.Value)))),
                RequestUri = new Uri(BaseUri + path)
            };
            var res = await httpClient.SendAsync(request).ConfigureAwait(false);
            if (res.IsSuccessStatusCode)
            {
                return;
            }
            throw new Exception(string.Format("Failed with code {0}. Message: {1}", res.StatusCode, await res.Content.ReadAsStringAsync()));
        }
{'fraction_non_alphanumeric': 0.09101654846335698, 'fraction_numerical': 0.001182033096926714, 'mean_word_length': 2.050475254015077, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 45, 'https://': 2, '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': '3932389', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2286, 'n_words': 526}