<commit_before>﻿using FrontSharp.Interfaces;
using FrontSharp.Logic;
using FrontSharp.Requests;
using FrontSharp.Serializers;
using RestSharp;
using RestSharp.Authenticators;
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FrontSharp
{
    public class FrontSharpClient
    {
        private string _baseUrl;
        private string _token;

        public ICommentLogic Comments { get; }

        public FrontSharpClient()
        {
            _baseUrl = ConfigurationManager.AppSettings["FrontAPIEndpoint"].ToString();
            _token = ConfigurationManager.AppSettings["FrontAPIToken"].ToString();
        }

        public FrontSharpClient(string baseUrl, string token)
        {
            this._baseUrl = baseUrl;
            this._token = token;
        }

        //Sets RestSharp to use JSON.Net for Deserialization
        private RestClient CreateClient(string baseUrl)
        {
            var client = new RestClient(baseUrl);

            // Override with Newtonsoft JSON Handler
            client.AddHandler("application/json", NewtonsoftJsonSerializer.Default);
            client.AddHandler("text/json", NewtonsoftJsonSerializer.Default);
            client.AddHandler("text/x-json", NewtonsoftJsonSerializer.Default);
            client.AddHandler("text/javascript", NewtonsoftJsonSerializer.Default);
            client.AddHandler("*+json", NewtonsoftJsonSerializer.Default);

            return client;
        }

        public T Execute<T>(RestRequest request, object objToBeSerialized = null, NewtonsoftJsonSerializer serializer = null) where T : new()
        {
            //Set Defaults for request
            request.RequestFormat = DataFormat.Json;
            request.JsonSerializer = serializer != null ? serializer : NewtonsoftJsonSerializer.Default;
            if (objToBeSerialized != null) request.AddJsonBody(objToBeSerialized);

            var client = CreateClient(_baseUrl);
            client.Authenticator = new JwtAuthenticator(_token); // used on every request
            request.RootElement = "";
            
            var response = client.Execute<T>(request);

            //Throw Error if Exception Occurred (Usually network issues)
            if (response.ErrorException != null)
            {
                const string message = "Error retrieving response.  Check inner details for more info.";
                var webApiException = new ApplicationException(message, response.ErrorException);
                throw webApiException;
            }
            return response.Data;
        }
    }
}<commit_msg>Added Comments to FrontSharpClient.<commit_after>﻿using FrontSharp.Interfaces;
using FrontSharp.Logic;
using FrontSharp.Requests;
using FrontSharp.Serializers;
using RestSharp;
using RestSharp.Authenticators;
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FrontSharp
{
    public class FrontSharpClient
    {
        private string _baseUrl;
        private string _token;

        

        public FrontSharpClient() : this(ConfigurationManager.AppSettings["FrontAPIEndpoint"].ToString(),ConfigurationManager.AppSettings["FrontAPIToken"].ToString())
        {
            
        }

        public FrontSharpClient(string baseUrl, string token)
        {
            this._baseUrl = baseUrl;
            this._token = token;
            this.Comments = new CommentLogic(this);
        }

        //Sets RestSharp to use JSON.Net for Deserialization
        private RestClient CreateClient(string baseUrl)
        {
            var client = new RestClient(baseUrl);

            // Override with Newtonsoft JSON Handler
            client.AddHandler("application/json", NewtonsoftJsonSerializer.Default);
            client.AddHandler("text/json", NewtonsoftJsonSerializer.Default);
            client.AddHandler("text/x-json", NewtonsoftJsonSerializer.Default);
            client.AddHandler("text/javascript", NewtonsoftJsonSerializer.Default);
            client.AddHandler("*+json", NewtonsoftJsonSerializer.Default);

            return client;
        }

        public T Execute<T>(RestRequest request, object objToBeSerialized = null, NewtonsoftJsonSerializer serializer = null) where T : new()
        {
            //Set Defaults for request
            request.RequestFormat = DataFormat.Json;
            request.JsonSerializer = serializer != null ? serializer : NewtonsoftJsonSerializer.Default;
            if (objToBeSerialized != null) request.AddJsonBody(objToBeSerialized);

            var client = CreateClient(_baseUrl);
            client.Authenticator = new JwtAuthenticator(_token); // used on every request
            request.RootElement = "";
            
            var response = client.Execute<T>(request);

            //Throw Error if Exception Occurred (Usually network issues)
            if (response.ErrorException != null)
            {
                const string message = "Error retrieving response.  Check inner details for more info.";
                var webApiException = new ApplicationException(message, response.ErrorException);
                throw webApiException;
            }
            return response.Data;
        }

        public ICommentLogic Comments { get; private set; }
    }
}
{'fraction_non_alphanumeric': 0.08115568641884431, 'fraction_numerical': 0.0, 'mean_word_length': 2.495176848874598, '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': '2126708', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1270, 'n_words': 325}