public static string CreateMethodReturn(Api api, Operation operation, int indent = 3, bool async = true) { StringBuilder result = new StringBuilder(); string methodReturnType = GetMethodReturnTaskParameter(operation.ResponseType); result.Append(GetIndent(indent)); if (methodReturnType != String.Empty) { result.Append("return "); } result.Append(async ? "await RawCall" : "RawCall"); result.Append(methodReturnType); result.AppendFormat("(HttpMethod.{0},", FirstUpperCase(operation.HttpMethod)); result.Append(GetApiPath(api, operation)); if (operation.Parameters.Any(parameter => parameter.ParamType == ParamType.Body)) { result.Append(",requestBody"); } result.Append(");"); return result.ToString(); }
public static string GetMethodName(Api api, Operation operation) { string prefix = GetMethodPrefix(operation); string[] tokens = api.Path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); StringBuilder result = new StringBuilder(prefix); foreach (var token in tokens) { if (token.StartsWith("{")) continue; result.Append(FirstUpperCase(token)); } if (operation.ResponseType.EndsWith("[]")) { if (result[result.Length - 1] == 's' && result[result.Length - 2] != 'm' && result[result.Length - 3] != 'S') // remove existing 's' { result.Remove(result.Length - 1, 1); } if (operation.ResponseType == "long[]") { result.Append("Id"); } else if (operation.ResponseType == "string[]") { result.Append("Name"); } result.Append("s"); } return result.ToString(); }
public static string GetMethodNameAndParameters(Api api, Operation operation) { StringBuilder result = new StringBuilder(""); result.Append(GetMethodName(api, operation)); List<string> required = new List<string>(); List<string> nulllable = new List<string>(); foreach (var p in operation.Parameters) { StringBuilder param = new StringBuilder(); param.Append(GetType(p.DataType)); if (IsNullableType(p)) { param.Append('?'); } param.Append(' '); param.Append(GetParameterName(p)); if (!p.Required) { param.Append(" = null"); nulllable.Insert(0, param.ToString()); // To have have from date before to date } else { required.Add(param.ToString()); } } result.Append('('); for (int i = 0; i < required.Count; i++) { if (i > 0) result.Append(','); result.Append(required[i]); } for (int i = 0; i < nulllable.Count; i++) { if (required.Count > 0 || i > 0) result.Append(','); result.Append(nulllable[i]); } result.Append(')'); return result.ToString(); }
public static string GetApiPath(Api api, Operation operation) { string path = api.Path; int count = 0; List<string> args = new List<string>(); bool hasQueryString = false; foreach (Parameter p in operation.Parameters) { switch (p.ParamType) { case ParamType.Path: path = path.Replace("{" + p.Name + "}", "{" + count + "}"); args.Add(GetParameterName(p)); count++; break; case ParamType.Query: hasQueryString = true; break; } } StringBuilder result = new StringBuilder("String.Format("); result.Append('"'); result.Append(path); if (hasQueryString) result.AppendFormat("{{{0}}}", count); result.Append('"'); foreach (var arg in args) { result.AppendFormat(",System.Uri.EscapeDataString({0}.ToString()).Replace(\"%2B\", \"+\")", arg); } if (hasQueryString) result.Append(",queryString"); result.Append(")"); return result.ToString(); }