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

namespace HelloSignNet
{
    public class HelloSignConfig
    {
        public string ApiKey { get; set; }
        public string BaseUri { get; set; }
        public string SignatureRequestUri { get; set; }
        public string SendSignatureRequestUri { get; set; }

        public HelloSignConfig()
        {
            // elided
        }

        public HelloSignConfig(string apiKey, string baseUri = "https://api.hellosign.com/v3/")
        {
            ApiKey = apiKey;
            BaseUri = baseUri;
            SignatureRequestUri = BaseUri + "signature_request";
            SendSignatureRequestUri = BaseUri + "signature_request/send";
        }
    }

    public class HelloSignApiClient
    {
        private readonly HttpClient _client;
        private readonly HelloSignConfig _config;

        public HelloSignApiClient(string apiKey)
            : this(new HelloSignConfig(apiKey))
        {
        }

        public HelloSignApiClient(HelloSignConfig config)
        {
            _config = config;
            _client = FrameworkHelper.CreateBasicAuthHttpClient(_config.ApiKey, "");
        }

        public HelloSignApiClient(HttpClient httpClient, HelloSignConfig config)
        {
            _client = httpClient;
            _config = config;
        }

        public Task<SignatureRequestResponse> SendSignatureRequest(SignatureRequestData request, int milisecondsTimeout = 100000)
        {
            using (var formData = CreateFormData(request))
            {
                var tcs = new TaskCompletionSource<SignatureRequestResponse>();

                var postTask = _client.PostAsync(_config.SendSignatureRequestUri, formData).ContinueWith(t =>
                    {
                        try
                        {
                            t.Result.EnsureSuccessStatusCode();

                            var serializer = new JsonSerializer
                            {
                                ContractResolver = new FrameworkHelper.UnderscoreMappingResolver()
                            };

                            using (var streamReader = new StreamReader(t.Result.Content.ReadAsStreamAsync().Result))
                            using (var jsonReader = new JsonTextReader(streamReader))
                            {
                                var response = serializer.Deserialize<SignatureRequestResponse>(jsonReader);
                                tcs.TrySetResult(response);
                            }
                        }
                        catch(Exception ex)
                        {
                            tcs.TrySetException(ex);
                        }

                    });
                
                postTask.Wait(milisecondsTimeout);

                return tcs.Task;
            }
        }

        private MultipartFormDataContent CreateFormData(SignatureRequestData request)
        {
            var formData = new MultipartFormDataContent();

            if (!string.IsNullOrEmpty(request.Title))
                formData.AddStringContent("title", request.Title);
            if (!string.IsNullOrEmpty(request.Subject))
                formData.AddStringContent("subject", request.Subject);
            if (!string.IsNullOrEmpty(request.Message))<commit_msg>Update Complete Source<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace HelloSignNet
{
    public class HelloSignConfig
    {
        public string ApiKey { get; set; }
        public string BaseUri { get; set; }
        public string SignatureRequestUri { get; set; }
        public string SendSignatureRequestUri { get; set; }

        public HelloSignConfig()
        {
            // elided
        }

        public HelloSignConfig(string apiKey, string baseUri = "https://api.hellosign.com/v3/")
        {
            ApiKey = apiKey;
            BaseUri = baseUri;
            SignatureRequestUri = BaseUri + "signature_request";
            SendSignatureRequestUri = BaseUri + "signature_request/send";
        }
    }

    public class HelloSignApiClient
    {
        private readonly HttpClient _client;
        private readonly HelloSignConfig _config;

        public HelloSignApiClient(string apiKey)
            : this(new HelloSignConfig(apiKey))
        {
        }

        public HelloSignApiClient(HelloSignConfig config)
        {
            _config = config;
            _client = FrameworkHelper.CreateBasicAuthHttpClient(_config.ApiKey, "");
        }

        public HelloSignApiClient(HttpClient httpClient, HelloSignConfig config)
        {
            _client = httpClient;
            _config = config;
        }

        public Task<SignatureRequestResponse> SendSignatureRequest(SignatureRequestData request, int milisecondsTimeout = 100000)
        {
            var formData = CreateFormData(request);
            return _client.PostAsync(_config.SendSignatureRequestUri, formData).ContinueWith(t =>
            {
                t.Result.EnsureSuccessStatusCode();

                var serializer = new JsonSerializer { ContractResolver = new FrameworkHelper.UnderscoreMappingResolver() };

                using (var streamReader = new StreamReader(t.Result.Content.ReadAsStreamAsync().Result))
                using (var jsonReader = new JsonTextReader(streamReader))
                {
                    var response = serializer.Deserialize<SignatureRequestResponse>(jsonReader);
                    return response;
                }
            });
        }

        private MultipartFormDataContent CreateFormData(SignatureRequestData request)
        {
            var formData = new MultipartFormDataContent();

            if (!string.IsNullOrEmpty(request.Title))
                formData.AddStringContent("title", request.Title);
            if (!string.IsNullOrEmpty(request.Subject))
                formData.AddStringContent("subject", request.Subject);
            if (!string.IsNullOrEmpty(request.Message))
{'fraction_non_alphanumeric': 0.0756394260761073, 'fraction_numerical': 0.002183406113537118, 'mean_word_length': 1.8591172536781098, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1345226', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1490, 'n_words': 313}