private void WriteReturnTypes(GeneratorContext context, ReturnType returnType, ApiGroup apiGroup) { int indent = 0; this.text.WriteLine(indent, "// WriteReturnTypes(" + apiGroup.Name + ", " + returnType.Name + ")"); this.text.WriteLine(indent, "namespace " + this.RootNamespace + "." + apiGroup.Name); this.text.WriteLine(indent++, "{"); this.WriteNamespace(indent, "System"); this.WriteNamespace(indent, "System.Collections.Generic"); this.WriteNamespace(indent, "System.Xml.Serialization"); this.WriteNamespace(indent, "Newtonsoft.Json"); this.text.WriteLine(); this.text.WriteLine(indent, "/// <summary>"); this.text.WriteLine(indent, "/// Name: '" + returnType.Name + "'"); this.text.WriteLine(indent, "/// </summary>"); if (returnType.Remark != null) { this.text.WriteLine(indent, "/// <remarks>"); this.text.WriteLine(indent, "/// " + returnType.Remark + ""); this.text.WriteLine(indent, "/// </remarks>"); } var className = this.GetPropertyName(returnType.ClassName, returnType.Name); this.text.WriteLine(indent, "[Serializable, XmlRoot(\"" + returnType.Name + "\")]"); this.text.WriteLine(indent, "public class " + className); this.text.WriteLine(indent++, "{"); foreach (var itemGroup in returnType.Fields.GroupBy(f => f.FieldName.PropertyName).ToArray()) { var item = itemGroup.First(); var isCollection = item.IsCollection; var type = item.Type ?? item.FieldName.ClassName ?? "string"; ReturnType fieldReturnType = null; if (!csharpTypes.Contains(type)) { fieldReturnType = context.Definition.FindReturnType(type); if (fieldReturnType != null && !type.Contains('.')) { type = fieldReturnType.ClassName ?? Namify(fieldReturnType.Name); } } if (isCollection) { type = "List<" + type + ">"; } this.text.WriteLine(indent, "/// <summary>"); foreach (var subItem in itemGroup) { this.text.WriteLine(indent, "/// Field: '" + subItem.Name + "' (" + (subItem.IsDefault ? "default" : "on-demand") + ")"); } foreach (var subItem in itemGroup.Where(f => f.Selectors != null).SelectMany(f => f.Selectors)) { this.text.WriteLine(indent, "/// Field: '" + subItem.Name + "'"); } var xmlAttribute = item.IsAttribute ? "XmlAttribute" : "XmlElement"; var xmlAttributeNameProp = item.IsAttribute ? "AttributeName" : "ElementName"; var jsonName = (item.IsAttribute ? "_" : "") + Namify(item.FieldName.ApiName, NameTransformation.PascalCase); if (item.IsCollection) jsonName = "values"; this.text.WriteLine(indent, "/// </summary>"); this.text.WriteLine(indent, (item.Ignore ? "////" : "") + "[" + xmlAttribute + "(" + xmlAttributeNameProp + " = \"" + item.FieldName.ApiName + "\")]"); this.text.WriteLine(indent, (item.Ignore ? "////" : "") + "[JsonProperty(PropertyName = \"" + jsonName + "\")]"); this.text.WriteLine(indent, "public " + type + " " + item.FieldName.PropertyName + " { get; set; }"); this.text.WriteLine(); } if (returnType.Headers != null) { foreach (var item in returnType.Headers) { var isCollection = item.IsCollection; var type = item.Type ?? item.FieldName.ClassName ?? "string"; ReturnType fieldReturnType = null; if (!csharpTypes.Contains(type)) { fieldReturnType = context.Definition.FindReturnType(type); if (fieldReturnType != null) { type = fieldReturnType.ClassName ?? Namify(fieldReturnType.Name); } } if (isCollection) { type = "List<" + type + ">"; } this.text.WriteLine(indent, "/// <summary>"); this.text.WriteLine(indent, "/// HTTP header '" + item.Name + "'"); this.text.WriteLine(indent, "/// </summary>"); this.text.WriteLine(indent, "public " + type + " " + item.FieldName.PropertyName + " { get; set; }"); this.text.WriteLine(); } } this.text.WriteLine(--indent, "}"); this.text.WriteLine(--indent, "}"); this.text.WriteLine(); }
private void WriteReturnTypeFields(GeneratorContext context, ApiGroup apiGroup, ReturnType[] returnTypes) { int indent = 0; this.text.WriteLine(indent, "// WriteReturnTypeFields(" + apiGroup.Name + ")"); this.text.WriteLine(indent, "namespace " + this.RootNamespace + "." + apiGroup.Name); this.text.WriteLine(indent++, "{"); this.WriteNamespace(indent, "System"); this.WriteNamespace(indent, "System.Collections.Generic"); this.WriteNamespace(indent, "System.Xml.Serialization"); this.text.WriteLine(); this.text.WriteLine(indent, "/// <summary>"); this.text.WriteLine(indent, "/// Field selectors for the '" + string.Join("', '", returnTypes.Select(r => r.Name).ToArray()) + "' return types."); this.text.WriteLine(indent, "/// </summary>"); this.text.WriteLine(indent++, "public static class " + apiGroup.Name + "Fields {"); foreach (var returnType in returnTypes) { ////var returnTypeName = this.GetPropertyName(returnType.ClassName, returnType.Name); ////this.text.WriteLine(indent++, "public static FieldSelector<" + returnTypeName + "> Fields(this FieldSelector<" + returnTypeName + "> me) {"); ////this.text.WriteLine(indent, "return me.Add(\"" + field.Name + "\");"); ////this.text.WriteLine(--indent, "}"); ////this.text.WriteLine(indent, ""); var returnTypeName = this.GetPropertyName(returnType.ClassName, returnType.Name); ////this.text.WriteLine(indent, "// return type: " + (returnType.ClassName ?? returnType.Name)); var allFields = new List<string>(); foreach (var fieldGroup in returnType.Fields.GroupBy(f => f.Name).ToArray()) { var field = fieldGroup.First(); var selectors = fieldGroup.Where(f => f.Selectors != null).SelectMany(f => f.Selectors).ToArray(); if (selectors.Length == 0 && returnType.AutoGenerateFieldSelectors) { var name = field.Name; var fieldName = this.GetPropertyName(null, field.Name); allFields.Add(name); WriteReturnTypeField(indent, returnTypeName, fieldName, name); } else { foreach (var selector in selectors) { var name = selector.Name; var fieldName = this.GetPropertyName(selector.PropertyName, name); allFields.Add(name); WriteReturnTypeField(indent, returnTypeName, fieldName, name); } } } if (returnType.Selectors != null) { foreach (var customSelector in returnType.Selectors) { if (!allFields.Contains(customSelector.Name)) { var fieldName = this.GetPropertyName(customSelector.PropertyName, customSelector.Name); WriteReturnTypeField(indent, returnTypeName, fieldName, customSelector.Name); allFields.Add(customSelector.Name); } } } if (returnType.ImportFieldSelectors != null) { foreach (var import in returnType.ImportFieldSelectors) { if (import.ReturnType != null) { var type = context.Definition.FindReturnType(import.ReturnType); if (type != null) { foreach (var field in type.Fields) { var fullName = import.Name + ":(" + field.Name + ")"; var fullPropertyName = this.GetPropertyName(null, import.Name) + this.GetPropertyName(field.PropertyName, field.Name); WriteReturnTypeField(indent, returnTypeName, fullPropertyName, fullName); allFields.Add(fullName); } } } } } if (allFields.Count > 0) { this.text.WriteLine(indent, "/// <summary>"); this.text.WriteLine(indent, "/// Includes all the fields."); this.text.WriteLine(indent, "/// </summary>"); this.text.WriteLine(indent, "/// <param name=\"me\">The field selector.</param>"); this.text.WriteLine(indent, "/// <returns>The field selector.</returns>"); this.text.Write(indent++, "public static FieldSelector<" + returnTypeName + "> WithAllFields(this FieldSelector<" + returnTypeName + "> me) { "); this.text.Write(@"return me.AddRange("""); var sep = ""; for (int i = 0; i < allFields.Count; i++) { var name = allFields[i]; var slash = name.IndexOf('/'); if (slash > 0) name = name.Substring(0, slash); this.text.Write(sep); this.text.Write(name); sep = "\", \""; } this.text.Write("\"); "); this.text.WriteLine("}"); indent--; this.text.WriteLine(indent, ""); } else { ////this.text.WriteLine(indent, "// allFields.Count == 0"); } ////this.text.WriteLine(indent, ""); } this.text.WriteLine(--indent, "}"); this.text.WriteLine(--indent, "}"); this.text.WriteLine(); }
private void WriteReturnTypes(GeneratorContext context, ReturnType returnType, ApiGroup apiGroup) { int indent = 0; this.text.WriteLine(indent, "// WriteReturnTypes(" + apiGroup.Name + ", " + returnType.Name + ")"); this.text.WriteLine(indent, "namespace " + this.RootNamespace + "." + apiGroup.Name); this.text.WriteLine(indent++, "{"); this.WriteNamespace(indent, "System"); this.WriteNamespace(indent, "System.Collections.Generic"); this.WriteNamespace(indent, "System.Xml.Serialization"); this.WriteNamespace(indent, "Newtonsoft.Json"); this.WriteNamespace(indent, "Newtonsoft.Json.Linq"); this.text.WriteLine(); this.text.WriteLine(indent, "/// <summary>"); this.text.WriteLine(indent, "/// Name: '" + returnType.Name + "'"); this.text.WriteLine(indent, "/// </summary>"); if (returnType.Remark != null) { this.text.WriteLine(indent, "/// <remarks>"); this.text.WriteLine(indent, "/// " + returnType.Remark + ""); this.text.WriteLine(indent, "/// </remarks>"); } var className = this.GetPropertyName(returnType.ClassName, returnType.Name); this.text.WriteLine(indent, "[Serializable, XmlRoot(\"" + returnType.Name + "\")]"); this.text.WriteLine(indent, "public class " + className); this.text.WriteLine(indent++, "{"); foreach (var itemGroup in returnType.Fields.GroupBy(f => f.FieldName.PropertyName).ToArray()) { var item = itemGroup.First(); var isCollection = item.IsCollection; var type = item.Type ?? item.FieldName.ClassName ?? "string"; ReturnType fieldReturnType = null; if (!csharpTypes.Contains(type)) { fieldReturnType = context.Definition.FindReturnType(type); if (fieldReturnType != null && !type.Contains('.')) { type = fieldReturnType.ClassName ?? Namify(fieldReturnType.Name); } } if (isCollection) { type = "List<" + type + ">"; } this.text.WriteLine(indent, "/// <summary>"); foreach (var subItem in itemGroup) { this.text.WriteLine(indent, "/// Field: '" + subItem.Name + "' (" + (subItem.IsDefault ? "default" : "on-demand") + ")"); } foreach (var subItem in itemGroup.Where(f => f.Selectors != null).SelectMany(f => f.Selectors)) { this.text.WriteLine(indent, "/// Field: '" + subItem.Name + "'"); } var xmlAttribute = item.IsAttribute ? "XmlAttribute" : "XmlElement"; var xmlAttributeNameProp = item.IsAttribute ? "AttributeName" : "ElementName"; var jsonName = (item.IsAttribute ? "_" : "") + item.FieldName.ApiName; // Namify(item.FieldName.ApiName, NameTransformation.PascalCase); this.text.WriteLine(indent, "/// </summary>"); this.text.WriteLine(indent, (item.Ignore ? "////" : "") + "[" + xmlAttribute + "(" + xmlAttributeNameProp + " = \"" + item.FieldName.ApiName + "\")]"); this.text.WriteLine(indent, (item.Ignore ? "////" : "") + "[JsonProperty(PropertyName = \"" + jsonName + "\")]"); this.text.WriteLine(indent, "public " + type + " " + item.FieldName.PropertyName + " { get; set; }"); this.text.WriteLine(); } if (returnType.Headers != null) { foreach (var item in returnType.Headers) { var isCollection = item.IsCollection; var type = item.Type ?? item.FieldName.ClassName ?? "string"; ReturnType fieldReturnType = null; if (!csharpTypes.Contains(type)) { fieldReturnType = context.Definition.FindReturnType(type); if (fieldReturnType != null) { type = fieldReturnType.ClassName ?? Namify(fieldReturnType.Name); } } if (isCollection) { type = "List<" + type + ">"; } this.text.WriteLine(indent, "/// <summary>"); this.text.WriteLine(indent, "/// HTTP header '" + item.Name + "'"); this.text.WriteLine(indent, "/// </summary>"); this.text.WriteLine(indent, "public " + type + " " + item.FieldName.PropertyName + " { get; set; }"); this.text.WriteLine(); } } this.text.WriteLine(--indent, "}"); this.text.WriteLine(--indent, "}"); this.text.WriteLine(); }
private void WriteMethod(GeneratorContext context, ApiGroup apiGroup, int indent) { foreach (var method in apiGroup.Methods) { var returnType = "void"; ReturnType returnTypeType = null; if (method.ReturnType != null) { returnTypeType = context.Definition.FindReturnType(method.ReturnType, apiGroup.Name); if (returnTypeType != null) { returnType = this.GetPropertyName(returnTypeType.ClassName, returnTypeType.Name); if (method.IsValueType) { returnType = returnType.ToLower(); } else { returnType = returnTypeType.ApiGroup + "." + returnType; } } } string postReturnType = null; ReturnType postReturnTypeType = null; if (method.PostReturnType != null) { postReturnTypeType = context.Definition.FindReturnType(method.PostReturnType, apiGroup.Name); if (postReturnTypeType != null) { postReturnType = this.GetPropertyName(postReturnTypeType.ClassName, postReturnTypeType.Name); postReturnType = postReturnTypeType.ApiGroup + "." + postReturnType; } } var urlParameters = new List <Parameter>(); if (method.RequiresUserAuthentication) { urlParameters.Add(new Parameter("user", "UserAuthorization")); } var urlParams = GetUrlPathParameters(method.Path, NameTransformation.PascalCase); foreach (var urlParam in urlParams) { urlParameters.Add(urlParam.Value); } var queryParameters = new List <Parameter>(); var queryParams = string.IsNullOrWhiteSpace(method.Query) ? new Dictionary <string, Parameter>() : GetUrlPathParameters(method.Query, NameTransformation.PascalCase); foreach (var queryParam in queryParams) { urlParameters.Add(queryParam.Value); } if (method.UsesAcceptLanguage) { urlParameters.Add(new Parameter("acceptLanguages", "string[]", originalName: "acceptLanguages = null")); } // doc this.text.WriteLine(indent, "/// <summary>"); this.text.WriteLine(indent, "/// " + method.Title); this.text.WriteLine(indent, "/// </summary>"); if (method.Remark != null) { this.text.WriteLine(indent, "/// <remarks>"); this.text.WriteLine(indent, "/// " + method.Remark + ""); this.text.WriteLine(indent, "/// </remarks>"); } // name and arguments this.text.WriteLine(indent++, "public " + returnType + " " + this.GetPropertyName(method.MethodName, method.Path) + "("); var sep = " "; foreach (var parameter in urlParameters) { this.text.WriteLine(indent, sep + (parameter.Type ?? "string") + " " + parameter.Name + " " + (parameter.Value != null ? ("= " + parameter.Value) : string.Empty)); sep = ", "; } if (postReturnTypeType != null) { this.text.WriteLine(indent, ", " + postReturnType + " postData"); } this.text.WriteLine(--indent, ")"); this.text.WriteLine(indent++, "{"); // body / url if (urlParams.Count > 0 || queryParams.Count > 0) { this.text.WriteLine(indent, "string skipUrlParamsEscape = \"" + method.SkipUrlParamsEscape + "\";"); } // body /format url if (urlParams.Count > 0) { this.text.WriteLine(indent, "string urlFormat = \"" + method.Path + "\";"); this.text.WriteLine(indent, "var url = FormatUrl(urlFormat, skipUrlParamsEscape, " + string.Join(", ", urlParams.Values.Select(p => "\"" + p.OriginalName + "\", " + p.Name).ToArray()) + ");"); } else { this.text.WriteLine(indent, "var url = \"" + method.Path + "\";"); } // body / format query if (queryParams.Count > 0) { this.text.WriteLine(indent, "string queryFormat = \"" + method.Query + "\";"); this.text.WriteLine(indent, "var query = FormatQuery(queryFormat, skipUrlParamsEscape, " + string.Join(", ", queryParams.Values.Select(p => "\"" + p.OriginalName + "\", " + p.Name).ToArray()) + ");"); } else { this.text.WriteLine(indent, "var query = \"" + method.Query + "\";"); } // body / create context this.text.WriteLine(); text.WriteLine(indent, "var context = new RequestContext();"); if (method.RequiresUserAuthentication) { text.WriteLine(indent, "context.UserAuthorization = user;"); } if (method.UsesAcceptLanguage) { text.WriteLine(indent, "context.AcceptLanguages = acceptLanguages;"); } text.WriteLine(indent, "context.Method = \"POST\";"); text.WriteLine(indent, "context.UrlPath = this.LinkedInApi.Configuration.BaseApiUrl + url;"); // tunneling text.WriteLine(indent, "// Requires for GET post tunneling"); text.WriteLine(indent, $"var httpMethodOverride = \"{method.HttpMethodOverride}\";"); text.WriteLine(indent, $"this.CreateTunnelingPostStream(context, httpMethodOverride, query, {(method.PostReturnType == null ? "null": "postData")}, {(method.IsStreamOctetResultHandeling ? "true" : "false")});"); // body / execute this.text.WriteLine(); if (method.UseRestliProtocol) { text.WriteLine(indent++, "if (!this.ExecuteQuery(context, true))"); } else { text.WriteLine(indent++, "if (!this.ExecuteQuery(context))"); } text.WriteLine(indent--, "this.HandleJsonErrorResponse(context);"); text.WriteLine(indent, ""); if (method.IsStreamOctetResultHandeling) { text.WriteLine(indent, @" var result = string.Empty; var headerETag = context.ResponseHeaders.GetValues(""eTag""); if (headerETag != null && headerETag.Length > 0) { result = headerETag.First(); result = result.TrimEnd('""').TrimStart('""'); }" ); } else if (method.IsUGCPostHandeling) { text.WriteLine(indent, @" var result = string.Empty; var headerETag = context.ResponseHeaders.GetValues(""X-RestLi-Id""); if (headerETag != null && headerETag.Length > 0) { result = headerETag.First(); result = result.TrimEnd('""').TrimStart('""'); }" ); } else if (method.IsIntResultHandeling) { text.WriteLine(indent, "var result = (int)context.HttpStatusCode;"); } else if (method.IsStringResultHandeling) { text.WriteLine(indent, "var result = this.HandleRawResponse(context, System.Text.Encoding.UTF8);"); } else { text.WriteLine(indent, "var result = this.HandleJsonResponse<" + returnType + ">(context);"); } if (returnTypeType != null && returnTypeType.Headers != null) { foreach (var header in returnTypeType.Headers) { text.WriteLine(indent, "result." + header.PropertyName + " = this.ReadHeader<" + (header.Type ?? "string") + ">(context, \"" + header.Name + "\");"); } } text.WriteLine(indent, "return result;"); // body / handle // body / return ////this.text.WriteLine(indent, "throw new NotImplementedException(url);"); this.text.WriteLine(--indent, "}"); this.text.WriteLine(); // doc this.text.WriteLine(indent, "/// <summary>"); this.text.WriteLine(indent, "/// " + method.Title); this.text.WriteLine(indent, "/// </summary>"); if (method.Remark != null) { this.text.WriteLine(indent, "/// <remarks>"); this.text.WriteLine(indent, "/// " + method.Remark + ""); this.text.WriteLine(indent, "/// </remarks>"); } // name and arguments this.text.WriteLine(indent++, "public async Task<" + returnType + "> " + this.GetPropertyName(method.MethodName, method.Path) + "Async("); sep = " "; foreach (var parameter in urlParameters) { this.text.WriteLine(indent, sep + (parameter.Type ?? "string") + " " + parameter.Name + " " + (parameter.Value != null ? ("= " + parameter.Value) : string.Empty)); sep = ", "; } if (postReturnTypeType != null) { this.text.WriteLine(indent, ", " + postReturnType + " postData"); } this.text.WriteLine(--indent, ")"); this.text.WriteLine(indent++, "{"); // body / url if (urlParams.Count > 0 || queryParams.Count > 0) { this.text.WriteLine(indent, "string skipUrlParamsEscape = \"" + method.SkipUrlParamsEscape + "\";"); } // body /format url if (urlParams.Count > 0) { this.text.WriteLine(indent, "string urlFormat = \"" + method.Path + "\";"); this.text.WriteLine(indent, "var url = FormatUrl(urlFormat, skipUrlParamsEscape, " + string.Join(", ", urlParams.Values.Select(p => "\"" + p.OriginalName + "\", " + p.Name).ToArray()) + ");"); } else { this.text.WriteLine(indent, "var url = \"" + method.Path + "\";"); } // body / format query if (queryParams.Count > 0) { this.text.WriteLine(indent, "string queryFormat = \"" + method.Query + "\";"); this.text.WriteLine(indent, "var query = FormatQuery(queryFormat, skipUrlParamsEscape, " + string.Join(", ", queryParams.Values.Select(p => "\"" + p.OriginalName + "\", " + p.Name).ToArray()) + ");"); } else { this.text.WriteLine(indent, "var query = \"" + method.Query + "\";"); } // body / create context this.text.WriteLine(); text.WriteLine(indent, "var context = new RequestContext();"); if (method.RequiresUserAuthentication) { text.WriteLine(indent, "context.UserAuthorization = user;"); } if (method.UsesAcceptLanguage) { text.WriteLine(indent, "context.AcceptLanguages = acceptLanguages;"); } text.WriteLine(indent, "context.Method = \"POST\";"); text.WriteLine(indent, "context.UrlPath = this.LinkedInApi.Configuration.BaseApiUrl + url;"); // tunneling text.WriteLine(indent, "// Requires for GET post tunneling"); text.WriteLine(indent, $"var httpMethodOverride = \"{method.HttpMethodOverride}\";"); text.WriteLine(indent, $"this.CreateTunnelingPostStream(context, httpMethodOverride, query, {(method.PostReturnType == null ? "null" : "postData")}, {(method.IsStreamOctetResultHandeling ? "true" : "false")});"); // body / execute this.text.WriteLine(); if (method.UseRestliProtocol) { text.WriteLine(indent, "var exec = await this.ExecuteQueryAsync(context, true);"); } else { text.WriteLine(indent, "var exec = await this.ExecuteQueryAsync(context);"); } text.WriteLine(indent++, "if (!exec)"); text.WriteLine(indent--, "this.HandleJsonErrorResponse(context);"); text.WriteLine(indent, ""); if (method.IsStreamOctetResultHandeling) { text.WriteLine(indent, @" var result = string.Empty; var headerETag = context.ResponseHeaders.GetValues(""eTag""); if (headerETag != null && headerETag.Length > 0) { result = headerETag.First(); result = result.TrimEnd('""').TrimStart('""'); }" ); } else if (method.IsUGCPostHandeling) { text.WriteLine(indent, @" var result = string.Empty; var headerETag = context.ResponseHeaders.GetValues(""X-RestLi-Id""); if (headerETag != null && headerETag.Length > 0) { result = headerETag.First(); result = result.TrimEnd('""').TrimStart('""'); }" ); } else if (method.IsIntResultHandeling) { text.WriteLine(indent, "var result = (int)context.HttpStatusCode;"); } else if (method.IsStringResultHandeling) { text.WriteLine(indent, "var result = this.HandleRawResponse(context, System.Text.Encoding.UTF8);"); } else { text.WriteLine(indent, "var result = this.HandleJsonResponse<" + returnType + ">(context);"); } if (returnTypeType != null && returnTypeType.Headers != null) { foreach (var header in returnTypeType.Headers) { text.WriteLine(indent, "result." + header.PropertyName + " = this.ReadHeader<" + (header.Type ?? "string") + ">(context, \"" + header.Name + "\");"); } } text.WriteLine(indent, "return result;"); // body / handle // body / return ////this.text.WriteLine(indent, "throw new NotImplementedException(url);"); this.text.WriteLine(--indent, "}"); this.text.WriteLine(indent, ""); } }
internal ReturnType FindReturnType(string name, string apiGroupName = null, string subPart = null, string typeName = null) { // find return type or null ReturnType returnItem = null; // handle different namespaces int dotPosition = name.LastIndexOf('.'); if (dotPosition > 0) { var ns = name.Substring(0, dotPosition); int dotPosition2 = ns.LastIndexOf('.'); if (dotPosition2 > 0) { ns = ns.Substring(0, dotPosition2); } if (apiGroupName != null) { apiGroupName = ns; } name = name.Substring(dotPosition + 1); } foreach (var group in this.Root.ApiGroups.Where(g => apiGroupName == null || g.Name == apiGroupName)) { if (returnItem != null) break; foreach (var item in group.ReturnTypes) { if (returnItem != null) break; if (item.Name == name || item.ClassName == name) { returnItem = item; } } } // create return type in specified API group if (apiGroupName != null && returnItem == null) { var item = new ReturnType { Name = name, ClassName = typeName ?? CSharpGenerator.Namify(name, NameTransformation.CamelCase), Fields = new List<Field>(), }; var group = this.Root.ApiGroups.Single(g => g.Name == apiGroupName); group.ReturnTypes.Add(item); returnItem = item; } if (returnItem != null) { if (subPart != null && subPart.First() == '(' && subPart.Last() == ')') { // subPart = "main:(sub)" // subPart = "main:(sub:(woot))" Field newField = null; { var parts = subPart.Substring(1, subPart.Length - 2).Split(new char[] { ':', }, 2); newField = new Field { Name = parts[0], ReturnType = name, Type = parts.Length > 1 && parts[1].Length > 2 ? CSharpGenerator.Namify(parts[0]) : null, }; returnItem.Fields.Add(newField); } { // split "main:(sub:(woot))" var parts = subPart.Substring(1, subPart.Length - 2).Split(new char[] { ':', '/', }, 2); var mainPart = parts.Length == 1 ? parts[0] : parts[0]; var subPart1 = parts.Length == 2 ? parts[1] : null; if (parts.Length > 1) { // "(sub:(woot))" var subReturnType = this.FindReturnType(mainPart, apiGroupName, subPart: subPart1); } } } else if (subPart != null) { // "main/sub" var parts = subPart.Split(new char[] { ':', '/', }, 2); returnItem.Fields.Add(new Field { Name = parts[0], ReturnType = name, }); } } return returnItem; }
private void WriteMethod(GeneratorContext context, ApiGroup apiGroup, int indent) { foreach (var method in apiGroup.Methods) { var returnType = "void"; ReturnType returnTypeType = null; if (method.ReturnType != null) { returnTypeType = context.Definition.FindReturnType(method.ReturnType, apiGroup.Name); if (returnTypeType != null) { returnType = this.GetPropertyName(returnTypeType.ClassName, returnTypeType.Name); returnType = returnTypeType.ApiGroup + "." + returnType; } } string postReturnType = null; ReturnType postReturnTypeType = null; if (method.PostReturnType != null) { postReturnTypeType = context.Definition.FindReturnType(method.PostReturnType, apiGroup.Name); if (postReturnTypeType != null) { postReturnType = this.GetPropertyName(postReturnTypeType.ClassName, postReturnTypeType.Name); postReturnType = postReturnTypeType.ApiGroup + "." + postReturnType; } } var parameters = new List <Parameter>(); if (method.RequiresUserAuthentication) { parameters.Add(new Parameter("user", "UserAuthorization")); } var urlParams = this.GetUrlPathParameters(method.Path, NameTransformation.PascalCase); foreach (var urlParam in urlParams) { parameters.Add(urlParam.Value); } if (method.UsesAcceptLanguage) { parameters.Add(new Parameter("acceptLanguages", "string[]", originalName: "acceptLanguages = null")); } // doc this.text.WriteLine(indent, "/// <summary>"); this.text.WriteLine(indent, "/// " + method.Title); this.text.WriteLine(indent, "/// </summary>"); if (method.Remark != null) { this.text.WriteLine(indent, "/// <remarks>"); this.text.WriteLine(indent, "/// " + method.Remark + ""); this.text.WriteLine(indent, "/// </remarks>"); } // name and arguments this.text.WriteLine(indent++, "public " + returnType + " " + this.GetPropertyName(method.MethodName, method.Path) + "("); var sep = " "; foreach (var parameter in parameters) { this.text.WriteLine(indent, sep + (parameter.Type ?? "string") + " " + parameter.Name + " " + (parameter.Value != null ? ("= " + parameter.Value) : string.Empty)); sep = ", "; } if (postReturnTypeType != null) { this.text.WriteLine(indent, ", " + postReturnType + " postData"); } if (returnType != null && method.UseFieldSelectors) { this.text.WriteLine(indent, ", FieldSelector<" + returnType + "> fields = null"); } this.text.WriteLine(--indent, ")"); this.text.WriteLine(indent++, "{"); // body / format url if (urlParams.Count > 0) { this.text.WriteLine(indent, "const string urlFormat = \"" + method.Path + "\";"); this.text.WriteLine(indent, "var url = FormatUrl(urlFormat, " + (method.UseFieldSelectors ? "fields" : "default(FieldSelector)") + ", " + string.Join(", ", urlParams.Values.Select(p => "\"" + p.OriginalName + "\", " + p.Name).ToArray()) + ");"); } else if (method.Path.Contains("FieldSelector")) { this.text.WriteLine(indent, "const string urlFormat = \"" + method.Path + "\";"); this.text.WriteLine(indent, "var url = FormatUrl(urlFormat, fields);"); } else { this.text.WriteLine(indent, "var url = \"" + method.Path + "\";"); } // body / create context this.text.WriteLine(); text.WriteLine(indent, "var context = new RequestContext();"); if (method.RequiresUserAuthentication) { text.WriteLine(indent, "context.UserAuthorization = user;"); } if (method.UsesAcceptLanguage) { text.WriteLine(indent, "context.AcceptLanguages = acceptLanguages;"); } text.WriteLine(indent, "context.Method = \"" + method.HttpMethod + "\";"); text.WriteLine(indent, "context.UrlPath = this.LinkedInApi.Configuration.BaseApiUrl + url;"); if (postReturnTypeType != null) { this.text.WriteLine(indent, "this.CreateJsonPostStream(context, postData);"); } // body / execute this.text.WriteLine(); text.WriteLine(indent++, "if (!this.ExecuteQuery(context))"); ////text.WriteLine(indent--, "this.HandleXmlErrorResponse(context);"); ////text.WriteLine(indent, "return this.HandleXmlResponse<" + returnType + ">(context);"); text.WriteLine(indent--, "this.HandleJsonErrorResponse(context);"); text.WriteLine(indent, ""); text.WriteLine(indent, "var result = this.HandleJsonResponse<" + returnType + ">(context);"); if (returnTypeType != null && returnTypeType.Headers != null) { foreach (var header in returnTypeType.Headers) { text.WriteLine(indent, "result." + header.PropertyName + " = this.ReadHeader<" + (header.Type ?? "string") + ">(context, \"" + header.Name + "\");"); } } text.WriteLine(indent, "return result;"); // body / handle // body / return ////this.text.WriteLine(indent, "throw new NotImplementedException(url);"); this.text.WriteLine(--indent, "}"); this.text.WriteLine(); this.text.WriteLine(--indent, "#if ASYNCTASKS"); indent++; // doc this.text.WriteLine(indent, "/// <summary>"); this.text.WriteLine(indent, "/// " + method.Title); this.text.WriteLine(indent, "/// </summary>"); if (method.Remark != null) { this.text.WriteLine(indent, "/// <remarks>"); this.text.WriteLine(indent, "/// " + method.Remark + ""); this.text.WriteLine(indent, "/// </remarks>"); } // name and arguments this.text.WriteLine(indent++, "public async Task<" + returnType + "> " + this.GetPropertyName(method.MethodName, method.Path) + "Async("); sep = " "; foreach (var parameter in parameters) { this.text.WriteLine(indent, sep + (parameter.Type ?? "string") + " " + parameter.Name + " " + (parameter.Value != null ? ("= " + parameter.Value) : string.Empty)); sep = ", "; } if (postReturnTypeType != null) { this.text.WriteLine(indent, ", " + postReturnType + " postData"); } if (returnType != null && method.UseFieldSelectors) { this.text.WriteLine(indent, ", FieldSelector<" + returnType + "> fields = null"); } this.text.WriteLine(--indent, ")"); this.text.WriteLine(indent++, "{"); // body / format url if (urlParams.Count > 0) { this.text.WriteLine(indent, "const string urlFormat = \"" + method.Path + "\";"); this.text.WriteLine(indent, "var url = FormatUrl(urlFormat, " + (method.UseFieldSelectors ? "fields" : "default(FieldSelector)") + ", " + string.Join(", ", urlParams.Values.Select(p => "\"" + p.OriginalName + "\", " + p.Name).ToArray()) + ");"); } else if (method.Path.Contains("FieldSelector")) { this.text.WriteLine(indent, "const string urlFormat = \"" + method.Path + "\";"); this.text.WriteLine(indent, "var url = FormatUrl(urlFormat, fields);"); } else { this.text.WriteLine(indent, "var url = \"" + method.Path + "\";"); } // body / create context this.text.WriteLine(); text.WriteLine(indent, "var context = new RequestContext();"); if (method.RequiresUserAuthentication) { text.WriteLine(indent, "context.UserAuthorization = user;"); } if (method.UsesAcceptLanguage) { text.WriteLine(indent, "context.AcceptLanguages = acceptLanguages;"); } text.WriteLine(indent, "context.Method = \"" + method.HttpMethod + "\";"); text.WriteLine(indent, "context.UrlPath = this.LinkedInApi.Configuration.BaseApiUrl + url;"); if (postReturnTypeType != null) { this.text.WriteLine(indent, "this.CreateJsonPostStream(context, postData);"); } // body / execute this.text.WriteLine(); text.WriteLine(indent, "var exec = await this.ExecuteQueryAsync(context);"); text.WriteLine(indent++, "if (!exec)"); ////text.WriteLine(indent--, "this.HandleXmlErrorResponse(context);"); ////text.WriteLine(indent, "return this.HandleXmlResponse<" + returnType + ">(context);"); text.WriteLine(indent--, "this.HandleJsonErrorResponse(context);"); text.WriteLine(indent, ""); text.WriteLine(indent, "var result = this.HandleJsonResponse<" + returnType + ">(context);"); if (returnTypeType != null && returnTypeType.Headers != null) { foreach (var header in returnTypeType.Headers) { text.WriteLine(indent, "result." + header.PropertyName + " = this.ReadHeader<" + (header.Type ?? "string") + ">(context, \"" + header.Name + "\");"); } } text.WriteLine(indent, "return result;"); // body / handle // body / return ////this.text.WriteLine(indent, "throw new NotImplementedException(url);"); this.text.WriteLine(--indent, "}"); this.text.WriteLine(--indent, "#endif"); indent++; this.text.WriteLine(indent, ""); } }