<commit_before>﻿using System;
using System.Threading;
using System.Threading.Tasks;
using HttpAction.Core;
using HttpAction.Event;
using HttpAction.Service;
using System.Linq;

namespace HttpAction.Action
{
    public abstract class AbstractHttpAction : IHttpAction
    {
        protected virtual int MaxReTryTimes { get; set; } = 3;

        protected int ExcuteTimes { get; set; }
        protected int RetryTimes { get; set; }
        protected IHttpService HttpService { get; set; }

        protected AbstractHttpAction(IHttpService httpHttpService)
        {
            HttpService = httpHttpService;
        }

        protected virtual async Task<ActionEvent> NotifyActionEventAsync(ActionEvent actionEvent)
        {
            try
            {
                if (OnActionEvent != null) await OnActionEvent.Invoke(this, actionEvent);
                if (actionEvent.Type == ActionEventType.EvtRetry) ++RetryTimes;
                else RetryTimes = 0;
                return actionEvent;
            }
            catch (Exception ex)
            {
                ++RetryTimes;
                return await HandleExceptionAsync(ex);
            }
        }

        protected Task<ActionEvent> NotifyActionEventAsync(ActionEventType type, object target = null)
        {
            return NotifyActionEventAsync(ActionEvent.CreateEvent(type, target));
        }

        protected Task<ActionEvent> NotifyOkActionEventAsync(object target = null)
        {
            return NotifyActionEventAsync(ActionEventType.EvtOK, target);
        }

        protected Task<ActionEvent> NotifyErrorEventAsync(Exception ex)
        {
            return NotifyActionEventAsync(ActionEvent.CreateEvent(ActionEventType.EvtError, ex));
        }

        protected Task<ActionEvent> NotifyErrorEventAsync(string msg)
        {
            return NotifyActionEventAsync(ActionEvent.CreateEvent(ActionEventType.EvtError, new Exception(msg)));
        }

        public abstract HttpRequestItem BuildRequest();

        public abstract Task<ActionEvent> HandleResponse(HttpResponseItem response);

        public event ActionEventListener OnActionEvent;

        public virtual async Task<ActionEvent> HandleExceptionAsync(Exception ex)
        {
            try
            {
                var @event = ActionEvent.CreateEvent(RetryTimes < MaxReTryTimes ?
                    ActionEventType.EvtRetry : ActionEventType.EvtError, ex);
                return await NotifyActionEventAsync(@event);
            }
            catch (Exception e)
            {
                throw new Exception($"throw an unhandled exception when excute [{nameof(HandleExceptionAsync)}] method.", e);
            }
        }

        public virtual async Task<ActionEvent> ExecuteAsync(CancellationToken token)
        {
            HttpRequestItem requestItem = null;
            if (!token.IsCancellationRequested)
            {
                ++ExcuteTimes;
                try
                {
                    requestItem = BuildRequest();
                    var response = await HttpService.ExecuteHttpRequestAsync(requestItem, token).ConfigureAwait(false);
                    var result = await HandleResponse(response).ConfigureAwait(false);
                    // RetryTimes = 0;
                    return result;
                }
                catch (Exception ex)
                {
                    ex = ex.InnerException ?? ex;
#if DEBUG
                    // 此处用于生成请求信息，然后用fiddler等工具测试
                    if (requestItem != null)
                    {
                        var url = requestItem.RawUrl;
                        var header = requestItem.GetRequestHeader(HttpService.GetCookies(requestItem.RawUrl));
                        var data = requestItem.RawData;
                        var len = data.Length;
                    }
#endif
                    // ++RetryTimes;
                    return await HandleExceptionAsync(ex).ConfigureAwait(false);
                }
            }
            return await NotifyActionEventAsync(ActionEvent.CreateEvent(ActionEventType.EvtCanceled, this)).ConfigureAwait(false);
        }
    }
}
<commit_msg>添加常用方法<commit_after>﻿using System;
using System.Threading;
using System.Threading.Tasks;
using HttpAction.Core;
using HttpAction.Event;
using HttpAction.Service;
using System.Linq;

