Esempio n. 1
0
        public ApiActionDesc AddAction(string name, Type returnType, ApiMethod method = ApiMethod.Get, string route = null, IEnumerable <ApiParamDesc> parameters = null)
        {
            var action = new ApiActionDesc();

            action.ActionName = name;

            action.ReturnType = returnType;
            action.Method     = method;

            if (!String.IsNullOrWhiteSpace(route))
            {
                action.RouteTemplate = route;
            }

            if (parameters != null)
            {
                action.Parameters.AddRange(parameters);
            }

            Actions.Add(action);
            return(action);
        }
Esempio n. 2
0
        protected virtual string BuildUrlString(ApiActionDesc action, string template, ApiParamDesc[] getParameters, ApiParamDesc modelParameter)
        {
            List <string>       routeJs         = new List <string>();
            List <string>       queryJs         = new List <string>();
            List <ApiParamDesc> routeParameters = new List <ApiParamDesc>();

            bool seenOptionalRoute = false;

            string GetParamExecString(ApiParamDesc p)
            {
                var name = modelParameter == null ? p.ParameterName : $"{modelParameter.ParameterName}.{p.ParameterName}";
                var imm  = CreateTypeInitializerMethod(p.ParameterType);

                if (imm != null)
                {
                    return($"from_{imm}({name})");
                }
                return(name);
            }

            var parts = template.Split('/');

            for (int i = 0; i < parts.Length; i++)
            {
                var part = parts[i];
                if (!part.StartsWith("{"))
                {
                    routeJs.Add("\"" + part + "\"");
                    continue;
                }

                part = part.Substring(1, part.Length - 2);

                var typeIndex = part.IndexOf(":");
                if (typeIndex > 0)
                {
                    part = part.Substring(0, typeIndex);
                }

                if (part.StartsWith("*"))
                {
                    part = part.Substring(1);
                }

                var templateOptional = part.EndsWith("?");
                part = part.TrimEnd('?');

                if (templateOptional)
                {
                    seenOptionalRoute = true;
                }
                else if (seenOptionalRoute == true)
                {
                    throw new Exception($"In action '{action.ActionName}', required route parameter must not come after an optional route parameter in template: '{template}'.");
                }

                var get = getParameters.FirstOrDefault(g => g.ParameterName.Equals(part));
                if (get == null)
                {
                    throw new Exception($"In action '{action.ActionName}', route parameter `{part}` does not match any available method parameters. " +
                                        $"Please check your route template: '{template}'." +
                                        $"Parameters: [{String.Join(", ", action.Parameters.Select(s => s.ParameterName))}]");
                }

                if (templateOptional != get.IsOptional)
                {
                    bool canBeNull = !get.ParameterType.GetDnxCompatible().IsValueType || (Nullable.GetUnderlyingType(get.ParameterType) != null);
                    if (!canBeNull)
                    {
                        throw new Exception($"In action '{action.ActionName}', route parameter `{part}` is marked optional={get.IsOptional}, but the route " +
                                            $"template '{template}' is marked optional={templateOptional} and the type is non-nullable so is required.");
                    }
                }

                var typeCode = Type.GetTypeCode(get.ParameterType);
                switch (typeCode)
                {
                case TypeCode.DateTime:
                case TypeCode.Object:
                    throw new Exception($"In action '{action.ActionName}' parameter type '{get.ParameterType.Name}' is not suitable " +
                                        $"as a route parameter for template '{template}'. (Type code: {typeCode})");
                }

                routeParameters.Add(get);
                routeJs.Add($"[{GetParamExecString(get)}, \"{get.ParameterName}\"]");
            }

            foreach (var q in getParameters.Except(routeParameters))
            {
                queryJs.Add($"[{GetParamExecString(q)}, \"{q.ParameterName}\", {q.IsOptional.ToString().ToLower()}]");
            }

            return($"_build_url(this._basePath, [{String.Join(", ", routeJs)}], [{String.Join(", ", queryJs)}])");
        }