Ejemplo n.º 1
0
        /// <summary>
        /// Generate CodeDom of the client API for ApiDescriptions.
        /// </summary>
        /// <param name="descriptions">Web Api descriptions exposed by Configuration.Services.GetApiExplorer().ApiDescriptions</param>
        public void CreateCodeDom(OpenApiPaths paths, OpenApiComponents components)
        {
            if (paths == null && components == null)
            {
                return;
            }

            clientNamespace = new CodeNamespace(settings.ClientNamespace);
            CodeCompileUnit.Namespaces.Add(clientNamespace);            //namespace added to Dom

            ComponentsToTsTypes componentsToTsTypes = new ComponentsToTsTypes(settings, CodeCompileUnit, clientNamespace);

            componentsToTsTypes.CreateCodeDom(components);

            if (paths == null)
            {
                return;
            }

            AddBasicReferences();

            string[] containerClassNames = GetContainerClassNames(paths);

            CodeTypeDeclaration[] newClassesCreated = containerClassNames.Select(d => CreateControllerClientClass(clientNamespace, d)).ToArray();

            foreach (KeyValuePair <string, OpenApiPathItem> p in paths)
            {
                string relativePath = p.Key;
                foreach (KeyValuePair <OperationType, OpenApiOperation> op in p.Value.Operations)
                {
                    ClientApiTsFunctionGenAbstract apiFunctionGen = apiFunctionGenFactory();
                    CodeMemberMethod apiFunction = apiFunctionGen.CreateApiFunction(settings, relativePath, op.Key, op.Value, componentsToTsTypes);
                    if (apiFunction == null)
                    {
                        System.Diagnostics.Trace.TraceWarning($"Not to generate TS for {p.Key} {op.Key}.");
                        continue;
                    }

                    string containerClassName         = nameComposer.GetContainerName(op.Value, p.Key);
                    CodeTypeDeclaration existingClass = LookupExistingClass(containerClassName);
                    existingClass.Members.Add(apiFunction);
                }
            }


            foreach (CodeTypeDeclaration c in newClassesCreated)
            {
                AddHelperFunctionsInClass(c);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generate CodeDom of the client API for ApiDescriptions.
        /// </summary>
        /// <param name="descriptions">Web Api descriptions exposed by Configuration.Services.GetApiExplorer().ApiDescriptions</param>
        public void CreateCodeDom(OpenApiPaths paths, OpenApiComponents components)
        {
            if (paths == null && components == null)
            {
                return;
            }

            clientNamespace = new CodeNamespace(settings.ClientNamespace);
            codeCompileUnit.Namespaces.Add(clientNamespace);            //namespace added to Dom

            ComponentsToCsTypes componentsToCsTypes = new ComponentsToCsTypes(settings, codeCompileUnit, clientNamespace);

            componentsToCsTypes.CreateCodeDom(components);

            if (paths == null)
            {
                return;
            }

            clientNamespace.Imports.AddRange(new CodeNamespaceImport[] {
                new CodeNamespaceImport("System"),
                new CodeNamespaceImport("System.Linq"),
                new CodeNamespaceImport("System.Collections.Generic"),
                new CodeNamespaceImport("System.Threading.Tasks"),
                new CodeNamespaceImport("System.Net.Http"),
                new CodeNamespaceImport("Newtonsoft.Json"),
            });

            if (settings.UseEnsureSuccessStatusCodeEx)
            {
                clientNamespace.Imports.Add(new CodeNamespaceImport("Fonlow.Net.Http"));
            }

            string[] containerClassNames = GetContainerClassNames(paths);

            CodeTypeDeclaration[] newClassesCreated = containerClassNames.Select(d => CreateControllerClientClass(clientNamespace, d)).ToArray();

            foreach (KeyValuePair <string, OpenApiPathItem> p in paths)
            {
                foreach (KeyValuePair <OperationType, OpenApiOperation> op in p.Value.Operations)
                {
                    ClientApiFunctionGen apiFunctionGen = new ClientApiFunctionGen();
                    CodeMemberMethod     apiFunction    = apiFunctionGen.CreateApiFunction(settings, p.Key, op.Key, op.Value, componentsToCsTypes, true, settings.UseEnsureSuccessStatusCodeEx);
                    if (apiFunction == null)
                    {
                        System.Diagnostics.Trace.TraceWarning($"Not to generate C# for {p.Key} {op.Key}.");
                        continue;
                    }

                    string containerClassName         = nameComposer.GetContainerName(op.Value, p.Key);
                    CodeTypeDeclaration existingClass = LookupExistingClass(containerClassName);

                    existingClass.Members.Add(apiFunction);
                    if (settings.GenerateBothAsyncAndSync)
                    {
                        ClientApiFunctionGen functionGen2 = new ClientApiFunctionGen();
                        existingClass.Members.Add(functionGen2.CreateApiFunction(settings, p.Key, op.Key, op.Value, componentsToCsTypes, false, settings.UseEnsureSuccessStatusCodeEx));
                    }
                }
            }

            if (settings.UseEnsureSuccessStatusCodeEx && settings.IncludeEnsureSuccessStatusCodeExBlock)
            {
                CreateDummyOfEnsureSuccessStatusCodeEx();
            }
        }