Skip to content
Snippets Groups Projects
Commit b6550fc6 authored by Benedikt Heinrichs's avatar Benedikt Heinrichs
Browse files

Generate correct axios api clients

parent 13ee686f
No related branches found
No related tags found
1 merge request!1New: Create a Api Client Script
...@@ -20,7 +20,10 @@ namespace Coscine.CodeGen ...@@ -20,7 +20,10 @@ namespace Coscine.CodeGen
{ {
var configuration = new ConsulConfiguration(); var configuration = new ConsulConfiguration();
var jarDownloadLink = await configuration.GetStringAsync("coscine/local/codegen/jarlink", "https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.27/swagger-codegen-cli-3.0.27.jar"); var jarDownloadLink = await configuration.GetStringAsync("coscine/local/codegen/jarlink",
//"https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.27/swagger-codegen-cli-3.0.27.jar"
"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 webClient = new WebClient();
...@@ -48,7 +51,7 @@ namespace Coscine.CodeGen ...@@ -48,7 +51,7 @@ namespace Coscine.CodeGen
var swaggerUrl = $"https://{hostName}.{domainName}/coscine/api/{key}/swagger/v1/swagger.json"; var swaggerUrl = $"https://{hostName}.{domainName}/coscine/api/{key}/swagger/v1/swagger.json";
var command = $"java -jar \"{jarFileName}\" generate -i \"{swaggerUrl}\" -l typescript-fetch -o \"{outputPath}/{key}\" -Dio.swagger.parser.util.RemoteUrl.trustAll=true -Dio.swagger.v3.parser.util.RemoteUrl.trustAll=true"; var command = $"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";
await ExecuteCommand(command); await ExecuteCommand(command);
} }
...@@ -58,6 +61,8 @@ namespace Coscine.CodeGen ...@@ -58,6 +61,8 @@ namespace Coscine.CodeGen
var combinationFileImports = new List<string>(); var combinationFileImports = new List<string>();
var combinationFileExports = new List<string>(); var combinationFileExports = new List<string>();
var first = true;
foreach (var directory in Directory.GetDirectories(outputPath)) foreach (var directory in Directory.GetDirectories(outputPath))
{ {
var apiName = directory[(directory.LastIndexOf(".") + 1)..]; var apiName = directory[(directory.LastIndexOf(".") + 1)..];
...@@ -67,19 +72,27 @@ namespace Coscine.CodeGen ...@@ -67,19 +72,27 @@ namespace Coscine.CodeGen
apiName = apiName.Replace("Notices", "Notice"); apiName = apiName.Replace("Notices", "Notice");
combinationFileImports.Add($"import {{ {apiName}ApiFactory }} from './{directoryName}/api';"); combinationFileImports.Add($"import {{ {apiName}ApiFactory }} from './{directoryName}/api';");
combinationFileExports.Add($"{apiName}Api: {apiName}ApiFactory({{ 'accessToken': accessToken }}, isomorphicFetch, 'https://' + getHostName() + '/coscine/api/{directoryName}')"); if (first)
{
first = false;
combinationFileImports.Add($"import {{ Configuration }} from './{directoryName}/configuration';");
}
combinationFileExports.Add($"{apiName}Api: {apiName}ApiFactory(new Configuration({{ 'accessToken': accessToken }}), 'https://' + getHostName() + '/coscine/api/{directoryName}', axios)");
} }
var combinationFileText = "import * as isomorphicFetch from 'isomorphic-fetch';\n\n"; var combinationFileText = "import { AxiosInstance } from 'axios';\n\n";
combinationFileText += string.Join('\n', combinationFileImports); combinationFileText += string.Join('\n', combinationFileImports);
combinationFileText += "\n\n"; combinationFileText += "\n\n";
combinationFileText += "declare var coscine: { authorization: { bearer: string }};\n"; combinationFileText += "let accessToken = '';";
combinationFileText += "const accessToken = coscine.authorization.bearer;\n";
combinationFileText += @"
if (typeof coscine !== 'undefined') {
accessToken = coscine.authorization.bearer;
}
combinationFileText += @"const getHostName = () => { const getHostName = () => {
let hostName = window.location.hostname; let hostName = typeof window !== 'undefined' ? window.location.hostname : 'coscine.rwth-aachen.de';
if (hostName.indexOf(':') !== -1) { if (hostName.indexOf(':') !== -1) {
if (hostName.indexOf('https://') !== -1) { if (hostName.indexOf('https://') !== -1) {
hostName = hostName.replace('https://', ''); hostName = hostName.replace('https://', '');
...@@ -93,12 +106,13 @@ namespace Coscine.CodeGen ...@@ -93,12 +106,13 @@ namespace Coscine.CodeGen
combinationFileText += "export default {\n "; combinationFileText += @"function implementations(axios?: AxiosInstance) {
return {
";
combinationFileText += string.Join(",\n ", combinationFileExports); combinationFileText += string.Join(",\n ", combinationFileExports);
combinationFileText += "\n};\n"; combinationFileText += "\n };\n};\n\nexport default implementations;\n";
await File.WriteAllTextAsync(combinationFile, combinationFileText); await File.WriteAllTextAsync(combinationFile, combinationFileText);
} }
private static Task ExecuteCommand(string command) private static Task ExecuteCommand(string command)
...@@ -119,15 +133,18 @@ namespace Coscine.CodeGen ...@@ -119,15 +133,18 @@ namespace Coscine.CodeGen
{ {
process.Start(); process.Start();
string output = process.StandardOutput.ReadToEnd(); var outputTask = process.StandardOutput.ReadToEndAsync();
Console.WriteLine(output);
var errorTask = process.StandardError.ReadToEndAsync();
Task.WaitAll(outputTask, errorTask);
Console.WriteLine(outputTask.Result);
Console.WriteLine(errorTask.Result);
string errors = process.StandardError.ReadToEnd();
Console.WriteLine(errors);
return process.WaitForExitAsync(); return process.WaitForExitAsync();
} }
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment