Files
2020-04-13 15:16:46 +01:00

153 lines
5.7 KiB
C#

using AutoBet.ApiGenerator.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace AutoBet.ApiGenerator
{
internal class ApiClientGenetaor
{
private string _inputXmlFile { get; }
private string _outputFilesDirectory { get; }
public ApiClientGenetaor(string inputXmlFile, string outputFilesDirectory)
{
_inputXmlFile = inputXmlFile;
_outputFilesDirectory = outputFilesDirectory;
}
public void Process(string apiClientName, string namespaceValue)
{
var doc = XDocument.Load(_inputXmlFile);
var sb = new StringBuilder();
sb.AppendLine("using System;");
sb.AppendLine("using System.Collections.Generic;");
sb.AppendLine("using System.Net.Http;");
sb.AppendLine("using System.Net.Http.Headers;");
sb.AppendLine("using System.Text;");
sb.AppendLine("using System.Threading.Tasks;");
sb.AppendFormat("using {0}.{1}{2};", namespaceValue, apiClientName, "Models");
sb.AppendLine();
sb.AppendFormat("using {0}.{1}{2};", namespaceValue, apiClientName, "Models.Enums");
sb.AppendLine();
sb.AppendLine();
sb.Append("namespace ");
sb.Append(namespaceValue);
sb.AppendLine();
sb.AppendLine("{");
sb.AppendWithIndent(4, "public class ");
sb.Append(apiClientName);
sb.Append("ApiClient");
sb.AppendLine();
sb.AppendWithIndent(4, "{\r\n");
var nodes = doc.Root.Elements("operation");
foreach(var node in nodes)
{
AppendDescription(sb, node);
sb.AppendWithIndent(8, "public ");
var paramaeters = node.Element("parameters");
var response = paramaeters.Element("simpleResponse");
sb.Append("async Task<");
var returnType = CSharpClassBuilder.GetDataType(response.Attribute("type").Value);
sb.Append(returnType);
sb.Append("> ");
var camelcaseMethodName = node.Attribute("name").Value;
var methodName = GetMethodName(camelcaseMethodName);
sb.Append(methodName);
var parametersAppended = AppendParameters(sb, paramaeters.Element("request"));
sb.AppendWithIndent(8, "{\r\n");
AppendMethodBody(sb, returnType, camelcaseMethodName + "/", parametersAppended);
sb.AppendWithIndent(8, "}\r\n\r\n");
}
sb.Remove(sb.Length - 2, 2);
sb.AppendWithIndent(4, "}\r\n");
sb.Append("}");
File.WriteAllText(Path.Combine(_outputFilesDirectory, $"{apiClientName}ApiClient.cs"), sb.ToString());
}
private void AppendMethodBody(StringBuilder sb, string returnType, string endpoint, string[] paramaters)
{
var request = "";
if(paramaters.Length > 0)
{
sb.AppendWithIndent(12, "var request = new\r\n");
sb.AppendWithIndent(12, "{\r\n");
foreach (var parameter in paramaters)
{
sb.AppendWithIndent(16, parameter);
sb.Append(",\r\n");
}
sb.Remove(sb.Length - 3, 3);
sb.Append("\r\n");
sb.AppendWithIndent(12, "};\r\n");
request = ", request";
}
sb.AppendWithIndent(12, "var result = await ExecuteRequest<");
sb.Append(returnType);
sb.Append(">(_endpoint + \"");
sb.Append(endpoint);
sb.Append("\"");
sb.Append(request);
sb.Append(");\r\n");
sb.AppendWithIndent(12, "return result;\r\n");
}
private string[] AppendParameters(StringBuilder sb, XElement request)
{
var paramatersAppended = new List<string>();
sb.Append("(");
var parameters = request.Elements("parameter");
foreach (var parameter in parameters)
{
var name = parameter.Attribute("name").Value;
var type = CSharpClassBuilder.GetDataType(parameter.Attribute("type").Value);
sb.Append(type);
var mandatory = parameter.Attribute("mandatory");
var isRequired = mandatory != null && mandatory.Value != null && mandatory.Value == "true";
if (!isRequired && (type == "int" || type == "long" || type == "bool"))
{
sb.Append("?");
}
sb.Append(" ");
sb.Append(name);
if (!isRequired)
{
sb.Append(" = null");
}
paramatersAppended.Add(name);
sb.Append(", ");
}
if(parameters.Any())
{
sb.Remove(sb.Length - 2, 2);
}
sb.Append(")\r\n");
return paramatersAppended.ToArray();
}
private void AppendDescription(StringBuilder sb, XElement node)
{
var description = node.Element("description").Value;
if (!String.IsNullOrWhiteSpace(description))
{
CSharpClassBuilder.Summary(sb, description);
}
}
private static string GetMethodName(string methodName)
{
return Char.ToUpper(methodName[0]).ToString() + methodName.Substring(1, methodName.Length - 1);
}
}
}