namespace HttpAction.Action
{
    public abstract class AbstractHttpAction : IHttpAction
    {
        protected virtual int MaxReTryTimes { get; set; } = 3;

        protected int ExcuteTimes { get; set; }
        protected int RetryTimes { get; set; }
        protected IHttpService HttpService { get; set; }

        protected AbstractHttpAction(IHttpService httpHttpService)
        {
            HttpService = httpHttpService;
        }

        protected virtual async Task<ActionEvent> NotifyActionEventAsync(ActionEvent actionEvent)
        {
            try
            {
                if (OnActionEvent != null) await OnActionEvent.Invoke(this, actionEvent);
                if (actionEvent.Type == ActionEventType.EvtRetry) ++RetryTimes;
                else RetryTimes = 0;
                return actionEvent;
            }
            catch (Exception ex)
            {
                ++RetryTimes;
                return await HandleExceptionAsync(ex);
            }
        }

        protected Task<ActionEvent> NotifyActionEventAsync(ActionEventType type, object target = null)
        {
            return NotifyActionEventAsync(ActionEvent.CreateEvent(type, target));
        }

        protected Task<ActionEvent> NotifyOkActionEventAsync(object target = null)
        {
            return NotifyActionEventAsync(ActionEventType.EvtOK, target);
        }

        protected Task<ActionEvent> NotifyOkEventAsync(object target = null)
        {
            return NotifyActionEventAsync(ActionEventType.EvtOK, target);
        }

        protected Task<ActionEvent> NotifyErrorEventAsync(Exception ex)
        {
            return NotifyActionEventAsync(ActionEvent.CreateEvent(ActionEventType.EvtError, ex));
        }

        protected Task<ActionEvent> NotifyErrorEventAsync(string msg)
        {
            return NotifyActionEventAsync(ActionEvent.CreateEvent(ActionEventType.EvtError, new Exception(msg)));
        }

        public abstract HttpRequestItem BuildRequest();

        public abstract Task<ActionEvent> HandleResponse(HttpResponseItem response);

        public event ActionEventListener OnActionEvent;

        public virtual async Task<ActionEvent> HandleExceptionAsync(Exception ex)
        {
            try
            {
                var @event = ActionEvent.CreateEvent(RetryTimes < MaxReTryTimes ?
                    ActionEventType.EvtRetry : ActionEventType.EvtError, ex);
                return await NotifyActionEventAsync(@event);
            }
            catch (Exception e)
            {
                throw new Exception($"throw an unhandled exception when excute [{nameof(HandleExceptionAsync)}] method.", e);
            }
        }

        public virtual async Task<ActionEvent> ExecuteAsync(CancellationToken token)
        {
            HttpRequestItem requestItem = null;
            if (!token.IsCancellationRequested)
            {
                ++ExcuteTimes;
                try
                {
                    requestItem = BuildRequest();
                    var response = await HttpService.ExecuteHttpRequestAsync(requestItem, token).ConfigureAwait(false);
                    var result = await HandleResponse(response).ConfigureAwait(false);
                    // RetryTimes = 0;
                    return result;
                }
                catch (Exception ex)
                {
                    ex = ex.InnerException ?? ex;
#if DEBUG
                    // 此处用于生成请求信息，然后用fiddler等工具测试
                    if (requestItem != null)
                    {
                        var url = requestItem.RawUrl;
                        var header = requestItem.GetRequestHeader(HttpService.GetCookies(requestItem.RawUrl));
                        var data = requestItem.RawData;
                        var len = data.Length;
                    }
#endif
                    // ++RetryTimes;
                    return await HandleExceptionAsync(ex).ConfigureAwait(false);
                }
            }
            return await NotifyActionEventAsync(ActionEvent.CreateEvent(ActionEventType.EvtCanceled, this)).ConfigureAwait(false);
        }
    }
}

{'fraction_non_alphanumeric': 0.07173938609902387, 'fraction_numerical': 0.0007056333058920382, 'mean_word_length': 1.8308921438082557, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 20, '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': '402951', 'n_tokens_mistral': 2093, 'n_tokens_neox': 2025, 'n_words': 438}