<commit_before>﻿/*
    Copyright 2015 <NAME>

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace BasicRestClient.RestClient {
    /// <summary>
    ///     Abstract class.
    /// </summary>
    public abstract class AbstractRestClient {
        protected static string UrlEncoded = "application/x-www-form-urlencoded;charset=UTF-8";
        protected static string Multipart = "multipart/form-data";
        protected static string Accept = "application/xml";
        protected bool Connected;

        #region HTTP Events 

        /// <summary>
        ///     Event fired when the request is about to be sent to the Server
        /// </summary>
        public event EventHandler<HttpRequestEventArgs> Sending = delegate { };

        /// <summary>
        ///     Event fired when the request has been completed successful
        /// </summary>
        public event EventHandler<HttpResponseEventArgs> Success = delegate { };

        /// <summary>
        ///     Event fired when there are exceptions. One can ignore the exception thrown and subscribe to this event.
        /// </summary>
        /// <remarks>
        ///     When subscribe to this event one can ignore the throwable exception like this:
        ///     catch(Exception e){}
        /// </remarks>
        public event EventHandler<HttpRequestExceptionEventArgs> Error = delegate { };

        /// <summary>
        ///     Event fired when the request has been completed it is successful or not.
        /// </summary>
        public event EventHandler<HttpResponseEventArgs> Complete = delegate { };

        /// <summary>
        ///     Event fired when the request has return 4xx error codes.
        ///     <remarks>
        ///         When subscribe to this event one can ignore the throwable exception like this:
        ///         catch(Exception e){}
        ///     </remarks>
        /// </summary>
        public event EventHandler<HttpResponseEventArgs> Failure = delegate { };

        #endregion

        protected AbstractRestClient(string baseUrl, IRequestHandler requestHandler, IRequestLogger requestLogger) {
            RequestLogger = requestLogger;
            RequestHandler = requestHandler;
            BaseUrl = baseUrl;
            RequestHeaders = new Dictionary<string, string>();
            ConnectionTimeout = 2000; //Default 2s, deliberately short.
            ReadWriteTimeout = 8000; // Default 8s, reasonably short if accidentally called from the UI thread
        }

        /// <summary>
        ///     Constructs a client with empty baseUrl. Prevent sub-classes from calling
        ///     this as it doesn't result in an instance of the subclass
        /// </summary>
        protected AbstractRestClient(int connectionLimit) : this("", connectionLimit) { }

        /// <summary>
        ///     Constructs a new client with base URL that will be appended in the request methods.
        /// </summary>
        /// <param name="baseUrl">Base Url</param>
        /// <param name="connectionLimit"></param>
        protected AbstractRestClient(string baseUrl, int connectionLimit) : this(baseUrl, new HttpRequestHandler(connectionLimit)) { }

        /// <summary>
        ///     Construct a client with baseUrl and RequestHandler.
        /// </summary>
        /// <param name="baseUrl">Base Url</param>
        /// <param name="requestHandler">Request Handler</param>
        protected AbstractRestClient(string baseUrl, IRequestHandler requestHandler) : this(baseUrl, requestHandler, new ConsoleRequestLogger(true)) { }

        #region Http Client Methods that drives the Web Requests<commit_msg>Fixes<commit_after>﻿/*
    Copyright 2015 <NAME>

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace BasicRestClient.RestClient {
    /// <summary>
    ///     Abstract class.
    /// </summary>
    public abstract class AbstractRestClient {
        /// <summary>
        /// Default URL encoded
        /// </summary>
        protected static string UrlEncoded = "application/x-www-form-urlencoded;charset=UTF-8";

        /// <summary>
        /// Default multipart. This is used internally by the Rest Client in case none is provided.
        /// </summary>
        protected static string Multipart = "multipart/form-data";

        /// <summary>
        /// Default Accept header used
        /// </summary>
        protected static string Accept = "application/xml";

        /// <summary>
        /// States whether the Rest Client is connected or not.
        /// </summary>
        protected bool Connected;

        #region HTTP Events 

        /// <summary>
        ///     Event fired when the request is about to be sent to the Server
        /// </summary>
        public event EventHandler<HttpRequestEventArgs> Sending = delegate { };

        /// <summary>
        ///     Event fired when the request has been completed successful
        /// </summary>
        public event EventHandler<HttpResponseEventArgs> Success = delegate { };

        /// <summary>
        ///     Event fired when there are exceptions. One can ignore the exception thrown and subscribe to this event.
        /// </summary>
        /// <remarks>
        ///     When subscribe to this event one can ignore the throwable exception like this:
        ///     catch(Exception e){}
        /// </remarks>
        public event EventHandler<HttpRequestExceptionEventArgs> Error = delegate { };

        /// <summary>
        ///     Event fired when the request has been completed it is successful or not.
        /// </summary>
        public event EventHandler<HttpResponseEventArgs> Complete = delegate { };

        /// <summary>
        ///     Event fired when the request has return 4xx error codes.
        ///     <remarks>
        ///         When subscribe to this event one can ignore the throwable exception like this:
        ///         catch(Exception e){}
        ///     </remarks>
        /// </summary>
        public event EventHandler<HttpResponseEventArgs> Failure = delegate { };

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="baseUrl"></param>
        /// <param name="requestHandler"></param>
        /// <param name="requestLogger"></param>
        protected AbstractRestClient(string baseUrl, IRequestHandler requestHandler, IRequestLogger requestLogger) {
            RequestLogger = requestLogger;
            RequestHandler = requestHandler;
            BaseUrl = baseUrl;
            RequestHeaders = new Dictionary<string, string>();
            ConnectionTimeout = 2000; //Default 2s, deliberately short.
            ReadWriteTimeout = 8000; // Default 8s, reasonably short if accidentally called from the UI thread
        }

        /// <summary>
        ///     Constructs a client with empty baseUrl. Prevent sub-classes from calling
        ///     this as it doesn't result in an instance of the subclass
        /// </summary>
        protected AbstractRestClient(int connectionLimit) : this("", connectionLimit) { }

        /// <summary>
        ///     Constructs a new client with base URL that will be appended in the request methods.
        /// </summary>
        /// <param name="baseUrl">Base Url</param>
        /// <param name="connectionLimit"></param>
        protected AbstractRestClient(string baseUrl, int connectionLimit) : this(baseUrl, new HttpRequestHandler(connectionLimit)) { }

        /// <summary>
        ///     Construct a client with baseUrl and RequestHandler.
        /// </summary>
        /// <param name="baseUrl">Base Url</param>
        /// <param name="requestHandler">Request Handler</param>
        protected AbstractRestClient(string baseUrl, IRequestHandler requestHandler) : this(baseUrl, requestHandler, new ConsoleRequestLogger(true)) { }

        #region Http Client Methods that drives the Web Requests
{'fraction_non_alphanumeric': 0.09775086505190311, 'fraction_numerical': 0.004325259515570935, 'mean_word_length': 2.2170434782608694, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, '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': '1782422', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2125, 'n_words': 818}