public ClientGeneratorModel BuildModel()
        {
            warnings = new Dictionary<string, string>();
            classesNames = new Collection<string>();
            classes = new Collection<ClassObject>();
            classesObjectsRegistry = new Dictionary<string, ClassObject>();
            uriParameterObjects = new Dictionary<string, ApiObject>();
            enums = new Dictionary<string, ApiEnum>();

            ParseSchemas();
            schemaRequestObjects = GetRequestObjects();
            schemaResponseObjects = GetResponseObjects();

            CleanProperties(schemaObjects);
            CleanProperties(schemaRequestObjects);
            CleanProperties(schemaResponseObjects);

            clientMethodsGenerator = new ClientMethodsGenerator(raml, schemaResponseObjects, uriParameterObjects,
                queryObjects, headerObjects, responseHeadersObjects, schemaRequestObjects, linkKeysWithObjectNames,
                schemaObjects);

            var parentClass = new ClassObject { Name = rootClassName, Description = "Main class for grouping root resources. Nested resources are defined as properties. The constructor can optionally receive an URL and HttpClient instance to override the default ones." };
            classesNames.Add(parentClass.Name);
            classes.Add(parentClass);
            classesObjectsRegistry.Add(rootClassName, parentClass);

            var classObjects = GetClasses(raml.Resources, null, parentClass, null);
            SetClassesProperties(classesObjectsRegistry[rootClassName]);

            var apiRequestObjects = apiRequestGenerator.Generate(classObjects);
            var apiResponseObjects = apiResponseGenerator.Generate(classObjects);

            CleanNotUsedObjects(classObjects);

            return new ClientGeneratorModel
                   {
                       Namespace = NetNamingMapper.GetNamespace(raml.Title),
                       SchemaObjects = schemaObjects,
                       RequestObjects = schemaRequestObjects,
                       ResponseObjects = schemaResponseObjects,
                       QueryObjects = queryObjects,
                       HeaderObjects = headerObjects,
                       ApiRequestObjects = apiRequestObjects,
                       ApiResponseObjects = apiResponseObjects,
                       ResponseHeaderObjects = responseHeadersObjects,
                       BaseUriParameters = ParametersMapper.Map(raml.BaseUriParameters),
                       BaseUri = raml.BaseUri,
                       Security = SecurityParser.GetSecurity(raml),
                       Version = raml.Version,
                       Warnings = warnings,
                       Classes = classObjects.Where(c => c.Name != rootClassName).ToArray(),
                       Root = classObjects.First(c => c.Name == rootClassName),
                       UriParameterObjects = uriParameterObjects,
                       Enums = Enums
                   };
        }
        public void Should_Generate_One_Method_Per_Verb()
        {
            var methods = new List<Method>
                          {
                              new Method
                              {
                                  Verb = "get"
                              },
                              new Method
                              {
                                  Verb = "post"
                              }
                          };

            var resource = new Resource
            {
                RelativeUri = "/abc{token}{code}",
                Methods = methods,
            };

            var schemaResponseObjects = new Dictionary<string, ApiObject>();
            var schemaRequestObjects = new Dictionary<string, ApiObject>();
            var ramlDocument = new RamlDocument();
            var uriParameterObjects = new Dictionary<string, ApiObject>();
            var queryObjects = new Dictionary<string, ApiObject>();
            var headerObjects = new Dictionary<string, ApiObject>();
            var responseHeadersObjects = new Dictionary<string, ApiObject>();
            var linkedKeyWithObjectNames = new Dictionary<string, string>();
            var classObject = new ClassObject();
            var schemaObjects = new Dictionary<string, ApiObject>();

            var generator = new ClientMethodsGenerator(ramlDocument, schemaResponseObjects,
                uriParameterObjects, queryObjects, headerObjects, responseHeadersObjects, schemaRequestObjects, linkedKeyWithObjectNames,
                schemaObjects);

            var generatorMethods = generator.GetMethods(resource, "/", classObject, "Test", new Dictionary<string, Parameter>());

            Assert.AreEqual(2, generatorMethods.Count);
        }
示例#3
0
        public ClientGeneratorModel BuildModel()
        {
            warnings = new Dictionary<string, string>();
            classesNames = new Collection<string>();
            classes = new Collection<ClassObject>();
            classesObjectsRegistry = new Dictionary<string, ClassObject>();
            uriParameterObjects = new Dictionary<string, ApiObject>();
            enums = new Dictionary<string, ApiEnum>();

            var ns = NetNamingMapper.GetNamespace(raml.Title);
            new RamlTypeParser(raml.Types, schemaObjects, ns, enums, warnings).Parse();

            ParseSchemas();
            schemaRequestObjects = GetRequestObjects();
            schemaResponseObjects = GetResponseObjects();

            CleanProperties(schemaObjects);
            CleanProperties(schemaRequestObjects);
            CleanProperties(schemaResponseObjects);

            clientMethodsGenerator = new ClientMethodsGenerator(raml, schemaResponseObjects, uriParameterObjects,
                queryObjects, headerObjects, responseHeadersObjects, schemaRequestObjects, linkKeysWithObjectNames,
                schemaObjects);

            var parentClass = new ClassObject { Name = rootClassName, Description = "Main class for grouping root resources. Nested resources are defined as properties. The constructor can optionally receive an URL and HttpClient instance to override the default ones." };
            classesNames.Add(parentClass.Name);
            classes.Add(parentClass);
            classesObjectsRegistry.Add(rootClassName, parentClass);

            var classObjects = GetClasses(raml.Resources, null, parentClass, null, new Dictionary<string, Parameter>());
            SetClassesProperties(classesObjectsRegistry[rootClassName]);

            var apiRequestObjects = apiRequestGenerator.Generate(classObjects);
            var apiResponseObjects = apiResponseGenerator.Generate(classObjects);

            CleanNotUsedObjects(classObjects);


            
            return new ClientGeneratorModel
                   {
                       Namespace = ns,
                       SchemaObjects = schemaObjects,
                       RequestObjects = schemaRequestObjects,
                       ResponseObjects = schemaResponseObjects,
                       QueryObjects = queryObjects,
                       HeaderObjects = headerObjects,

                       ApiRequestObjects = apiRequestObjects.ToArray(),
                       ApiResponseObjects = apiResponseObjects.ToArray(),
                       ResponseHeaderObjects = responseHeadersObjects,

                       BaseUriParameters = ParametersMapper.Map(raml.BaseUriParameters).ToArray(),
                       BaseUri = raml.BaseUri,
                       Security = SecurityParser.GetSecurity(raml),
                       Version = raml.Version,
                       Warnings = warnings,
                       Classes = classObjects.Where(c => c.Name != rootClassName).ToArray(),
                       Root = classObjects.First(c => c.Name == rootClassName),
                       UriParameterObjects = uriParameterObjects,
                       Enums = Enums.ToArray()
                   };
        }
 public ClientGeneratorService(RamlDocument raml, string rootClassName)
     : base(raml)
 {
     clientMethodsGenerator = new ClientMethodsGenerator(raml);
     this.rootClassName     = rootClassName;
 }