<commit_before>﻿using System;
using RestSharp;
using RestSharp.Serializers.NewtonsoftJson;

namespace NetSapiensSharp
{
    public class Connector : IDisposable
    {
        private string _ApiBaseUrl;
        private string _ClientId;
        private string _ClientSecret;
        private string _Username;
        private string _Password;
        private string _SessionToken;
        private string _RefreshToken;
        private int _ExpiresInSeconds;
        private DateTime _ExpirationStartTime = DateTime.UtcNow;

        public Connector(string api_base_url, string client_id, string client_secret, string username, string password)
        {
            _ApiBaseUrl = api_base_url.TrimEnd('/') + '/';
            _ClientId = client_id;
            _ClientSecret = client_secret;
            _Username = username;
            _Password = password;
        }

        private void AuthenticateUsernamePassword()
        {
            var r = Connector.Authenticate(_ApiBaseUrl, _ClientId, _ClientSecret, _Username, _Password);
            if (r.Data == null)
            {
                throw (r.ErrorException);
            }
            _SessionToken = r.Data.access_token;
            _RefreshToken = r.Data.refresh_token;
            _ExpiresInSeconds = r.Data.expires_in;
            _ExpirationStartTime = DateTime.UtcNow;
        }

        private void AuthenticateRefreshToken()
        {
            var r = Connector.Authenticate(_ApiBaseUrl, _ClientId, _ClientSecret, _RefreshToken);
            if (r.Data == null)
            {
                throw (r.ErrorException);
            }
            _SessionToken = r.Data.access_token;
            _RefreshToken = r.Data.refresh_token;
            _ExpiresInSeconds = r.Data.expires_in;
            _ExpirationStartTime = DateTime.UtcNow;
        }

        private void Authenticate()
        {
            if (_SessionToken != null)
            {
                return;
            }
            if (_RefreshToken != null)
            {
                try
                {
                    AuthenticateRefreshToken();
                }
                catch
                {
                }
            }
            if (_SessionToken == null)
            {
                AuthenticateUsernamePassword();
            }
        }

        private bool IsAccessTokenExpired()
        {
            var myElapsedSeconds = Math.Ceiling((DateTime.UtcNow - _ExpirationStartTime).TotalSeconds);
            return myElapsedSeconds >= (double)_ExpiresInSeconds;
        }

        public Request<Tresponse> CreateRequest<Tresponse>(string action_path) where Tresponse : class, new()
        {
            var myItem = new Request<Tresponse>()
            {
                _Client = new RestClient(_ApiBaseUrl).UseNewtonsoftJson(),
                _Request = new RestRequest(action_path, Method.POST, DataFormat.Json)
            };
            myItem._Client.AddHandler("text/html", () => new JsonNetSerializer());
            myItem._Request.AddHeader("content-type", "application/json");
            return myItem;
        }

        public IRestResponse<Tresponse> Send<Tresponse>(Request<Tresponse> request)
            where Tresponse : class, new()
        {
            if (IsAccessTokenExpired())
            {
                _SessionToken = null;
            }
            Authenticate();
            request._Request.AddHeader("Authorization", "Bearer " + _SessionToken);
            var r = request._Client.Execute<Tresponse>(request._Request);
            if (r.StatusCode == System.Net.HttpStatusCode.OK && r.Data == null && typeof(Tresponse) == typeof(Common.OK))
            {
                r.Data = new Tresponse();
                r.ErrorException = null;
                r.ErrorMessage = null;
                r.ResponseStatus = ResponseStatus.Completed;
            }
            return r;
        }

        private class Authentication_Response<commit_msg>Token_Expiration Check for unauthorized response up to a retry limit. Throw an exception if we cannot connect due to failed authorization<commit_after>﻿using System;
using System.Linq;
using RestSharp;
using RestSharp.Serializers.NewtonsoftJson;

namespace NetSapiensSharp
{
    public class Connector : IDisposable
    {
        private string _ApiBaseUrl;
        private string _ClientId;
        private string _ClientSecret;
        private string _Username;
        private string _Password;
        private string _SessionToken;
        private string _RefreshToken;
        private int _ExpiresInSeconds;
        private DateTime _ExpirationStartTime = DateTime.UtcNow;
        private const int _UnauthorizedRetryLimit = 5;

