예제 #1
0
        public Route GetRoute(RESTMethods Method, string URL)
        {
            Dictionary <string, RouteBinding> methodDictionary;
            Route route;
            Match match;

            if (string.IsNullOrEmpty(URL))
            {
                throw new ArgumentNullException(nameof(URL));
            }

            methodDictionary = GetMethodDictionary(Method);

            foreach (KeyValuePair <string, RouteBinding> keyValuePair in methodDictionary)
            {
                match = Regex.Match(URL, keyValuePair.Key);
                if (!match.Success)
                {
                    continue;
                }

                route = new Route()
                {
                    Binding = keyValuePair.Value
                };

                foreach (Group group in match.Groups)
                {
                    route.Set(group.Name, WebUtility.UrlDecode(group.Value));
                }

                return(route);
            }
            throw new RouteNotFoundException(URL);
        }
예제 #2
0
        private Dictionary <string, RouteBinding> GetMethodDictionary(RESTMethods Method)
        {
            switch (Method)
            {
            case RESTMethods.GET: return(getRouteDictionary);

            case RESTMethods.PUT: return(putRouteDictionary);

            case RESTMethods.POST: return(postRouteDictionary);

            case RESTMethods.DELETE: return(deleteRouteDictionary);

            default: throw new InvalidParameterException($"REST Method {Method} not supported");
            }
        }
예제 #3
0
        public Response GetResponse(RESTMethods Method, string URL)
        {
            Route         route;
            List <object> parameters;
            object        result;

            if (string.IsNullOrEmpty(URL))
            {
                throw new ArgumentNullException(nameof(URL));
            }

            route      = GetRoute(Method, URL);
            parameters = new List <object>();


            foreach (ParameterInfo pi in route.Binding.MethodInfo.GetParameters())
            {
                try
                {
                    parameters.Add(Convert.ChangeType(route.Get(pi.Name), pi.ParameterType));
                }
                catch
                {
                    throw new InvalidParameterException(URL);
                }
            }

            try
            {
                result = route.Binding.MethodInfo.Invoke(route.Binding.RouteHandler, parameters.ToArray());
                if (result is Response response)
                {
                    return(response);
                }

                return(serializer.Serialize(result));
            }
            catch
            {
                return(Response.InternalError);
            }
        }
예제 #4
0
        public RouteBinding BindRoute(IRouteHandler RouteHandler, MethodInfo MethodInfo, RESTMethods Method, string Pattern)
        {
            Dictionary <string, RouteBinding> methodDictionary;

            ParameterInfo[] pis;
            ParameterInfo   pi;
            RouteBinding    binding;
            Regex           regex;

            string[] variables;

            if (RouteHandler == null)
            {
                throw new ArgumentNullException(nameof(RouteHandler));
            }
            if (MethodInfo == null)
            {
                throw new ArgumentNullException(nameof(MethodInfo));
            }
            if (string.IsNullOrEmpty(Pattern))
            {
                throw new ArgumentNullException(nameof(Pattern));
            }

            pis = MethodInfo.GetParameters();

            methodDictionary = GetMethodDictionary(Method);
            if (methodDictionary.ContainsKey(Pattern))
            {
                throw new DuplicateRouteException(MethodInfo.Name);
            }

            regex     = new Regex(Pattern);
            variables = regex.GetGroupNames().Skip(1).ToArray();

            if (variables.Count() != pis.Length)
            {
                throw new InvalidRouteException(MethodInfo.Name);
            }
            foreach (string variable in variables)
            {
                pi = pis.FirstOrDefault(item => item.Name == variable);
                if (pi == null)
                {
                    throw new InvalidRouteException(MethodInfo.Name);
                }
            }


            binding = new RouteBinding()
            {
                MethodInfo = MethodInfo, RouteHandler = RouteHandler
            };

            methodDictionary.Add(Pattern, binding);


            return(binding);
        }
예제 #5
0
 public RouteAttribute(RESTMethods Method, string URL)
 {
     this.Method = Method; this.URL = URL;
 }