<commit_before>using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestSharp;
using Salience.FluentApi.Internal;

namespace Salience.FluentApi
{
    public class FluentClient : IFluentClient
    {
        private readonly RestClient _restClient;
        private readonly string _defaultBaseApiPath;
        private readonly List<ITraceWriter> _traceWriters = new List<ITraceWriter>();

        public JsonSerializer Serializer { get; set; }

        public static JsonSerializer GetNewDefaultSerializer()
        {
            return new JsonSerializer
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                MissingMemberHandling = MissingMemberHandling.Ignore,
                NullValueHandling = NullValueHandling.Include,
                DefaultValueHandling = DefaultValueHandling.Include,
                DateParseHandling = DateParseHandling.None
            };
        }

        public FluentClient(string host, string defaultBaseApiPath = "")
        {
            Guard.NotNullOrEmpty(host, "host");
            Guard.NotNull(defaultBaseApiPath, "defaultBaseApiPath");

            _defaultBaseApiPath = defaultBaseApiPath.Trim();
            _defaultBaseApiPath = string.IsNullOrEmpty(_defaultBaseApiPath) ? "" : "/" + _defaultBaseApiPath.Trim('/');
            var url = new Uri(host.Trim().TrimEnd('/'), UriKind.Absolute);
            _restClient = new RestClient { BaseUrl = url };

            this.Serializer = GetNewDefaultSerializer();
        }

        public IFluentClient SetAuthenticator(IAuthenticator authenticator)
        {
            Guard.NotNull(authenticator, "authenticator");

            _restClient.Authenticator = authenticator;
            return this;
        }

        public IFluentClient AddTrace(ITraceWriter traceWriter)
        {
            Guard.NotNull(traceWriter, "traceWriter");

            _traceWriters.Add(traceWriter);
            return this;
        }

        public IFluentClient RemoveTrace(ITraceWriter traceWriter)
        {
            Guard.NotNull(traceWriter, "traceWriter");

            _traceWriters.Remove(traceWriter);
            return this;
        }

        public IRequestWithOperation To(string operation)
        {
            var data = this.CreateRequestData();
            var request = this.CreateEmptyRequest(data);
            return request.To(operation);
        }

        protected internal virtual RequestData CreateRequestData()
        {
            return new RequestData
            {
                BaseApiPath = _defaultBaseApiPath
            };
        }

        protected internal virtual IEmptyRequest CreateEmptyRequest(RequestData data)
        {
            return new FluentRequest(this, data);
        }

        protected internal virtual void Trace(TraceLevel level, string messageFormat, params object[] args)
        {
            foreach(var traceWriter in _traceWriters)
                traceWriter.Trace(level, string.Format(messageFormat, args));
        }

        protected internal virtual void TraceError(TraceLevel level, Exception exception, string messageFormat, params object[] args)
        {
            foreach(var traceWriter in _traceWriters)
                traceWriter.Trace(level, string.Format(messageFormat, args), exception);
        }

        protected internal virtual void HandleRequest(RequestData data)
        {
            this.CreateRestRequest(data);
            this.ConfigureRequest(data);
            this.TraceRequest(data);
            this.ExecuteRequest(data);
            this.ValidateResponse(data);
            this.TraceResponse(data);
            this.DeserializeResponseContent(data);
        }

        protected internal virtual async Task HandleRequestAsync(RequestData data)
        {
            this.CreateRestRequest(data);
            this.ConfigureRequest(data);
            this.TraceRequest(data);
            await this.ExecuteRequestAsync(data);
            this.ValidateResponse(data);
            this.TraceResponse(data);
            this.DeserializeResponseContent(data);
        }

        protected internal virtual void CreateRestRequest(RequestData data)
        {
            data.Request = new RestRequest
            {
                RequestFormat = DataFormat.Json,
                JsonSerializer = new JsonDotNetSerializerWrapper(this.Serializer)
            };
        }

        protected internal virtual void ConfigureRequest(RequestData data)<commit_msg>added default accept header (json only)<commit_after>using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestSharp;
using Salience.FluentApi.Internal;

namespace Salience.FluentApi
{
    public class FluentClient : IFluentClient
    {
        private readonly RestClient _restClient;
        private readonly string _defaultBaseApiPath;
        private readonly List<ITraceWriter> _traceWriters = new List<ITraceWriter>();

