private static void GenerateCode(ExchangeSettings exchangeSettings) { try { Console.WriteLine($"Generating Documentation for {exchangeSettings.ExchangeName}"); var document = SwaggerYamlDocument.FromUrlAsync(exchangeSettings.SwaggerFileUrl).Result; var settings = new SwaggerToCSharpClientGeneratorSettings { ClassName = $"{exchangeSettings.ExchangeName}Client", CSharpGeneratorSettings = { Namespace = $"CryptoSharp.Client.Exchanges.{exchangeSettings.ExchangeName}", SchemaType = SchemaType.OpenApi3, ClassStyle = CSharpClassStyle.Poco, }, GenerateClientInterfaces = true, CodeGeneratorSettings = { SchemaType = SchemaType.OpenApi3 }, }; var generator = new SwaggerToCSharpClientGenerator(document, settings); var code = generator.GenerateFile(); // hack to replace double by decimal var regex = new Regex("\\sdouble\\s", RegexOptions.Multiline); code = regex.Replace(code, " decimal "); if (!Directory.Exists(_generatedDirectory)) { Directory.CreateDirectory(_generatedDirectory); } File.WriteAllText($"{ _generatedDirectory}/{exchangeSettings.ExchangeName}Client.cs", code); } catch (Exception ex) { Console.WriteLine(ex); } }
protected async Task <SwaggerDocument> ReadSwaggerDocumentAsync(string input) { if (!IsJson(input) && !IsYaml(input)) { if (input.StartsWith("http://") || input.StartsWith("https://")) { if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) || input.EndsWith(".yml", StringComparison.OrdinalIgnoreCase)) { return(await SwaggerYamlDocument.FromUrlAsync(input).ConfigureAwait(false)); } else { return(await SwaggerDocument.FromUrlAsync(input).ConfigureAwait(false)); } } else { if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) || input.EndsWith(".yml", StringComparison.OrdinalIgnoreCase)) { return(await SwaggerYamlDocument.FromFileAsync(input).ConfigureAwait(false)); } else { return(await SwaggerDocument.FromFileAsync(input).ConfigureAwait(false)); } } } else { if (IsYaml(input)) { return(await SwaggerYamlDocument.FromYamlAsync(input).ConfigureAwait(false)); } else { return(await SwaggerDocument.FromJsonAsync(input).ConfigureAwait(false)); } } }
/// <summary>Loads the Swagger spec.</summary> public async Task <SwaggerDocument> RunAsync() { if (!string.IsNullOrEmpty(Swagger)) { if (Swagger.StartsWith("{")) { return(await SwaggerDocument.FromJsonAsync(Swagger).ConfigureAwait(false)); } else { return(await SwaggerYamlDocument.FromYamlAsync(Swagger).ConfigureAwait(false)); } } else if (Url.StartsWith("http://") || Url.StartsWith("https://")) { if (Url.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase)) { return(await SwaggerYamlDocument.FromUrlAsync(Url).ConfigureAwait(false)); } else { return(await SwaggerDocument.FromUrlAsync(Url).ConfigureAwait(false)); } } else { if (Url.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase)) { return(await SwaggerYamlDocument.FromFileAsync(Url).ConfigureAwait(false)); } else { return(await SwaggerDocument.FromFileAsync(Url).ConfigureAwait(false)); } } }