예제 #1
0
 private string getMethodName(ApiMethodDesc method, string httpMethod)
 {
     return(method.TsName + (method.HttpMethods.Count == 1 ? "" : httpMethod.Substring(0, 1) + httpMethod.Substring(1).ToLower()));
 }
예제 #2
0
        protected override ApiMethodDesc GetMethodDesc(ApiServiceDesc service, MethodInfo method)
        {
            var controllerRoute       = service.Controller.GetAttributes("System.Web.Http.RouteAttribute").SingleOrDefault();
            var controllerRoutePrefix = service.Controller.GetAttributes("System.Web.Http.RoutePrefixAttribute").SingleOrDefault();
            var methodRoute           = method.GetAttributes("System.Web.Http.RouteAttribute").SingleOrDefault();

            if (controllerRoute == null && methodRoute == null)
            {
                return(null);
            }
            var iHttpMethod     = "System.Web.Http.Controllers.IActionHttpMethodProvider";
            var httpMethodsAttr = method.GetAttributesByInterface(iHttpMethod).SingleOrDefault();

            if (httpMethodsAttr == null)
            {
                return(null);
            }

            var m = new ApiMethodDesc();

            m.Method       = method;
            m.Service      = service;
            m.HttpMethods  = ((IEnumerable <object>)httpMethodsAttr.ReadProperty("HttpMethods", Reflection.KnownTypes[iHttpMethod])).Select(hm => ((string)hm.ReadProperty("Method")).ToUpper()).ToList();
            m.BodyEncoding = BodyEncoding.Json;
            m.TsName       = method.Name;
            m.TsReturnType = MapType(method.ReturnType);
            if (m.TsReturnType == null)
            {
                throw new InvalidOperationException($"Cannot map return type {method.ReturnType.FullName} of method {method.Name}, controller {service.Controller.FullName}");
            }
            foreach (var par in method.GetParameters())
            {
                var p = new ApiMethodParameterDesc();
                m.Parameters.Add(p);
                p.Method    = m;
                p.Parameter = par;
                p.TsName    = par.Name;
                p.TsType    = MapType(par.ParameterType);
                if (p.TsType == null)
                {
                    throw new InvalidOperationException($"Cannot map parameter type {par.ParameterType.FullName} of parameter {par.Name}, method {method.Name}, controller {service.Controller.FullName}");
                }
                p.Location = IsUrlParameter(p) ? ParameterLocation.QueryString : ParameterLocation.RequestBody;
            }
            m.UrlPath = "/";
            if (controllerRoutePrefix != null)
            {
                m.UrlPath += (string)controllerRoutePrefix.ReadProperty("Prefix") + "/";
            }
            if (methodRoute != null)
            {
                m.UrlPath += (string)methodRoute.ReadProperty("Template");
            }
            else // controllerRoute != null
            {
                m.UrlPath += (string)controllerRoute.ReadProperty("Template");
            }
            m.UrlPath = m.UrlPath.Substring(1);

            var pars = m.Parameters.ToDictionary(p => p.TsName);

            m.UrlPath = Regex.Replace(m.UrlPath, @"{([a-zA-Z0-9]+)}", match =>
            {
                var parName = match.Groups[1].Value;
                if (!pars.ContainsKey(parName))
                {
                    throw new InvalidOperationException($"No matching method parameter found for URL segment \"{match.Value}\", method {method.Name}, controller {service.Controller.FullName}");
                }
                pars[parName].Location = ParameterLocation.UrlSegment;
                return("${encodeURIComponent('' + " + parName + ")}");
            });

            return(m);
        }