<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Dolstagis.Web.Http;
using Dolstagis.Web.Routing;

namespace Dolstagis.Web.Lifecycle
{
    public class RequestContextBuilder : IRequestContextBuilder
    {
        private RouteTable _routes;
        private Func<ActionInvocation> _createAction;

        public RequestContextBuilder(RouteTable routes, Func<ActionInvocation> createAction)
        {
            _routes = routes;
            _createAction = createAction;
        }

        public ActionInvocation GetAction(IRequest request)
        {
            var route = _routes.Lookup(request.AppRelativePath).FirstOrDefault();
            if (route == null) return null;
            return GetAction(request, route);
        }

        public IEnumerable<ActionInvocation> GetActions(IRequest request)
        {
            return _routes.Lookup(request.AppRelativePath)
                .Select(route => GetAction(request, route));
        }

        private ActionInvocation GetAction(IRequest request, RouteInfo route)
        {
            var action = _createAction();
            action.HandlerType = route.Definition.HandlerType;
            var method = action.HandlerType.GetMethod(request.Method,
                BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.DeclaredOnly);
            if (method == null) return action;
            action.Method = method;
            var args = new List<object>();
            foreach (var parameter in method.GetParameters())
            {
                string arg;
                if (route.Arguments.TryGetValue(parameter.Name, out arg))
                {
                    args.Add(arg);
                }
                else if (parameter.IsOptional)
                {
                    args.Add(parameter.HasDefaultValue ? parameter.DefaultValue : null);
                }
                else
                {<commit_msg>Remove unused method.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Dolstagis.Web.Http;
using Dolstagis.Web.Routing;

namespace Dolstagis.Web.Lifecycle
{
    public class RequestContextBuilder : IRequestContextBuilder
    {
        private RouteTable _routes;
        private Func<ActionInvocation> _createAction;

        public RequestContextBuilder(RouteTable routes, Func<ActionInvocation> createAction)
        {
            _routes = routes;
            _createAction = createAction;
        }

        public IEnumerable<ActionInvocation> GetActions(IRequest request)
        {
            return _routes.Lookup(request.AppRelativePath)
                .Select(route => GetAction(request, route));
        }

        private ActionInvocation GetAction(IRequest request, RouteInfo route)
        {
            var action = _createAction();
            action.HandlerType = route.Definition.HandlerType;
            var method = action.HandlerType.GetMethod(request.Method,
                BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.DeclaredOnly);
            if (method == null) return action;
            action.Method = method;
            var args = new List<object>();
            foreach (var parameter in method.GetParameters())
            {
                string arg;
                if (route.Arguments.TryGetValue(parameter.Name, out arg))
                {
                    args.Add(arg);
                }
                else if (parameter.IsOptional)
                {
                    args.Add(parameter.HasDefaultValue ? parameter.DefaultValue : null);
                }
                else
                {
{'fraction_non_alphanumeric': 0.07848761408083442, 'fraction_numerical': 0.0, 'mean_word_length': 1.994535519125683, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '5521855', 'n_tokens_mistral': 946, 'n_tokens_neox': 917, 'n_words': 205}