Skip to content
Snippets Groups Projects
Select Git revision
  • c992191f57e090b1c3fffb1779cd8f4eb4c49f98
  • main default protected
  • gitkeep
  • dev protected
  • Issue/xxxx-configurableApiHostname
  • Issue/2732-updatedApiClient
  • APIv2
  • Issue/2518-docs
  • Hotfix/2427-adminTrouble
  • Issue/1910-MigrationtoNET6.0
  • Issue/1833-newLogin
  • Sprint/2022-01
  • Sprint/2021-23
  • Issue/1745-coscineConnection
  • x/setup
15 results

CoscineCodeGenerator.cs

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    CoscineCodeGenerator.cs 3.04 KiB
    using Coscine.Configuration;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Threading.Tasks;
    
    namespace Coscine.CodeGen.CodeGenerator
    {
        public class CoscineCodeGenerator : CodeGenerator
        {
            private readonly IConfiguration _configuration;
    
            public CoscineCodeGenerator(IConfiguration configuration)
            {
                this._configuration = configuration;
            }
    
            public async override Task<string> GetClientGenerator()
            {
                var jarDownloadLink = await _configuration.GetStringAsync("coscine/local/codegen/jarlink",
                                "https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/5.2.1/openapi-generator-cli-5.2.1.jar"
                            );
    
                var webClient = new WebClient();
    
                var jarFileName = await _configuration.GetStringAsync("coscine/local/codegen/jarpath", "codegen.jar");
    
                await webClient.DownloadFileTaskAsync(new Uri(jarDownloadLink), jarFileName);
                return jarFileName;
            }
    
            public async override Task<IEnumerable<string>> GetApiNames()
            {
                var apiPrefix = "coscine/apis/";
    
                var keys = await _configuration.KeysAsync(apiPrefix);
                return keys.Select((entry) => entry.Split('/')[2]).Distinct();
            }
    
            internal async override Task<string> GetOutputPath()
            {
                return await _configuration.GetStringAsync("coscine/local/codegen/outputpath", "Output");
            }
    
            internal override Task<string> GetSwaggerUrl(string domainName, string hostName, string key)
            {
                return Task.FromResult($"https://{hostName}.{domainName}/coscine/api/{key}/swagger/v1/swagger.json");
            }
    
            internal override string GetGenerationCommand(string outputPath, string jarFileName, string key, string swaggerUrl)
            {
                return $"java \"-Dio.swagger.parser.util.RemoteUrl.trustAll=true\" \"-Dio.swagger.v3.parser.util.RemoteUrl.trustAll=true\" -jar \"{jarFileName}\" generate -i \"{swaggerUrl}\" -g typescript-axios -o \"{outputPath}/{key}\" --skip-validate-spec";
            }
    
            internal override Task<string> GetCustomBasePath(string directoryName)
            {
                return Task.FromResult($"https://' + getHostName() + '/coscine/api/{directoryName}");
            }
    
            internal override Task<string> GetCustomCodeForCombinationFile(string combinationFileText)
            {
                combinationFileText += "let accessToken = '';";
    
                combinationFileText += @"
    if (typeof coscine !== 'undefined') {
      accessToken = coscine.authorization.bearer;
    }
    
    const getHostName = () => {