<commit_before>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Platron.Client.Authentication;
using Platron.Client.Http.Callbacks;
using Platron.Client.Http.Plain;
using Platron.Client.Utils;

namespace Platron.Client.Http
{
    public class Connection : IConnection
    {
        private static readonly HashSet<ErrorCode> errorCodesWithoutSignature = new HashSet<ErrorCode>(
            new[] {ErrorCode.InvalidMerchant, ErrorCode.InvalidSignature, ErrorCode.BadRequestParameter});

        private readonly Authenticator _authenticator;
        private readonly HttpRequestEncoder _httpRequestEncoder;
        private readonly IXmlPipeline _xmlPipeline;
        private HttpClient _httpClient;

        public Connection(Credentials credentials) : this(PlatronClient.PlatronUrl, credentials)
        {
        }

        public Connection(Uri baseAddress, Credentials credentials,
            HttpRequestEncodingType httpRequestEncodingType = HttpRequestEncodingType.PostWithXml)
            : this(baseAddress, credentials, new XmlPipeline(), httpRequestEncodingType)
        {
        }

        public Connection(Uri baseAddress, Credentials credentials, IXmlPipeline xmlPipeline,
            HttpRequestEncodingType httpRequestEncodingType = HttpRequestEncodingType.PostWithXml)
        {
            Ensure.ArgumentNotNull(baseAddress, "baseAddress");
            Ensure.ArgumentNotNull(credentials, "credentials");
            Ensure.ArgumentNotNull(xmlPipeline, "xmlPipeline");

            if (!baseAddress.IsAbsoluteUri)
            {
                throw new ArgumentException(
                    string.Format(CultureInfo.InvariantCulture, "The base address '{0}' must be an absolute URI",
                        baseAddress), nameof(baseAddress));
            }

            BaseAddress = baseAddress;

            _xmlPipeline = xmlPipeline;
            _httpClient = new HttpClient {BaseAddress = baseAddress};
            _authenticator = new Authenticator(credentials, xmlPipeline);
            _httpRequestEncoder = new HttpRequestEncoder(xmlPipeline);

            Callback = new CallbackResponder(_authenticator, _xmlPipeline);
        }

        public Uri BaseAddress { get; }

        public HttpRequestEncodingType EncodingType => _httpRequestEncoder.EncodingType;

        public ICallbackResponder Callback { get; }

        public Connection EnableProxy(WebProxy proxy)
        {
            Ensure.ArgumentNotNull(proxy, "proxy");

            var handler = new HttpClientHandler
                          {
                              Proxy = proxy,
                              UseProxy = true<commit_msg>* Connection.cs http request encoding type was ignored<commit_after>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Platron.Client.Authentication;
using Platron.Client.Http.Callbacks;
using Platron.Client.Http.Plain;
using Platron.Client.Utils;

namespace Platron.Client.Http
{
    public class Connection : IConnection
    {
        private static readonly HashSet<ErrorCode> errorCodesWithoutSignature = new HashSet<ErrorCode>(
            new[] {ErrorCode.InvalidMerchant, ErrorCode.InvalidSignature, ErrorCode.BadRequestParameter});

        private readonly Authenticator _authenticator;
        private readonly HttpRequestEncoder _httpRequestEncoder;
        private readonly IXmlPipeline _xmlPipeline;
        private HttpClient _httpClient;

        public Connection(Credentials credentials) : this(PlatronClient.PlatronUrl, credentials)
        {
        }

        public Connection(Uri baseAddress, Credentials credentials,
            HttpRequestEncodingType httpRequestEncodingType = HttpRequestEncodingType.PostWithXml)
            : this(baseAddress, credentials, new XmlPipeline(), httpRequestEncodingType)
        {
        }

        public Connection(Uri baseAddress, Credentials credentials, IXmlPipeline xmlPipeline,
            HttpRequestEncodingType httpRequestEncodingType = HttpRequestEncodingType.PostWithXml)
        {
            Ensure.ArgumentNotNull(baseAddress, "baseAddress");
            Ensure.ArgumentNotNull(credentials, "credentials");
            Ensure.ArgumentNotNull(xmlPipeline, "xmlPipeline");

            if (!baseAddress.IsAbsoluteUri)
            {
                throw new ArgumentException(
                    string.Format(CultureInfo.InvariantCulture, "The base address '{0}' must be an absolute URI",
                        baseAddress), nameof(baseAddress));
            }

            _xmlPipeline = xmlPipeline;
            _authenticator = new Authenticator(credentials, xmlPipeline);
            _httpRequestEncoder = new HttpRequestEncoder(xmlPipeline, httpRequestEncodingType);

            _httpClient = new HttpClient { BaseAddress = baseAddress };

            BaseAddress = baseAddress;
            Callback = new CallbackResponder(_authenticator, _xmlPipeline);
        }

        public Uri BaseAddress { get; }

        public HttpRequestEncodingType EncodingType => _httpRequestEncoder.EncodingType;

        public ICallbackResponder Callback { get; }

        public Connection EnableProxy(WebProxy proxy)
        {
            Ensure.ArgumentNotNull(proxy, "proxy");

            var handler = new HttpClientHandler
                          {
                              Proxy = proxy,
                              UseProxy = true
{'fraction_non_alphanumeric': 0.06740976645435244, 'fraction_numerical': 0.00035385704175513094, 'mean_word_length': 2.7044560943643514, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, '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': '7575739', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1296, 'n_words': 313}