<commit_before>﻿using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using UltimateTeam.Toolkit.Constants;
using UltimateTeam.Toolkit.Exceptions;
using UltimateTeam.Toolkit.Extensions;
using UltimateTeam.Toolkit.Models;

namespace UltimateTeam.Toolkit.Requests
{
    public abstract class FutRequestBase
    {
        private string _phishingToken;

        private string _sessionId;

        private IHttpClient _httpClient;

        public string PhishingToken
        {
            set
            {
                value.ThrowIfInvalidArgument();
                _phishingToken = value;
            }
        }

        public string SessionId
        {
            set
            {
                value.ThrowIfInvalidArgument();
                _sessionId = value;
            }
        }

        internal IHttpClient HttpClient
        {
            get { return _httpClient ?? (_httpClient = new HttpClientWrapper()); }
            set
            {
                value.ThrowIfNullArgument();
                _httpClient = value;
            }
        }

        protected FutRequestBase()
        {
            JsonConvert.DefaultSettings = () => new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error };
            HttpClient.SetExpectContinueHeaderToFalse();
        }

        protected void AddCommonHeaders()
        {
            HttpClient.AddRequestHeader(NonStandardHttpHeaders.PhishingToken, _phishingToken);
            HttpClient.AddRequestHeader(NonStandardHttpHeaders.EmbedError, "true");
            HttpClient.AddRequestHeader(NonStandardHttpHeaders.SessionId, _sessionId);
            AddAcceptEncodingHeader();
            AddAcceptLanguageHeader();
            AddAcceptHeader("application/json");
            HttpClient.AddRequestHeader(HttpHeaders.ContentType, "application/json");
            AddReferrerHeader("http://www.easports.com/iframe/fut/bundles/futweb/web/flash/FifaUltimateTeam.swf");
            AddUserAgent();
            HttpClient.AddConnectionKeepAliveHeader();
        }

        protected void AddUserAgent()
        {
            HttpClient.AddRequestHeader(HttpHeaders.UserAgent, "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36");
        }

        protected void AddAcceptHeader(string value)
        {
            HttpClient.AddRequestHeader(HttpHeaders.Accept, value);
        }

        protected void AddReferrerHeader(string value)
        {
            HttpClient.SetReferrerUri(value);
        }

        protected void AddAcceptEncodingHeader()
        {
            HttpClient.AddRequestHeader(HttpHeaders.AcceptEncoding, "gzip,deflate,sdch");
        }

        protected void AddAcceptLanguageHeader()
        {
            HttpClient.AddRequestHeader(HttpHeaders.AcceptLanguage, "en-US,en;q=0.8");
        }

        protected void AddMethodOverrideHeader(HttpMethod httpMethod)
        {
            HttpClient.AddRequestHeader(NonStandardHttpHeaders.MethodOverride, httpMethod.Method);
        }

        protected static async Task<T> Deserialize<T>(HttpResponseMessage message) where T : class
        {
            message.EnsureSuccessStatusCode();
            var messageContent = await message.Content.ReadAsStringAsync();
            T deserializedObject = null;

            try
            {
                deserializedObject = JsonConvert.DeserializeObject<T>(messageContent);
            }
            catch (JsonSerializationException serializationException)
            {
                try
                {
                    var futErrorWithDebugString = JsonConvert.DeserializeObject<FutErrorWithDebugString>(messageContent);
                    MapAndThrowException(serializationException, futErrorWithDebugString);
                }
                catch (JsonSerializationException)
                {
                    try
                    {
                        var futErrorWithMessage = JsonConvert.DeserializeObject<FutErrorWithMessage>(messageContent);
                        MapAndThrowException(serializationException, futErrorWithMessage);
                    }
                    catch (JsonSerializationException)
                    {
                        throw serializationException;
                    }
                }
            }

            return deserializedObject;
        }

        private static void MapAndThrowException(Exception exception, FutErrorBase futError)
        {
            switch (futError.Code)
            {
                case FutErrorCode.PermissionDenied:
                    throw new PermissionDeniedException((FutErrorWithDebugString)futError, exception);
                case FutErrorCode.ExpiredSession:
                    throw new ExpiredSessionException((FutErrorWithMessage)futError, exception);
                case FutErrorCode.InternalServerError:
                    throw new InternalServerException((FutErrorWithDebugString)futError, exception);
                default:
                    throw new FutException("Serialization exception, check inner exception for details", exception);
            }
        }<commit_msg>Now passing a more detailed message when a FutException is thrown<commit_after>﻿using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using UltimateTeam.Toolkit.Constants;
using UltimateTeam.Toolkit.Exceptions;
using UltimateTeam.Toolkit.Extensions;
using UltimateTeam.Toolkit.Models;

namespace UltimateTeam.Toolkit.Requests
{
    public abstract class FutRequestBase
    {
        private string _phishingToken;

