<commit_before>﻿using System;
using System.Collections.Generic;
using log4net;
using UniRx;
using UnityEngine.Networking;

namespace Silphid.Loadzup.Http
{
    public class HttpRequester : IHttpRequester, INetworkStatusProvider
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(HttpRequester));

        private readonly ReactiveProperty<NetworkStatus> _status =
            new ReactiveProperty<NetworkStatus>(NetworkStatus.Undetermined);

        public IReadOnlyReactiveProperty<NetworkStatus> Status => _status;

        public IObservable<Response> Request(Uri uri, Options options = null) =>
            RequestInternal(uri.AbsoluteUri, options)
                .DoOnSubscribe(() =>
                    Log.Info(GetLogMessage(uri, options)))
                .DoOnError(ex =>
                {
                    if (ex is NetworkException)
                        _status.Value = NetworkStatus.Offline;
                    
                    Log.Info($"Failed {GetLogMessage(uri, options)}", ex);
                })
                .Select(www => new
                {
                    WWW = www,
                    Headers = www.GetResponseHeaders()
                })
                .Do(x =>
                {
                    if (uri.Scheme != Scheme.Http && uri.Scheme != Scheme.Https)
                        return;

                    if (x.Headers == null)
                        Log.Warn($"No headers in response from {uri}");
                        
                    _status.Value = NetworkStatus.Online;
                })
                .Select(x => new Response(x.WWW.responseCode, x.WWW.downloadHandler.data,
                    x.Headers ?? new Dictionary<string, string>(), options));

        private IObservable<UnityWebRequest> RequestInternal(string url, Options options = null)
        {
            if (options == null || options.Method == HttpMethod.Get)
                return ObservableWebRequest.Get(url, options?.Headers, options?.Timeout);

            if (options.Method == HttpMethod.Post)
                return ObservableWebRequest.Post(url, options.PostForm, options.Headers, options.Timeout);

            if (options.Method == HttpMethod.Put)
                return ObservableWebRequest.Put(url, options.PutBody, options.Headers, options.Timeout);

            throw new NotImplementedException($"HTTP method {options.Method} not implemented for: {url}");
        }
        
        private string GetLogMessage(Uri uri, Options options)
        {
            var method = (options?.Method ?? HttpMethod.Get)
                .ToString()
                .ToUpper();
            
            return $"{method} {uri}";
        }
    }
}<commit_msg>Fixed logging of HttpRequester errors.<commit_after>﻿using System;
using System.Collections.Generic;
using log4net;
using UniRx;
using UnityEngine.Networking;

namespace Silphid.Loadzup.Http
{
    public class HttpRequester : IHttpRequester, INetworkStatusProvider
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(HttpRequester));

        private readonly ReactiveProperty<NetworkStatus> _status =
            new ReactiveProperty<NetworkStatus>(NetworkStatus.Undetermined);

        public IReadOnlyReactiveProperty<NetworkStatus> Status => _status;

        public IObservable<Response> Request(Uri uri, Options options = null) =>
            RequestInternal(uri.AbsoluteUri, options)
                .DoOnSubscribe(() =>
                    Log.Info(GetLogMessage(uri, options)))
                .DoOnError(ex =>
                {
                    if (ex is NetworkException)
                        _status.Value = NetworkStatus.Offline;
                    
                    Log.Debug($"Failed {GetLogMessage(uri, options)} with exception:\r\n{ex}");
                })
                .Select(www => new
                {
                    WWW = www,
                    Headers = www.GetResponseHeaders()
                })
                .Do(x =>
                {
                    if (uri.Scheme != Scheme.Http && uri.Scheme != Scheme.Https)
                        return;

                    if (x.Headers == null)
                        Log.Warn($"No headers in response from {uri}");
                        
                    _status.Value = NetworkStatus.Online;
                })
                .Select(x => new Response(x.WWW.responseCode, x.WWW.downloadHandler.data,
                    x.Headers ?? new Dictionary<string, string>(), options));

        private IObservable<UnityWebRequest> RequestInternal(string url, Options options = null)
        {
            if (options == null || options.Method == HttpMethod.Get)
                return ObservableWebRequest.Get(url, options?.Headers, options?.Timeout);

            if (options.Method == HttpMethod.Post)
                return ObservableWebRequest.Post(url, options.PostForm, options.Headers, options.Timeout);

            if (options.Method == HttpMethod.Put)
                return ObservableWebRequest.Put(url, options.PutBody, options.Headers, options.Timeout);

            throw new NotImplementedException($"HTTP method {options.Method} not implemented for: {url}");
        }
        
        private string GetLogMessage(Uri uri, Options options)
        {
            var method = (options?.Method ?? HttpMethod.Get)
                .ToString()
                .ToUpper();
            
            return $"{method} {uri}";
        }
    }
}
{'fraction_non_alphanumeric': 0.09785766158315178, 'fraction_numerical': 0.00036310820624546115, 'mean_word_length': 1.7071253071253072, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6344371', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1301, 'n_words': 295}