Beispiel #1
0
 public WebApiMethodsGenerator(RamlDocument raml, IDictionary <string, ApiObject> schemaResponseObjects,
                               IDictionary <string, ApiObject> schemaRequestObjects, IDictionary <string, string> linkKeysWithObjectNames,
                               IDictionary <string, ApiObject> schemaObjects)
     : base(raml, schemaResponseObjects, schemaRequestObjects, linkKeysWithObjectNames, schemaObjects)
 {
     queryParametersParser = new QueryParametersParser(schemaObjects);
 }
Beispiel #2
0
        public void Generate(Resource resource, string url, ClientGeneratorMethod clientGeneratorMethod,
                             IDictionary <string, ApiObject> uriParameterObjects, IDictionary <string, Parameter> parentUriParameters)
        {
            var parameters = GetUriParameters(resource, url, parentUriParameters).ToArray();

            clientGeneratorMethod.UriParameters = parameters;

            if (!parameters.Any())
            {
                return;
            }

            var name = NetNamingMapper.GetObjectName(url) + "UriParameters";

            clientGeneratorMethod.UriParametersType = name;
            if (uriParameterObjects.ContainsKey(name))
            {
                return;
            }

            var properties = new List <Property>();

            if (resource.BaseUriParameters != null)
            {
                properties.AddRange(QueryParametersParser.ConvertParametersToProperties(resource.BaseUriParameters));
            }

            if (resource.UriParameters != null)
            {
                properties.AddRange(QueryParametersParser.ConvertParametersToProperties(resource.UriParameters)
                                    .Where(up => properties.All(p => !String.Equals(up.Name, p.Name, StringComparison.InvariantCultureIgnoreCase))));
            }

            var urlParameters     = ExtractParametersFromUrl(url).ToArray();
            var matchedParameters = MatchParameters(parentUriParameters, urlParameters);

            foreach (var urlParameter in matchedParameters)
            {
                var property = ConvertGeneratorParamToProperty(urlParameter);
                if (properties.All(p => !String.Equals(property.Name, p.Name, StringComparison.InvariantCultureIgnoreCase)))
                {
                    properties.Add(property);
                }
            }

            var apiObject = new ApiObject
            {
                Name        = name,
                Description = "Uri Parameters for resource " + resource.RelativeUri,
                Properties  = properties
            };

            uriParameterObjects.Add(name, apiObject);
        }
        private IEnumerable <Property> GetSecurityParameters(RamlDocument ramlDocument, Method method)
        {
            var securityParams = new Collection <Property>();

            if (ramlDocument.SecuritySchemes == null || !ramlDocument.SecuritySchemes.Any())
            {
                return(securityParams);
            }

            if ((ramlDocument.SecuredBy == null || !ramlDocument.SecuredBy.Any()) &&
                (method.SecuredBy == null || !method.SecuredBy.Any()))
            {
                return(securityParams);
            }

            var securedBy = method.SecuredBy != null && method.SecuredBy.Any() ? method.SecuredBy : ramlDocument.SecuredBy;

            if (securedBy == null)
            {
                return(securityParams);
            }

            var secured = securedBy.First();

            var dic = ramlDocument.SecuritySchemes.FirstOrDefault(s => s.ContainsKey(secured));

            if (dic == null)
            {
                return(securityParams);
            }

            var descriptor = ramlDocument.SecuritySchemes.First(s => s.ContainsKey(secured))[secured].DescribedBy;

            if (descriptor == null || descriptor.QueryParameters == null || !descriptor.QueryParameters.Any())
            {
                return(securityParams);
            }

            return(QueryParametersParser.ConvertParametersToProperties(descriptor.QueryParameters));
        }
        public IEnumerable <ControllerMethod> GetMethods(Resource resource, string url, ControllerObject parent, string objectName, IDictionary <string, Parameter> parentUriParameters)
        {
            var methodsNames = new List <string>();

            if (parent != null && parent.Methods != null)
            {
                methodsNames = parent.Methods.Select(m => m.Name).ToList();
            }

            var generatorMethods = new Collection <ControllerMethod>();

            if (resource.Methods == null)
            {
                return(generatorMethods);
            }

            foreach (var method in resource.Methods)
            {
                var generatedMethod = BuildControllerMethod(url, method, resource, parent, parentUriParameters);

                if (IsVerbForMethod(method))
                {
                    if (methodsNames.Contains(generatedMethod.Name))
                    {
                        generatedMethod.Name = GetUniqueName(methodsNames, generatedMethod.Name, resource.RelativeUri);
                    }

                    if (method.QueryParameters != null && method.QueryParameters.Any())
                    {
                        var queryParameters = QueryParametersParser.ParseParameters(method);
                        generatedMethod.QueryParameters = queryParameters;
                    }

                    generatorMethods.Add(generatedMethod);
                    methodsNames.Add(generatedMethod.Name);
                }
            }

            return(generatorMethods);
        }
 public UriParametersGenerator(IDictionary <string, ApiObject> schemaObjects)
 {
     this.schemaObjects    = schemaObjects;
     queryParametersParser = new QueryParametersParser(schemaObjects);
 }