        public JsonSerializer Serializer { get; set; }

        public static JsonSerializer GetNewDefaultSerializer()
        {
            return new JsonSerializer
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                MissingMemberHandling = MissingMemberHandling.Ignore,
                NullValueHandling = NullValueHandling.Include,
                DefaultValueHandling = DefaultValueHandling.Include,
                DateParseHandling = DateParseHandling.None
            };
        }

        public FluentClient(string host, string defaultBaseApiPath = "")
        {
            Guard.NotNullOrEmpty(host, "host");
            Guard.NotNull(defaultBaseApiPath, "defaultBaseApiPath");

            _defaultBaseApiPath = defaultBaseApiPath.Trim();
            _defaultBaseApiPath = string.IsNullOrEmpty(_defaultBaseApiPath) ? "" : "/" + _defaultBaseApiPath.Trim('/');
            var url = new Uri(host.Trim().TrimEnd('/'), UriKind.Absolute);
            _restClient = new RestClient { BaseUrl = url };

            this.Serializer = GetNewDefaultSerializer();
        }

        public IFluentClient SetAuthenticator(IAuthenticator authenticator)
        {
            Guard.NotNull(authenticator, "authenticator");

            _restClient.Authenticator = authenticator;
            return this;
        }

        public IFluentClient AddTrace(ITraceWriter traceWriter)
        {
            Guard.NotNull(traceWriter, "traceWriter");

            _traceWriters.Add(traceWriter);
            return this;
        }

        public IFluentClient RemoveTrace(ITraceWriter traceWriter)
        {
            Guard.NotNull(traceWriter, "traceWriter");

            _traceWriters.Remove(traceWriter);
            return this;
        }

        public IRequestWithOperation To(string operation)
        {
            var data = this.CreateRequestData();
            var request = this.CreateEmptyRequest(data);
            return request.To(operation);
        }

        protected internal virtual RequestData CreateRequestData()
        {
            return new RequestData
            {
                BaseApiPath = _defaultBaseApiPath
            };
        }

        protected internal virtual IEmptyRequest CreateEmptyRequest(RequestData data)
        {
            return new FluentRequest(this, data);
        }

        protected internal virtual void Trace(TraceLevel level, string messageFormat, params object[] args)
        {
            foreach(var traceWriter in _traceWriters)
                traceWriter.Trace(level, string.Format(messageFormat, args));
        }

        protected internal virtual void TraceError(TraceLevel level, Exception exception, string messageFormat, params object[] args)
        {
            foreach(var traceWriter in _traceWriters)
                traceWriter.Trace(level, string.Format(messageFormat, args), exception);
        }

        protected internal virtual void HandleRequest(RequestData data)
        {
            this.CreateRestRequest(data);
            this.ConfigureRequest(data);
            this.TraceRequest(data);
            this.ExecuteRequest(data);
            this.ValidateResponse(data);
            this.TraceResponse(data);
            this.DeserializeResponseContent(data);
        }

        protected internal virtual async Task HandleRequestAsync(RequestData data)
        {
            this.CreateRestRequest(data);
            this.ConfigureRequest(data);
            this.TraceRequest(data);
            await this.ExecuteRequestAsync(data);
            this.ValidateResponse(data);
            this.TraceResponse(data);
            this.DeserializeResponseContent(data);
        }

        protected internal virtual void CreateRestRequest(RequestData data)
        {
            data.Request = new RestRequest
            {
                RequestFormat = DataFormat.Json,
                JsonSerializer = new JsonDotNetSerializerWrapper(this.Serializer)
            };
            data.Request.AddHeader("Accept", "application/json, text/json, text/x-json, text/javascript");
        }

        protected internal virtual void ConfigureRequest(RequestData data)
{'fraction_non_alphanumeric': 0.07390054307315515, 'fraction_numerical': 0.0, 'mean_word_length': 2.234159779614325, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2250363', 'n_tokens_mistral': 2244, 'n_tokens_neox': 2165, 'n_words': 477}