        public Connector(string api_base_url, string client_id, string client_secret, string username, string password)
        {
            _ApiBaseUrl = api_base_url.TrimEnd('/') + '/';
            _ClientId = client_id;
            _ClientSecret = client_secret;
            _Username = username;
            _Password = password;
        }

        private void AuthenticateUsernamePassword()
        {
            var r = Connector.Authenticate(_ApiBaseUrl, _ClientId, _ClientSecret, _Username, _Password);
            if (r.Data == null)
            {
                throw (r.ErrorException);
            }
            _SessionToken = r.Data.access_token;
            _RefreshToken = r.Data.refresh_token;
            _ExpiresInSeconds = r.Data.expires_in;
            _ExpirationStartTime = DateTime.UtcNow;
        }

        private void AuthenticateRefreshToken()
        {
            var r = Connector.Authenticate(_ApiBaseUrl, _ClientId, _ClientSecret, _RefreshToken);
            if (r.Data == null)
            {
                throw (r.ErrorException);
            }
            _SessionToken = r.Data.access_token;
            _RefreshToken = r.Data.refresh_token;
            _ExpiresInSeconds = r.Data.expires_in;
            _ExpirationStartTime = DateTime.UtcNow;
        }

        private void Authenticate()
        {
            if (_SessionToken != null)
            {
                return;
            }
            if (_RefreshToken != null)
            {
                try
                {
                    AuthenticateRefreshToken();
                }
                catch
                {
                }
            }
            if (_SessionToken == null)
            {
                AuthenticateUsernamePassword();
            }
        }

        private bool IsAccessTokenExpired()
        {
            var myElapsedSeconds = Math.Ceiling((DateTime.UtcNow - _ExpirationStartTime).TotalSeconds);
            return myElapsedSeconds >= (double)_ExpiresInSeconds;
        }

        public Request<Tresponse> CreateRequest<Tresponse>(string action_path) where Tresponse : class, new()
        {
            var myItem = new Request<Tresponse>()
            {
                _Client = new RestClient(_ApiBaseUrl).UseNewtonsoftJson(),
                _Request = new RestRequest(action_path, Method.POST, DataFormat.Json)
            };
            myItem._Client.AddHandler("text/html", () => new JsonNetSerializer());
            myItem._Request.AddHeader("content-type", "application/json");
            return myItem;
        }

        public IRestResponse<Tresponse> Send<Tresponse>(Request<Tresponse> request)
            where Tresponse : class, new()
        {
            if (IsAccessTokenExpired())
            {
                _SessionToken = null;
            }
            int myRetryCount = 1;
            RequestRetry:

            Authenticate();
            request._Request.AddHeader("Authorization", "Bearer " + _SessionToken);
            var r = request._Client.Execute<Tresponse>(request._Request);
            if (r.StatusCode == System.Net.HttpStatusCode.Unauthorized && myRetryCount <= _UnauthorizedRetryLimit)
            {
                _SessionToken = null;
                var myOldAuthHeader = request._Request.Parameters.Where(p => p.Name.Equals("Authorization")).First();
                request._Request.Parameters.Remove(myOldAuthHeader);
                myRetryCount++;
                goto RequestRetry;
            }
            else if (r.StatusCode == System.Net.HttpStatusCode.OK && r.Data == null && typeof(Tresponse) == typeof(Common.OK))
            {
                r.Data = new Tresponse();
                r.ErrorException = null;
                r.ErrorMessage = null;
                r.ResponseStatus = ResponseStatus.Completed;
            }
            if (myRetryCount == _UnauthorizedRetryLimit)
            {
                throw new Exception($"Failed to authorize. Retry limit of {_UnauthorizedRetryLimit} has been reached.");
            }
            return r;
        }

        private class Authentication_Response
{'fraction_non_alphanumeric': 0.08185013037070626, 'fraction_numerical': 0.00022673166307674868, 'mean_word_length': 1.7061349693251533, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 22, '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': '224761', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2170, 'n_words': 462}