private static IEnumerable <ClassModel> GenerateModels(IEnumerable <Schema> schemas)
        {
            int i = 0;

            foreach (Schema s in schemas)
            {
                var sItems = s.Items as Schema;

                try
                {
                    if (s.Type.Name == "array" &&
                        sItems.Type.Name != "object" &&
                        sItems.Type.Name != "array")
                    {
                        continue;
                    }

                    string className = ClassNameFactory.Create(s);

                    if (className is null)
                    {
                        continue;
                    }

                    if (className.ToLower().Contains("customfield"))
                    {
                        continue;
                    }

                    var ns = s.Endpoint.Group.CleanForCode();

                    ClassModel gen = ModelCSGen.Generate(s, $"MAD.API.Procore.Endpoints.{ns}.Models", className);;

                    if (gen is null)
                    {
                        continue;
                    }

                    yield return(gen);
                }
                finally
                {
                    i++;
                }
            }
        }
        public static IEnumerable <ClassModel> Generate(Endpoint endpoint)
        {
            EndpointResponse     okResponse = endpoint.Responses.FirstOrDefault(y => y.Status >= 200 && y.Status <= 299);
            IEnumerable <Schema> schemas    = GetNestedSchemas(okResponse.Schema);

            IEnumerable <ClassModel> schemaClassModels = GenerateModels(schemas)
                                                         .OrderByDescending(y => y.Properties.Count)
                                                         .GroupBy(y => y.Name)
                                                         .Select(y => y.First());

            Schema responseType        = schemas.First();
            string requestResponseType = ClassNameFactory.Create(responseType);

            ClassModel responseTypeClassModel = schemaClassModels.First(y => y.Name == requestResponseType);

            if (responseTypeClassModel.BaseClass is null &&
                responseType.Type.Name == "array")
            {
                requestResponseType = $"IEnumerable<{requestResponseType}>";
            }

            ClassModel procoreRequestModel = new ClassModel
            {
                Name     = "ProcoreRequest",
                Generics =
                {
                    requestResponseType
                }
            };

            var requestNamespace = $"MAD.API.Procore.Endpoints.{endpoint.Group.CleanForCode()}";

            ClassModel endpointRequestModel = new ClassModel
            {
                Name           = $"{endpoint.Summary.CleanForCode()}Request",
                BaseClass      = procoreRequestModel,
                AccessModifier = "public",
                Usings         =
                {
                    "System",
                    "System.Text",
                    "Newtonsoft.Json",
                    "Newtonsoft.Json.Linq",
                    "System.Collections.Generic",
                    $"{requestNamespace}.Models",
                    "MAD.API.Procore",
                    "MAD.API.Procore.Requests"
                },
                Namespace = requestNamespace
            };

            endpointRequestModel.Properties.Add(new PropertyModel
            {
                Name     = "Resource",
                Getter   = $"$\"{FormatResourceStringForCSharpStringInterop(endpoint.Path)}\";",
                Type     = "string",
                Override = true
            });

            if (endpoint.Verb != "get")
            {
                endpointRequestModel.Usings.Add("System.Net.Http");
                endpointRequestModel.Properties.Add(new PropertyModel
                {
                    Name       = nameof(HttpMethod),
                    Override   = true,
                    IsNullable = false,
                    Type       = nameof(HttpMethod),
                    Getter     = $"HttpMethod.{CultureInfo.CurrentCulture.TextInfo.ToTitleCase(endpoint.Verb)}"
                });
            }

            foreach (BaseParam pp in endpoint.PathParams)
            {
                if (string.IsNullOrEmpty(pp.Name))
                {
                    continue;
                }

                endpointRequestModel.Properties.Add(GeneratePropertyModel(pp, false));
            }

            foreach (BaseParam qp in endpoint.QueryParams)
            {
                if (string.IsNullOrEmpty(qp.Name))
                {
                    continue;
                }

                if (qp.Name == "page" ||
                    qp.Name == "per_page")
                {
                    continue;
                }

                endpointRequestModel.Properties.Add(GeneratePropertyModel(qp, true));
            }

            yield return(endpointRequestModel);

            foreach (ClassModel i in schemaClassModels)
            {
                yield return(i);
            }
        }
Beispiel #3
0
        public async Task TestMethod1()
        {
            var endpointModels = await RemoteJsonFile.GetJsonFile <IEnumerable <EndpointModel> >("checklists");

            var schemas = Program.GetSchemasFromEndpointModels(endpointModels);

            foreach (var x in schemas)
            {
                var nested = Program.GetNestedSchemas(x);

                foreach (var y in nested)
                {
                    string generatedCode = CSGen.Generate(y, "MAD.API.Procore.Models", ClassNameFactory.Create(y));
                }
            }
        }