        private string _sessionId;

        private IHttpClient _httpClient;

        public string PhishingToken
        {
            set
            {
                value.ThrowIfInvalidArgument();
                _phishingToken = value;
            }
        }

        public string SessionId
        {
            set
            {
                value.ThrowIfInvalidArgument();
                _sessionId = value;
            }
        }

        internal IHttpClient HttpClient
        {
            get { return _httpClient ?? (_httpClient = new HttpClientWrapper()); }
            set
            {
                value.ThrowIfNullArgument();
                _httpClient = value;
            }
        }

        protected FutRequestBase()
        {
            JsonConvert.DefaultSettings = () => new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error };
            HttpClient.SetExpectContinueHeaderToFalse();
        }

        protected void AddCommonHeaders()
        {
            HttpClient.AddRequestHeader(NonStandardHttpHeaders.PhishingToken, _phishingToken);
            HttpClient.AddRequestHeader(NonStandardHttpHeaders.EmbedError, "true");
            HttpClient.AddRequestHeader(NonStandardHttpHeaders.SessionId, _sessionId);
            AddAcceptEncodingHeader();
            AddAcceptLanguageHeader();
            AddAcceptHeader("application/json");
            HttpClient.AddRequestHeader(HttpHeaders.ContentType, "application/json");
            AddReferrerHeader("http://www.easports.com/iframe/fut/bundles/futweb/web/flash/FifaUltimateTeam.swf");
            AddUserAgent();
            HttpClient.AddConnectionKeepAliveHeader();
        }

        protected void AddUserAgent()
        {
            HttpClient.AddRequestHeader(HttpHeaders.UserAgent, "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36");
        }

        protected void AddAcceptHeader(string value)
        {
            HttpClient.AddRequestHeader(HttpHeaders.Accept, value);
        }

        protected void AddReferrerHeader(string value)
        {
            HttpClient.SetReferrerUri(value);
        }

        protected void AddAcceptEncodingHeader()
        {
            HttpClient.AddRequestHeader(HttpHeaders.AcceptEncoding, "gzip,deflate,sdch");
        }

        protected void AddAcceptLanguageHeader()
        {
            HttpClient.AddRequestHeader(HttpHeaders.AcceptLanguage, "en-US,en;q=0.8");
        }

        protected void AddMethodOverrideHeader(HttpMethod httpMethod)
        {
            HttpClient.AddRequestHeader(NonStandardHttpHeaders.MethodOverride, httpMethod.Method);
        }

        protected static async Task<T> Deserialize<T>(HttpResponseMessage message) where T : class
        {
            message.EnsureSuccessStatusCode();
            var messageContent = await message.Content.ReadAsStringAsync();
            T deserializedObject = null;

            try
            {
                deserializedObject = JsonConvert.DeserializeObject<T>(messageContent);
            }
            catch (JsonSerializationException serializationException)
            {
                try
                {
                    var futErrorWithDebugString = JsonConvert.DeserializeObject<FutErrorWithDebugString>(messageContent);
                    MapAndThrowException(serializationException, futErrorWithDebugString);
                }
                catch (JsonSerializationException)
                {
                    try
                    {
                        var futErrorWithMessage = JsonConvert.DeserializeObject<FutErrorWithMessage>(messageContent);
                        MapAndThrowException(serializationException, futErrorWithMessage);
                    }
                    catch (JsonSerializationException)
                    {
                        throw serializationException;
                    }
                }
            }

            return deserializedObject;
        }

        private static void MapAndThrowException(Exception exception, FutErrorBase futError)
        {
            switch (futError.Code)
            {
                case FutErrorCode.PermissionDenied:
                    throw new PermissionDeniedException((FutErrorWithDebugString)futError, exception);
                case FutErrorCode.ExpiredSession:
                    throw new ExpiredSessionException((FutErrorWithMessage)futError, exception);
                case FutErrorCode.InternalServerError:
                    throw new InternalServerException((FutErrorWithDebugString)futError, exception);
                default:
                    throw new FutException(string.Format("Unknown EA error, please report on GitHub - Code: {0}, Reason: {1}", futError.Code, futError.Reason), exception);
            }
        }
{'fraction_non_alphanumeric': 0.07002748033734484, 'fraction_numerical': 0.005306547901070786, 'mean_word_length': 1.9316666666666666, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5928485', 'n_tokens_mistral': 2547, 'n_tokens_neox': 2409, 'n_words': 428}