private void FixupOneFile(OkapiInfo api, RenderFixupTask fixup) { var fn = Path.Combine(rootFolder, fixup.file); Console.Write($"Executing fixup for {fn}... "); if (!File.Exists(fn)) { Console.WriteLine(" File not found!"); } else { // Determine what the new string is var newstring = QuickStringMerge(fixup.replacement, api); // What encoding did they want - basically everyone SHOULD want UTF8, but ascii is possible I guess Encoding e = Encoding.UTF8; if (fixup.encoding == "ASCII") { e = Encoding.ASCII; } // Execute the fixup ReplaceStringInFile(fn, fixup.regex, newstring, e); Console.WriteLine(" Done!"); } }
private static OkapiInfo DownloadSwaggerJson(SwaggerRenderTask task) { string swaggerJson = null; OkapiInfo api = null; // Download the swagger JSON file from the server using (var client = new HttpClient()) { try { Console.WriteLine($"***** Downloading swagger JSON from {task.swaggerUri}"); var response = client.GetAsync(task.swaggerUri).Result; swaggerJson = response.Content.ReadAsStringAsync().Result; } catch (Exception ex) { Console.WriteLine($"Exception downloading swagger JSON file: {ex.ToString()}"); return(null); } } // Parse the swagger JSON file try { Console.WriteLine($"***** Processing swagger JSON"); api = ProcessSwagger(swaggerJson); } catch (Exception ex) { Console.WriteLine($"Exception processing swagger JSON file: {ex.ToString()}"); } return(api); }
private void RenderSingleFile(OkapiInfo api, RenderTemplateTask template) { var outputPath = Path.Combine(rootFolder, template.output); Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); var output = template.razor.ExecuteTemplate(api, null, null, null); File.WriteAllText(outputPath, output); }
/// <summary> /// Execute this template against the specified model /// </summary> /// <param name="model"></param> /// <param name="m"></param> /// <param name="e"></param> /// <returns></returns> public virtual string ExecuteTemplate(OkapiInfo api, OkapiMethodInfo method, OkapiModelInfo model, OkapiEnumInfo enumDataType) { Buffer.Clear(); SwaggerModel = api; MethodModel = method; ClassModel = model; EnumModel = enumDataType; Execute(); return(Buffer.ToString()); }
private void RenderMethods(OkapiInfo api, RenderTemplateTask template) { foreach (var method in api.Methods) { var outputPath = Path.Combine(rootFolder, QuickStringMerge(template.output, method)); Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); var output = template.razor.ExecuteTemplate(api, method, null, null); File.WriteAllText(outputPath, output); } }
private void RenderEnums(OkapiInfo api, RenderTemplateTask template) { foreach (var enumDataType in api.Enums) { var outputPath = Path.Combine(rootFolder, QuickStringMerge(template.output, enumDataType)); Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); var output = template.razor.ExecuteTemplate(api, null, null, enumDataType); File.WriteAllText(outputPath, output); } }
/// <summary> /// Render this particular type of client library /// </summary> /// <param name="api"></param> /// <param name="rootPath"></param> public virtual void Render(OkapiInfo api) { if (templates != null) { // Iterate through each template foreach (var template in templates) { Console.WriteLine($" Rendering {name}.{template.file}..."); // What type of template are we looking at? switch (template.type) { // A single template file for the entire API case TemplateType.singleFile: RenderSingleFile(api, template); break; // A separate file for each method category in the API case TemplateType.methodCategories: RenderMethodCategories(api, template); break; // One file per category case TemplateType.methods: RenderMethods(api, template); break; // One file per model case TemplateType.models: RenderModels(api, template); break; // One file per model case TemplateType.uniqueModels: RenderUniqueModels(api, template); break; // One file per enum case TemplateType.enums: RenderEnums(api, template); break; } } } // Are there any fixups? if (fixups != null) { foreach (var fixup in fixups) { FixupOneFile(api, fixup); } } }
public static void Render(SwaggerRenderTask task, OkapiInfo api) { // Render each target foreach (var target in task.targets) { Console.WriteLine($"***** Rendering {target.name}"); target.Render(api); } // Done Console.WriteLine("***** Done"); }
private void RenderUniqueModels(OkapiInfo api, RenderTemplateTask template) { var oldModels = api.Models; api.Models = (from m in api.Models where !m.SchemaName.StartsWith("FetchResult") select m).ToList(); foreach (var model in api.Models) { var outputPath = Path.Combine(rootFolder, QuickStringMerge(template.output, model)); Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); var output = template.razor.ExecuteTemplate(api, null, model, null); File.WriteAllText(outputPath, output); } api.Models = oldModels; }
private void RenderMethodCategories(OkapiInfo api, RenderTemplateTask template) { var categories = (from m in api.Methods select m.Category).Distinct(); foreach (var c in categories) { var oldMethods = api.Methods; api.Methods = (from m in api.Methods where m.Category == c select m).ToList(); var outputPath = Path.Combine(rootFolder, QuickStringMerge(template.output, c)); Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); template.razor.Category = c; var output = template.razor.ExecuteTemplate(api, null, null, null); template.razor.Category = null; File.WriteAllText(outputPath, output); api.Methods = oldMethods; } }
public string PhpTypeComment(OkapiInfo s, OkapiParameterInfo p) { string comment = ""; if (p.Comment != null) { comment = NoNewlines(FixWhitespace(p.Comment)); } // Is this an enum? If so, convert it to a string - we'll add a comment later if (IsEnumType(p.TypeName)) { return(comment + " (See " + p.TypeName.Replace("?", "") + "::* for a list of allowable values)"); } else if (p.TypeName == "Byte[]") { return(comment + " (This value is encoded as a Base64 string)"); } return(comment); }
private static void GenerateTemplateFile(string file) { // First parse the file SwaggerRenderTask task = ParseRenderTask(file); if (task == null) { return; } // Download the swagger file OkapiInfo api = DownloadSwaggerJson(task); if (api == null) { return; } // Render output Console.WriteLine($"***** Beginning render stage"); Render(task, api); }
private static OkapiInfo Cleanup(SwaggerModel obj) { OkapiInfo result = new OkapiInfo(); result.ApiVersion = obj.ApiVersion; // Set up alternative version numbers: This one does not permit dashes result.ApiVersionPeriodsOnly = result.ApiVersion.Replace("-", "."); // Set up alternative version numbers: This one permits only three segments var sb = new StringBuilder(); int numPeriods = 0; foreach (char c in obj.ApiVersion) { if (c == '.') { numPeriods++; } if (numPeriods > 3 || c == '-') { break; } sb.Append(c); } result.ApiVersionThreeSegmentsOnly = sb.ToString(); // Loop through all paths and spit them out to the console foreach (var path in (from p in obj.paths orderby p.Key select p)) { foreach (var verb in path.Value) { // Set up our API var api = new OkapiMethodInfo(); api.URI = path.Key; api.HttpVerb = verb.Key; api.Summary = verb.Value.summary; api.Description = verb.Value.description; api.Params = new List <OkapiParameterInfo>(); api.Category = verb.Value.tags.FirstOrDefault(); api.Name = verb.Value.operationId; // Now figure out all the URL parameters foreach (var parameter in verb.Value.parameters) { // Construct parameter var pi = ResolveType(parameter); // Query String Parameters if (parameter.paramIn == "query") { pi.ParameterLocation = ParameterLocationType.QueryString; // URL Path parameters } else if (parameter.paramIn == "path") { pi.ParameterLocation = ParameterLocationType.UriPath; // Body parameters } else if (parameter.paramIn == "body") { pi.ParamName = "model"; pi.ParameterLocation = ParameterLocationType.RequestBody; api.BodyParam = pi; } else if (parameter.paramIn == "header") { pi.ParameterLocation = ParameterLocationType.Header; } else if (parameter.paramIn == "formData") { pi.ParameterLocation = ParameterLocationType.FormData; } else { throw new Exception("Unrecognized parameter location: " + parameter.paramIn); } api.Params.Add(pi); // Is this property an enum? if (parameter.EnumDataType != null) { ExtractEnum(result.Enums, parameter); } } // Now figure out the response type SwaggerResult ok = null; if (verb.Value.responses.TryGetValue("200", out ok)) { api.ResponseType = ok.schema == null ? null : ok.schema.type; api.ResponseTypeName = ResolveTypeName(ok.schema); } else if (verb.Value.responses.TryGetValue("201", out ok)) { api.ResponseType = ok.schema == null ? null : ok.schema.type; api.ResponseTypeName = ResolveTypeName(ok.schema); } // Ensure that body parameters are always last for consistency if (api.BodyParam != null) { api.Params.Remove(api.BodyParam); api.Params.Add(api.BodyParam); } // Done with this API result.Methods.Add(api); } } // Loop through all the schemas foreach (var def in obj.definitions) { var m = new OkapiModelInfo() { SchemaName = def.Key, Comment = def.Value.description, Example = def.Value.example, Description = def.Value.description, Required = def.Value.required, Type = def.Value.type, Properties = new List <OkapiParameterInfo>() }; foreach (var prop in def.Value.properties) { if (!prop.Value.required && def.Value.required != null) { prop.Value.required = def.Value.required.Contains(prop.Key); } // Construct property var pi = ResolveType(prop.Value); pi.ParamName = prop.Key; m.Properties.Add(pi); // Is this property an enum? if (prop.Value.EnumDataType != null) { ExtractEnum(result.Enums, prop.Value); } } result.Models.Add(m); } // Here's your processed API return(result); }